From 9a5d42c4a83d7d2fba037803089f1acdea991cc2 Mon Sep 17 00:00:00 2001 From: Alexandre Agostinho Date: Thu, 25 May 2023 22:44:16 +0200 Subject: [PATCH] import - export work! - fix get the recipes of the current user --- MCTG/ConsoleApp/Menu/ExportRecipeMenu.cs | 13 ++++++++++++- MCTG/ConsoleApp/Menu/SearchUserRecipes.cs | 15 ++++++++++----- MCTG/ConsoleApp/Menu/SearcherRecipe.cs | 2 +- MCTG/Model/Managers/MasterManager.cs | 4 ++++ 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/MCTG/ConsoleApp/Menu/ExportRecipeMenu.cs b/MCTG/ConsoleApp/Menu/ExportRecipeMenu.cs index 54c543f..1518a1c 100644 --- a/MCTG/ConsoleApp/Menu/ExportRecipeMenu.cs +++ b/MCTG/ConsoleApp/Menu/ExportRecipeMenu.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using DataPersistence; namespace ConsoleApp.Menu { @@ -22,10 +23,20 @@ namespace ConsoleApp.Menu throw new ArgumentNullException(); Recipe recipe = CurrentSelected; + string path = $"export_{string.Concat(CurrentSelected.Title.Where(c => !char.IsWhiteSpace(c)))}"; + + // -- trying to put the right extention by checking the type of the DataManager... + //if (object.ReferenceEquals(_masterMgr.DataMgr.GetType(), typeof(DataContractXML))) + // path += ".xml"; + //else if (object.ReferenceEquals(_masterMgr.DataMgr.GetType(), typeof(DataContractJSON))) + // path += ".json"; + + path += ".xml"; try { - _masterMgr.DataMgr.Export(recipe, ""); + + _masterMgr.DataMgr.Export(recipe, path); } catch (ArgumentNullException e) { diff --git a/MCTG/ConsoleApp/Menu/SearchUserRecipes.cs b/MCTG/ConsoleApp/Menu/SearchUserRecipes.cs index 638dd6c..abf9613 100644 --- a/MCTG/ConsoleApp/Menu/SearchUserRecipes.cs +++ b/MCTG/ConsoleApp/Menu/SearchUserRecipes.cs @@ -16,12 +16,17 @@ namespace ConsoleApp.Menu public override void Update() { - _recipeCollectionOnSearch = - new RecipeCollection("My recipes", _masterMgr.DataMgr.GetFromData() - .Where(r => r.AuthorMail == MasterManager.CurrentConnectedUser?.Mail) - .ToArray()); + _recipeCollectionOnSearch = _masterMgr.GetCurrentUserRecipes(); _allSelectors = ConvertRecipeCollectionInSelectors(); - base.Update(); + + _selectList = SearchInSelection(); + + if (_selectList.Count == 0) + { + CurrentSelected = default; + return; + } + CurrentSelected = _selectList[CurrentLine].Item; } } } diff --git a/MCTG/ConsoleApp/Menu/SearcherRecipe.cs b/MCTG/ConsoleApp/Menu/SearcherRecipe.cs index 896e2d5..7f5065a 100644 --- a/MCTG/ConsoleApp/Menu/SearcherRecipe.cs +++ b/MCTG/ConsoleApp/Menu/SearcherRecipe.cs @@ -44,7 +44,7 @@ namespace ConsoleApp.Menu public override IMenu? Return() { if (CurrentSelected == null) - throw new ArgumentNullException("CurrentSelected"); + return this; return new PlainText(CurrentSelected.ToString()); } diff --git a/MCTG/Model/Managers/MasterManager.cs b/MCTG/Model/Managers/MasterManager.cs index bce1c23..6ea49e3 100644 --- a/MCTG/Model/Managers/MasterManager.cs +++ b/MCTG/Model/Managers/MasterManager.cs @@ -75,5 +75,9 @@ namespace Model.Managers DataMgr.Data[nameof(Recipe)].Add(recipe); Recipes = DataMgr.GetRecipes(); } + + public RecipeCollection GetCurrentUserRecipes() + => new RecipeCollection("User recipes", + DataMgr.GetRecipes().FindAll(r => r.AuthorMail == CurrentConnectedUser?.Mail).ToArray()); } }