From 596e699382484dfc0647dd739b666ec5c97c2579 Mon Sep 17 00:00:00 2001 From: Alexandre Agostinho Date: Tue, 30 May 2023 17:22:43 +0200 Subject: [PATCH] add RecipeType and change ctors of Recipes --- MCTG/ConsoleApp/Menu/AddRecipeMenu.cs | 9 +- MCTG/DataPersistence/DataContractJSON.cs | 237 +++++++++++----------- MCTG/DataPersistence/DataContractXML.cs | 247 ++++++++++++----------- MCTG/DataPersistence/Stubs.cs | 118 ++++++++--- MCTG/Model/Managers/MasterManager.cs | 2 +- MCTG/Model/Recipes/Recipe.cs | 94 ++++----- MCTG/Model/Recipes/RecipeCollection.cs | 19 +- MCTG/Model/Recipes/RecipeType.cs | 16 ++ MCTG/Tests/Model_UnitTests/Recipe_UT.cs | 5 +- MCTG/Views/App.xaml.cs | 11 +- MCTG/Views/Home.xaml | 4 +- MCTG/Views/Home.xaml.cs | 28 ++- MCTG/Views/Views.csproj | 202 +++++++++--------- 13 files changed, 539 insertions(+), 453 deletions(-) create mode 100644 MCTG/Model/Recipes/RecipeType.cs diff --git a/MCTG/ConsoleApp/Menu/AddRecipeMenu.cs b/MCTG/ConsoleApp/Menu/AddRecipeMenu.cs index 353e168..5e66b49 100644 --- a/MCTG/ConsoleApp/Menu/AddRecipeMenu.cs +++ b/MCTG/ConsoleApp/Menu/AddRecipeMenu.cs @@ -39,12 +39,13 @@ namespace ConsoleApp.Menu Recipe recipe = new Recipe( title: title, + type: RecipeType.Unspecified, id: null, authorMail: masterMgr.CurrentConnectedUser?.Mail, - picture: "", - ingredients: new List(), - preparationSteps: steps.ToArray() - ); + picture: null) + { + PreparationSteps = steps + }; masterMgr.AddRecipe(recipe); return null; diff --git a/MCTG/DataPersistence/DataContractJSON.cs b/MCTG/DataPersistence/DataContractJSON.cs index 13879b7..b32cbd2 100644 --- a/MCTG/DataPersistence/DataContractJSON.cs +++ b/MCTG/DataPersistence/DataContractJSON.cs @@ -1,118 +1,119 @@ -using Model; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.Serialization; -using System.Runtime.Serialization.Json; -using System.Text; -using System.Threading.Tasks; -using System.Xml; -using System.Xml.Linq; - -namespace DataPersistence -{ - /// - /// Define a serializer to manage JSON files. - /// - public class DataContractJSON : IDataManager - { - #region Attributes - private string _jsonFolderPath; - private DataContractJsonSerializerSettings _dataContractJsonSerializerSettings; - #endregion - - #region Constructors - /// - /// Constructor of the DataContractJSON serializer. - /// - /// Give the default path where to load and save the data (by default is the current execution dir). - /// Give another set of DataContractJson serializer setting to write file - public DataContractJSON(string jsonFolderPath = "", - DataContractJsonSerializerSettings? dataContractJsonSerializerSettings = null) - { - _jsonFolderPath = jsonFolderPath; - if (dataContractJsonSerializerSettings is null) - _dataContractJsonSerializerSettings = new DataContractJsonSerializerSettings() - { - KnownTypes = new[] - { - typeof(Recipe), - typeof(Review), - typeof(User), - typeof(Ingredient), - typeof(Quantity) - } - }; - else - _dataContractJsonSerializerSettings = dataContractJsonSerializerSettings; - } - #endregion - - #region IDataManager implementation - public void Export(T obj, string pathToExport) - where T : class - { - var jsonSerializer = new DataContractJsonSerializer(typeof(T), _dataContractJsonSerializerSettings); - using (FileStream stream = File.Create(Path.Combine(_jsonFolderPath, pathToExport))) - { - using (var jsonWriter = JsonReaderWriterFactory.CreateJsonWriter( - stream: stream, - encoding: System.Text.Encoding.UTF8, - ownsStream: false, - indent: true)) - { - jsonSerializer.WriteObject(jsonWriter, obj); - } - } - } - - public KeyValuePair Import(string pathToImport) - where T : class - { - T? obj; - var jsonSerializer = new DataContractJsonSerializer(typeof(T), _dataContractJsonSerializerSettings); - using (FileStream stream = File.OpenRead(Path.Combine(_jsonFolderPath, pathToImport))) - { - obj = jsonSerializer.ReadObject(stream) as T; - } - - if (obj is null) - throw new ArgumentNullException("obj"); - - string typeName = typeof(T).Name; - return new KeyValuePair(typeName, obj); - } - - public Dictionary> Load() - { - Dictionary>? elements = new Dictionary>(); - var jsonSerializer = new DataContractJsonSerializer(typeof(Dictionary>), _dataContractJsonSerializerSettings); - using (FileStream stream = File.OpenRead(Path.Combine(_jsonFolderPath, "data.json"))) - { - elements = jsonSerializer.ReadObject(stream) as Dictionary>; - } - - if (elements is null) - throw new ArgumentNullException("elements"); - - return elements; - } - - public void Save(Dictionary> elements) - { - var jsonSerializer = new DataContractJsonSerializer(typeof(Dictionary>), _dataContractJsonSerializerSettings); - using (FileStream stream = File.Create(Path.Combine(_jsonFolderPath, "data.json"))) - { - using (var jsonWriter = JsonReaderWriterFactory.CreateJsonWriter( - stream: stream, - encoding: System.Text.Encoding.UTF8, - ownsStream: false, - indent: true)) - { - jsonSerializer.WriteObject(jsonWriter, elements); - } - } - } - #endregion - } -} +using Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Runtime.Serialization.Json; +using System.Text; +using System.Threading.Tasks; +using System.Xml; +using System.Xml.Linq; + +namespace DataPersistence +{ + /// + /// Define a serializer to manage JSON files. + /// + public class DataContractJSON : IDataManager + { + #region Attributes + private string _jsonFolderPath; + private DataContractJsonSerializerSettings _dataContractJsonSerializerSettings; + #endregion + + #region Constructors + /// + /// Constructor of the DataContractJSON serializer. + /// + /// Give the default path where to load and save the data (by default is the current execution dir). + /// Give another set of DataContractJson serializer setting to write file + public DataContractJSON(string jsonFolderPath = "", + DataContractJsonSerializerSettings? dataContractJsonSerializerSettings = null) + { + _jsonFolderPath = jsonFolderPath; + if (dataContractJsonSerializerSettings is null) + _dataContractJsonSerializerSettings = new DataContractJsonSerializerSettings() + { + KnownTypes = new[] + { + typeof(Recipe), + typeof(RecipeType), + typeof(Review), + typeof(User), + typeof(Ingredient), + typeof(Quantity) + } + }; + else + _dataContractJsonSerializerSettings = dataContractJsonSerializerSettings; + } + #endregion + + #region IDataManager implementation + public void Export(T obj, string pathToExport) + where T : class + { + var jsonSerializer = new DataContractJsonSerializer(typeof(T), _dataContractJsonSerializerSettings); + using (FileStream stream = File.Create(Path.Combine(_jsonFolderPath, pathToExport))) + { + using (var jsonWriter = JsonReaderWriterFactory.CreateJsonWriter( + stream: stream, + encoding: System.Text.Encoding.UTF8, + ownsStream: false, + indent: true)) + { + jsonSerializer.WriteObject(jsonWriter, obj); + } + } + } + + public KeyValuePair Import(string pathToImport) + where T : class + { + T? obj; + var jsonSerializer = new DataContractJsonSerializer(typeof(T), _dataContractJsonSerializerSettings); + using (FileStream stream = File.OpenRead(Path.Combine(_jsonFolderPath, pathToImport))) + { + obj = jsonSerializer.ReadObject(stream) as T; + } + + if (obj is null) + throw new ArgumentNullException("obj"); + + string typeName = typeof(T).Name; + return new KeyValuePair(typeName, obj); + } + + public Dictionary> Load() + { + Dictionary>? elements = new Dictionary>(); + var jsonSerializer = new DataContractJsonSerializer(typeof(Dictionary>), _dataContractJsonSerializerSettings); + using (FileStream stream = File.OpenRead(Path.Combine(_jsonFolderPath, "data.json"))) + { + elements = jsonSerializer.ReadObject(stream) as Dictionary>; + } + + if (elements is null) + throw new ArgumentNullException("elements"); + + return elements; + } + + public void Save(Dictionary> elements) + { + var jsonSerializer = new DataContractJsonSerializer(typeof(Dictionary>), _dataContractJsonSerializerSettings); + using (FileStream stream = File.Create(Path.Combine(_jsonFolderPath, "data.json"))) + { + using (var jsonWriter = JsonReaderWriterFactory.CreateJsonWriter( + stream: stream, + encoding: System.Text.Encoding.UTF8, + ownsStream: false, + indent: true)) + { + jsonSerializer.WriteObject(jsonWriter, elements); + } + } + } + #endregion + } +} diff --git a/MCTG/DataPersistence/DataContractXML.cs b/MCTG/DataPersistence/DataContractXML.cs index 910d26d..b3d1d50 100644 --- a/MCTG/DataPersistence/DataContractXML.cs +++ b/MCTG/DataPersistence/DataContractXML.cs @@ -1,123 +1,124 @@ -using Model; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.Serialization; -using System.Text; -using System.Threading.Tasks; -using System.Xml; -using System.Xml.Linq; - -namespace DataPersistence -{ - /// - /// Define a serializer to manage XML files. - /// - public class DataContractXML : IDataManager - { - #region Attributes - private string _xmlFolderPath; - private XmlWriterSettings _xmlWriterSettings; - private DataContractSerializerSettings _dataContractSerializerSettings; - #endregion - - #region Constructors - /// - /// Constructor of a DataContractXML serializer. - /// - /// Give the default path where to load and save the data (by default is the current execution dir). - /// Give another set of XML setting to write file. - /// Give another set of DataContract serializer setting to write file - public DataContractXML(string xmlFolderPath = "", - XmlWriterSettings? xmlWriterSettings = null, - DataContractSerializerSettings? dataContractSerializerSettings = null) - { - _xmlFolderPath = xmlFolderPath; - - if (xmlWriterSettings is null) - _xmlWriterSettings = new XmlWriterSettings() { Indent = true }; - else - _xmlWriterSettings = xmlWriterSettings; - - if (dataContractSerializerSettings is null) - _dataContractSerializerSettings = new DataContractSerializerSettings() - { - KnownTypes = new Type[] - { - typeof(Recipe), - typeof(Review), - typeof(User), - typeof(Ingredient), - typeof(Quantity), - typeof(PasswordSHA256) - }, - PreserveObjectReferences = true - }; - else - _dataContractSerializerSettings = dataContractSerializerSettings; - } - #endregion - - #region IDataManager implementation - public void Export(T obj, string pathToExport) - where T : class - { - bool restore = _dataContractSerializerSettings.PreserveObjectReferences; - _dataContractSerializerSettings.PreserveObjectReferences = false; - var serializer = new DataContractSerializer(typeof(T), _dataContractSerializerSettings); - using (TextWriter textWriter = File.CreateText(Path.Combine(_xmlFolderPath, pathToExport))) - { - using (XmlWriter xmlWriter = XmlWriter.Create(textWriter, _xmlWriterSettings)) - { - serializer.WriteObject(xmlWriter, obj); - } - } - _dataContractSerializerSettings.PreserveObjectReferences = restore; - } - - public KeyValuePair Import(string pathToImport) - where T : class - { - T? obj; - var serializer = new DataContractSerializer(typeof(T), _dataContractSerializerSettings); - using (Stream s = File.OpenRead(Path.Combine(_xmlFolderPath, pathToImport))) - { - obj = serializer.ReadObject(s) as T; - } - - if (obj is null) - throw new ArgumentNullException("obj"); - - string typeName = typeof(T).Name; - return new KeyValuePair(typeName, obj); - } - - public Dictionary> Load() - { - Dictionary>? elements; - var serializer = new DataContractSerializer(typeof(Dictionary>), _dataContractSerializerSettings); - using (Stream s = File.OpenRead(Path.Combine(_xmlFolderPath, "data.xml"))) - { - elements = serializer.ReadObject(s) as Dictionary>; - } - - if (elements is null) - throw new ArgumentNullException("elements"); - - return elements; - } - - public void Save(Dictionary> elements) - { - var serializer = new DataContractSerializer(typeof(Dictionary>), _dataContractSerializerSettings); - using (TextWriter textWriter = File.CreateText(Path.Combine(_xmlFolderPath, "data.xml"))) - { - using (XmlWriter xmlWriter = XmlWriter.Create(textWriter, _xmlWriterSettings)) - { - serializer.WriteObject(xmlWriter, elements); - } - } - } - #endregion - } -} +using Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; +using System.Xml; +using System.Xml.Linq; + +namespace DataPersistence +{ + /// + /// Define a serializer to manage XML files. + /// + public class DataContractXML : IDataManager + { + #region Attributes + private string _xmlFolderPath; + private XmlWriterSettings _xmlWriterSettings; + private DataContractSerializerSettings _dataContractSerializerSettings; + #endregion + + #region Constructors + /// + /// Constructor of a DataContractXML serializer. + /// + /// Give the default path where to load and save the data (by default is the current execution dir). + /// Give another set of XML setting to write file. + /// Give another set of DataContract serializer setting to write file + public DataContractXML(string xmlFolderPath = "", + XmlWriterSettings? xmlWriterSettings = null, + DataContractSerializerSettings? dataContractSerializerSettings = null) + { + _xmlFolderPath = xmlFolderPath; + + if (xmlWriterSettings is null) + _xmlWriterSettings = new XmlWriterSettings() { Indent = true }; + else + _xmlWriterSettings = xmlWriterSettings; + + if (dataContractSerializerSettings is null) + _dataContractSerializerSettings = new DataContractSerializerSettings() + { + KnownTypes = new Type[] + { + typeof(Recipe), + typeof(RecipeType), + typeof(Review), + typeof(User), + typeof(Ingredient), + typeof(Quantity), + typeof(PasswordSHA256) + }, + PreserveObjectReferences = true + }; + else + _dataContractSerializerSettings = dataContractSerializerSettings; + } + #endregion + + #region IDataManager implementation + public void Export(T obj, string pathToExport) + where T : class + { + bool restore = _dataContractSerializerSettings.PreserveObjectReferences; + _dataContractSerializerSettings.PreserveObjectReferences = false; + var serializer = new DataContractSerializer(typeof(T), _dataContractSerializerSettings); + using (TextWriter textWriter = File.CreateText(Path.Combine(_xmlFolderPath, pathToExport))) + { + using (XmlWriter xmlWriter = XmlWriter.Create(textWriter, _xmlWriterSettings)) + { + serializer.WriteObject(xmlWriter, obj); + } + } + _dataContractSerializerSettings.PreserveObjectReferences = restore; + } + + public KeyValuePair Import(string pathToImport) + where T : class + { + T? obj; + var serializer = new DataContractSerializer(typeof(T), _dataContractSerializerSettings); + using (Stream s = File.OpenRead(Path.Combine(_xmlFolderPath, pathToImport))) + { + obj = serializer.ReadObject(s) as T; + } + + if (obj is null) + throw new ArgumentNullException("obj"); + + string typeName = typeof(T).Name; + return new KeyValuePair(typeName, obj); + } + + public Dictionary> Load() + { + Dictionary>? elements; + var serializer = new DataContractSerializer(typeof(Dictionary>), _dataContractSerializerSettings); + using (Stream s = File.OpenRead(Path.Combine(_xmlFolderPath, "data.xml"))) + { + elements = serializer.ReadObject(s) as Dictionary>; + } + + if (elements is null) + throw new ArgumentNullException("elements"); + + return elements; + } + + public void Save(Dictionary> elements) + { + var serializer = new DataContractSerializer(typeof(Dictionary>), _dataContractSerializerSettings); + using (TextWriter textWriter = File.CreateText(Path.Combine(_xmlFolderPath, "data.xml"))) + { + using (XmlWriter xmlWriter = XmlWriter.Create(textWriter, _xmlWriterSettings)) + { + serializer.WriteObject(xmlWriter, elements); + } + } + } + #endregion + } +} diff --git a/MCTG/DataPersistence/Stubs.cs b/MCTG/DataPersistence/Stubs.cs index a6fece1..925dbae 100644 --- a/MCTG/DataPersistence/Stubs.cs +++ b/MCTG/DataPersistence/Stubs.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; namespace DataPersistence @@ -21,77 +22,123 @@ namespace DataPersistence nameof(Recipe), new List(new[] { - new Recipe( - title: "Cookies classiques", - id: 50, - authorMail: "admin@mctg.fr", - picture : "room_service_icon.png", - ingredients: new List(new[] + new Recipe("Cookies classiques", RecipeType.Dessert, null, "admin@mctg.fr", "") + { + Ingredients = new List { new Ingredient("Patates", new Quantity(23, Unit.unit)), new Ingredient("Farine", new Quantity(23, Unit.G)) - }), - preparationSteps: new[] + }, + PreparationSteps = new List { new PreparationStep(1, "Faire cuire."), new PreparationStep(2, "Manger.") - }), + }, + Reviews = new List + { + new Review(4, "Bonne recette, je recommande !"), + new Review(3, "Bof bof, mais mangeable...") + } + }, new Recipe( - authorMail: "admin@mctg.fr", title: "Cookies au chocolat", - id: null, - preparationSteps: new[] + type: RecipeType.Dessert) + { + Ingredients = new List + { + new Ingredient("Farine", new Quantity(200, Unit.G)) + }, + PreparationSteps = new List() { new PreparationStep(1, "Moulinez la pâte."), new PreparationStep(2, "Faire cuire pendant une bonne heure."), new PreparationStep(3, "Sortir du four et mettre dans un plat.") - }), + } + }, new Recipe( - title: "Gateau nature", id: null, - authorMail: "admin@mctg.fr", - preparationSteps: new[] + title: "Gateau nature", + type: RecipeType.Dessert) + { + Ingredients = new List + { + new Ingredient("Farine", new Quantity(200, Unit.G)), + new Ingredient("Lait", new Quantity(2, Unit.L)) + }, + PreparationSteps = new List { new PreparationStep(1, "Achetez les ingrédients."), new PreparationStep(2, "Préparez le matériel. Ustensiles et tout."), new PreparationStep(3, "Pleurez.") - }), + }, + Reviews = new List + { + new Review("pedrosamigos@hotmail.com", 5, "C'était vraiment IN-CROY-ABLE !!!") + } + }, new Recipe( - title: "Gateau au pommes", id: null, - authorMail: "admin@mctg.fr", - preparationSteps: new[] + title: "Gateau au pommes", + type: RecipeType.Dessert) + { + PreparationSteps = new List { new PreparationStep(1, "Achetez les légumes."), new PreparationStep(2, "Préparez le plat. Ustensiles et préchauffez le four."), new PreparationStep(3, "Coupez les pommes en morceaux et disposez-les sur le plat."), new PreparationStep(4, "Mettez enfin le plat au four, puis une fois cuit, dégustez !") - }), + } + }, new Recipe( - title: "Gateau au chocolat", id: null, - authorMail: "pedrosamigos@hotmail.com", - preparationSteps: new[] + title: "Gateau au chocolat", + type: RecipeType.Dessert, + id: null, authorMail: "pedrosamigos@hotmail.com") + { + Ingredients = new List + { + new Ingredient("Mais", new Quantity(2, Unit.kG)), + new Ingredient("Sachet pépites de chocolat", new Quantity(1, Unit.unit)), + new Ingredient("Dinde", new Quantity(2, Unit.G)) + }, + PreparationSteps = new List { new PreparationStep(1, "Ajouter les oeufs."), new PreparationStep(2, "Ajouter la farine."), new PreparationStep(3, "Ajouter 100g de chocolat fondu."), new PreparationStep(4, "Mélanger le tout."), new PreparationStep(5, "Faire cuire 45h au four traditionnel.") - }), + } + }, new Recipe( title: "Dinde au jambon", - id: null, - authorMail: "pedrosamigos@hotmail.com", - preparationSteps: new[] + type: RecipeType.Dish, + id: null, authorMail: "pedrosamigos@hotmail.com") + { + Ingredients = new List + { + new Ingredient("Morceaux de bois", new Quantity(2, Unit.unit)), + new Ingredient("Sachet gélatine", new Quantity(1, Unit.unit)), + new Ingredient("Jambon", new Quantity(2, Unit.kG)) + }, + PreparationSteps = new List { new PreparationStep(1, "Faire une cuisson bien sec de la dinde à la poêle"), new PreparationStep(2, "Mettre la dinde au frigo."), new PreparationStep(3, "Mettre le jambon dans le micro-onde."), new PreparationStep(4, "Faire chauffer 3min."), new PreparationStep(5, "Présentez sur un plat la dinde et le jambon : Miam !") - }), + } + }, new Recipe( - title: "Poulet au curry", id: null, - authorMail: "pedrosamigos@hotmail.com", - preparationSteps: new[] + title: "Poulet au curry", + type: RecipeType.Dish, + id: null, authorMail: "pedrosamigos@hotmail.com") + { + Ingredients = new List + { + new Ingredient("Pissenlis", new Quantity(200, Unit.unit)), + new Ingredient("Boule de pétanque", new Quantity(10, Unit.unit)), + new Ingredient("Poivre", new Quantity(4, Unit.mG)) + }, + PreparationSteps = new List { new PreparationStep(1, "Trouvez des épices de curry."), new PreparationStep(2, "Trouvez maintenant du poulet."), @@ -99,7 +146,12 @@ namespace DataPersistence new PreparationStep(4, "Parsemez d'épices curry la tête de la poule."), new PreparationStep(5, "Mettre le tout au four traditionnel 30min."), new PreparationStep(6, "Dégustez en famille !") - }) + }, + Reviews = new List + { + new Review(5, "Meilleure recette que j'ai avalé de tout les temps !!!!!!!") + } + } }) #endregion }, diff --git a/MCTG/Model/Managers/MasterManager.cs b/MCTG/Model/Managers/MasterManager.cs index d9efaf1..aed3c0b 100644 --- a/MCTG/Model/Managers/MasterManager.cs +++ b/MCTG/Model/Managers/MasterManager.cs @@ -148,7 +148,7 @@ namespace Model.Managers /// The current connected user's personal recipes. public RecipeCollection GetCurrentUserRecipes() => new RecipeCollection("User recipes", - DataMgr.GetRecipes().FindAll(r => r.AuthorMail == CurrentConnectedUser?.Mail).ToArray()); + DataMgr.GetRecipes().ToList().FindAll(r => r.AuthorMail == CurrentConnectedUser?.Mail).ToArray()); /// /// Notify property change handler. diff --git a/MCTG/Model/Recipes/Recipe.cs b/MCTG/Model/Recipes/Recipe.cs index 9f220ca..7ba928a 100644 --- a/MCTG/Model/Recipes/Recipe.cs +++ b/MCTG/Model/Recipes/Recipe.cs @@ -19,6 +19,9 @@ namespace Model [DataMember(Name = "image")] private string _image = ""; + + [DataMember(Name = "authorMail")] + private string _authorMail = ""; #endregion #region Properties @@ -32,15 +35,22 @@ namespace Model /// List of reviews of this recipe. /// [DataMember(Name = "reviews")] - public List Reviews { get; private set; } + public List Reviews { get; set; } /// /// AuthorMail's mail of the recipe. /// - [DataMember(Name = "authorMail")] - public string? AuthorMail { get; private set; } - - public string Toto { get; set; } = "Coucou"; + public string? AuthorMail + { + get => _authorMail; + set + { + if (string.IsNullOrEmpty(value)) + _authorMail = "admin@mctg.fr"; + else + _authorMail = value; + } + } /// /// The Title of the recipe.
@@ -67,12 +77,14 @@ namespace Model get => _image; set { - if (!string.IsNullOrWhiteSpace(value)) + if (string.IsNullOrWhiteSpace(value)) _image = "room_service_icon.png"; - _image = value; + else + _image = value; } } + /// /// The list of ingredients. /// [DataMember(Name = "ingredient")] @@ -84,30 +96,34 @@ namespace Model ///
[DataMember(Name = "preparation-steps")] public List PreparationSteps { get; set; } + + /// + /// The type of recipe. + /// + [DataMember(Name = "type")] + public RecipeType Type { get; private set; } #endregion #region Constructors /// /// Construct a new recipe. /// - /// The title of the recipe - /// The id of the recipe. If not given, get a new id. - /// The name of the user that create this recipe. - /// The image that represent the recipe - /// Thr list of reviews. - /// Thr list of ingredients. - /// The steps of the preparation of the meal - public Recipe(string title, int? id, string? authorMail, string? picture, - List reviews, List ingredients, - params PreparationStep[] preparationSteps) + /// The title of the recipe + /// The type of the recipe. + /// The id of the recipe. If not given, get a new id. + /// The name of the user that create this recipe. + /// The image that represent the recipe + public Recipe(string title, RecipeType type, int? id, string? authorMail, string? picture) { Title = title; + Type = type; Image = picture; - PreparationSteps = new List(preparationSteps); - Ingredients = ingredients; - Reviews = reviews; AuthorMail = authorMail; + PreparationSteps = new List(); + Ingredients = new List(); + Reviews = new List(); + if (id == null) { var randomGenerator = RandomNumberGenerator.Create(); @@ -118,46 +134,20 @@ namespace Model else Id = (int)id; } - - /// - /// - /// - /// The title of the recipe. - /// The id of the recipe. If not given, get a new id. - /// Mail of the user that create the recipe - /// The steps of the preparation of the meal. - public Recipe(string title, int? id, string? authorMail, params PreparationStep[] preparationSteps) - : this(title, id, authorMail, null, new List(), new List(), preparationSteps) + public Recipe(string title, RecipeType type, int? id, string? authorMail) + : this(title, type, id, authorMail, null) { - } - /// - /// - /// - /// The title of the recipe. - /// The id of the recipe. If not given, get a new id. - /// Mail of the user that create the recipe - /// Mail of the user that create the recipe - /// List of ingredients that compose the recipe. - /// The steps of the preparation of the meal. - public Recipe(string title, int? id, string? authorMail, string? picture, List ingredients, params PreparationStep[] preparationSteps) - : this(title, id, authorMail, picture, new List(), ingredients, preparationSteps) + public Recipe(string title, RecipeType type) + : this(title, type, null, null) { } - /// - /// - /// - /// The title of the recipe. - /// The id of the recipe. If not given, get a new id. - /// Image that reppresent the recipe. - /// The steps of the preparation of the meal. - public Recipe() - : this("", null, null, null, new List(), new List(),new PreparationStep[0]) + public Recipe(string title) + : this(title, RecipeType.Unspecified) { } - #endregion #region Methods diff --git a/MCTG/Model/Recipes/RecipeCollection.cs b/MCTG/Model/Recipes/RecipeCollection.cs index 051d37c..dcbcb90 100644 --- a/MCTG/Model/Recipes/RecipeCollection.cs +++ b/MCTG/Model/Recipes/RecipeCollection.cs @@ -8,9 +8,10 @@ namespace Model { /// /// Define a collection of . - ///
This class implement and . + ///
This class is derived from + /// and implement and . ///
- public class RecipeCollection : List, IEquatable + public class RecipeCollection : ObservableCollection, IEquatable, ICloneable { #region Attributes private string _description = ""; @@ -56,7 +57,8 @@ namespace Model /// public Recipe? GetRecipeById(int id) { - Recipe? recipe = this.Find(r => r.Id == id); + Recipe? recipe = this.ToList().Find(r => r.Id == id); + if (recipe == null) throw new ArgumentException("No _recipes match the given id."); return recipe; } @@ -73,7 +75,7 @@ namespace Model return new RecipeCollection( description: $"Results of the research: {str}", - recipes: this.FindAll(x => x.Title.ToLower().Contains(str.ToLower())).ToArray()); + recipes: this.ToList().FindAll(x => x.Title.ToLower().Contains(str.ToLower())).ToArray()); } public virtual bool Equals(RecipeCollection? other) @@ -103,7 +105,14 @@ namespace Model sb.AppendFormat("\t - {0}\n", r.ToString()); } return sb.ToString(); - } + } + + public object Clone() + { + return new RecipeCollection( + description: this.Description, + recipes: this.ToArray()); + } #endregion } } diff --git a/MCTG/Model/Recipes/RecipeType.cs b/MCTG/Model/Recipes/RecipeType.cs new file mode 100644 index 0000000..be56ef7 --- /dev/null +++ b/MCTG/Model/Recipes/RecipeType.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Model +{ + public enum RecipeType + { + Unspecified, + Starter, + Dish, + Dessert + } +} diff --git a/MCTG/Tests/Model_UnitTests/Recipe_UT.cs b/MCTG/Tests/Model_UnitTests/Recipe_UT.cs index a16deb8..b98ab2a 100644 --- a/MCTG/Tests/Model_UnitTests/Recipe_UT.cs +++ b/MCTG/Tests/Model_UnitTests/Recipe_UT.cs @@ -8,9 +8,8 @@ namespace Model_UnitTests public void TestVoidConstructor() { Recipe r = new Recipe( - title: "test recipe", - id: null, - authorMail: "test@test.fr"); + title: "test recipe", type: RecipeType.Unspecified); + Assert.NotNull(r.Title); } } diff --git a/MCTG/Views/App.xaml.cs b/MCTG/Views/App.xaml.cs index fb3bc6d..da53cbf 100644 --- a/MCTG/Views/App.xaml.cs +++ b/MCTG/Views/App.xaml.cs @@ -16,14 +16,17 @@ using System.Runtime.CompilerServices; namespace Views { public partial class App : Application - { + { + private Recipe currentRecipe { get; set; } + /// /// Master manager - access to the Model. /// public MasterManager MasterMgr { get; private set; } = new MasterManager(new Stubs()); - //L'utilisateur courant de l'application - public User CurrentUser { get; set; } - private Recipe currentRecipe { get; set; } + + /// + /// Current selected recipe. + /// public Recipe CurrentRecipe { get => currentRecipe; diff --git a/MCTG/Views/Home.xaml b/MCTG/Views/Home.xaml index 9647a99..2a7b048 100644 --- a/MCTG/Views/Home.xaml +++ b/MCTG/Views/Home.xaml @@ -31,7 +31,8 @@