From 01fe143c7ff210308abb185a75ae5b01eaf2ecde Mon Sep 17 00:00:00 2001 From: Alexandre Agostinho Date: Wed, 24 May 2023 14:00:54 +0200 Subject: [PATCH] add some functions to simplify the access of data --- MCTG/ConsoleApp/Program.cs | 6 ++++-- MCTG/Model/Managers/DataManager.cs | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/MCTG/ConsoleApp/Program.cs b/MCTG/ConsoleApp/Program.cs index 2e438a9..c200562 100644 --- a/MCTG/ConsoleApp/Program.cs +++ b/MCTG/ConsoleApp/Program.cs @@ -23,9 +23,11 @@ dataMgr.Serializer = new DataContractXML(); //dataMgr.Import("C:\\Users\\alex6\\Downloads\\recipe2.json"); PasswordManager passwordManager = new PasswordManager(); -RecipeCollection rc = new RecipeCollection("All recipes", dataMgr.Data[nameof(Recipe)].Cast().ToArray()); +//RecipeCollection rc = new RecipeCollection("All recipes", dataMgr.Data[nameof(Recipe)].Cast().ToArray()); +RecipeCollection rc = dataMgr.GetRecipes("All recipes"); +//RecipeCollection rc = new RecipeCollection("All recipes", dataMgr.GetFromData().ToArray()); -User user = dataMgr.Data[nameof(User)].Cast().Last(); +User user = dataMgr.GetUsers().Last(); //rc[0].AddReview(new Review(user, 1, "bonne recette !1")); //rc[0].AddReview(new Review(user, 1, "bonne recette !2")); diff --git a/MCTG/Model/Managers/DataManager.cs b/MCTG/Model/Managers/DataManager.cs index 9b5277c..d84f433 100644 --- a/MCTG/Model/Managers/DataManager.cs +++ b/MCTG/Model/Managers/DataManager.cs @@ -76,6 +76,30 @@ namespace Model public void Export(T obj, string pathToExport) where T : class => Serializer.Export(obj, pathToExport); + + /// + /// Get all the recipe from the data. + /// + /// The title to give for the Recipe Collection + /// A RecipeCollection that contain all the recipe in the data. + public RecipeCollection GetRecipes(string rcTitle = "default") + => new RecipeCollection(rcTitle, Data[nameof(Recipe)].Cast().ToArray()); + + /// + /// Get all the Users from the data. + /// + /// A list of all Users. + public List GetUsers() + => new List(Data[nameof(User)].Cast()); + + /// + /// Get a list of an item in the data. + /// + /// The type of the item + /// The list of all the item found in the data. + public ICollection GetFromData() where T : class + => new List(Data[typeof(T).Name].Cast()); + #endregion } }