diff --git a/MCTG/ConsoleApp/Program.cs b/MCTG/ConsoleApp/Program.cs index 7430c33..d12e076 100644 --- a/MCTG/ConsoleApp/Program.cs +++ b/MCTG/ConsoleApp/Program.cs @@ -11,13 +11,18 @@ Console.WriteLine("Hello, World!\n\n"); // TESTS: -DataManager dataMgr = new DataManager(new DataContractXML()); +//DataManager dataMgr = new DataManager(new Stubs()); + +//DataManager dataMgr = new DataManager(new DataContractXML(xmlFolderPath: "../../../../DataPersistence/data")); +DataManager dataMgr = new DataManager(new DataContractJSON(jsonFolderPath: "../../../../DataPersistence/data")); + RecipeCollection rc = new RecipeCollection("All recipes", dataMgr.Data["recipes"].Cast().ToArray()); Recipe? ret = SearcherRecipe.ResearchOn(rc); Console.WriteLine(ret); -//dataMgr.Serializer = new DataContractXML(); +//dataMgr.Serializer = new DataContractXML(xmlFolderPath: "../../../../DataPersistence/data"); +//dataMgr.Serializer = new DataContractJSON(jsonFolderPath: "../../../../DataPersistence/data"); dataMgr.Save(); // press any key to quit diff --git a/MCTG/DataPersistence/DataContractJSON.cs b/MCTG/DataPersistence/DataContractJSON.cs new file mode 100644 index 0000000..524eaac --- /dev/null +++ b/MCTG/DataPersistence/DataContractJSON.cs @@ -0,0 +1,62 @@ +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; + +namespace DataPersistence +{ + public class DataContractJSON : IDataManager + { + private string _jsonFolderPath; + private DataContractJsonSerializerSettings _dataContractJsonSerializerSettings; + + public DataContractJSON(string jsonFolderPath = "", + DataContractJsonSerializerSettings? dataContractJsonSerializerSettings = null) + { + _jsonFolderPath = jsonFolderPath; + if (dataContractJsonSerializerSettings is null) + _dataContractJsonSerializerSettings = new DataContractJsonSerializerSettings() + { + KnownTypes = new[] { typeof(Recipe) } + }; + else + _dataContractJsonSerializerSettings = dataContractJsonSerializerSettings; + } + + 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); + } + } + } + } +} diff --git a/MCTG/DataPersistence/DataContractXML.cs b/MCTG/DataPersistence/DataContractXML.cs index 9d10475..833919e 100644 --- a/MCTG/DataPersistence/DataContractXML.cs +++ b/MCTG/DataPersistence/DataContractXML.cs @@ -13,21 +13,33 @@ namespace DataPersistence { private string _xmlFolderPath; private XmlWriterSettings _xmlWriterSettings; + private DataContractSerializerSettings _dataContractSerializerSettings; public DataContractXML(string xmlFolderPath = "", - XmlWriterSettings? xmlWriterSettings = null) + 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) }, + PreserveObjectReferences = true + }; + else + _dataContractSerializerSettings = dataContractSerializerSettings; } public Dictionary> Load() { Dictionary>? elements = new Dictionary>(); - var serializer = new DataContractSerializer(typeof(Dictionary>), new Type[] { typeof(Recipe) }); + var serializer = new DataContractSerializer(typeof(Dictionary>), _dataContractSerializerSettings); using (Stream s = File.OpenRead(Path.Combine(_xmlFolderPath, "data.xml"))) { elements = serializer.ReadObject(s) as Dictionary>; @@ -41,7 +53,7 @@ namespace DataPersistence public void Save(Dictionary> elements) { - var serializer = new DataContractSerializer(typeof(Dictionary>), new Type[] { typeof(Recipe) }); + var serializer = new DataContractSerializer(typeof(Dictionary>), _dataContractSerializerSettings); using (TextWriter textWriter = File.CreateText(Path.Combine(_xmlFolderPath, "data.xml"))) { using (XmlWriter xmlWriter = XmlWriter.Create(textWriter, _xmlWriterSettings)) diff --git a/MCTG/DataPersistence/DataManager.cs b/MCTG/DataPersistence/DataManager.cs index 6ea5843..7520704 100644 --- a/MCTG/DataPersistence/DataManager.cs +++ b/MCTG/DataPersistence/DataManager.cs @@ -12,11 +12,8 @@ namespace DataPersistence public IDataManager Serializer { get; set; } public Dictionary> Data { get; private set; } - public DataManager(IDataManager dataMgr, string? path = null) + public DataManager(IDataManager dataMgr) { - //if (path is null) path = "..//..//..//..//data"; - //Directory.SetCurrentDirectory(Path.Combine(Directory.GetCurrentDirectory(), path)); - Serializer = dataMgr; Data = Serializer.Load(); } diff --git a/MCTG/DataPersistence/DataPersistence.csproj b/MCTG/DataPersistence/DataPersistence.csproj index 4b28c7b..d5050a6 100644 --- a/MCTG/DataPersistence/DataPersistence.csproj +++ b/MCTG/DataPersistence/DataPersistence.csproj @@ -10,4 +10,8 @@ + + + + diff --git a/MCTG/DataPersistence/data/data.json b/MCTG/DataPersistence/data/data.json new file mode 100644 index 0000000..7b89454 --- /dev/null +++ b/MCTG/DataPersistence/data/data.json @@ -0,0 +1,168 @@ +[ + { + "Key": "recipes", + "Value": [ + { + "__type": "recipe:#Model", + "id": 3005, + "preparation-steps": [ + { + "description": "Faire cuire.", + "order": 1 + }, + { + "description": "Manger.", + "order": 2 + } + ], + "title": "Cookies classiques" + }, + { + "__type": "recipe:#Model", + "id": 28377, + "preparation-steps": [ + { + "description": "Moulinez la pâte.", + "order": 1 + }, + { + "description": "Faire cuire pendant une bonne heure.", + "order": 2 + }, + { + "description": "Sortir du four et mettre dans un plat.", + "order": 3 + } + ], + "title": "Cookies au chocolat" + }, + { + "__type": "recipe:#Model", + "id": 10495, + "preparation-steps": [ + { + "description": "Achetez les ingrédients.", + "order": 1 + }, + { + "description": "Préparez le matériel. Ustensiles et tout.", + "order": 2 + }, + { + "description": "Pleurez.", + "order": 3 + } + ], + "title": "Gateau nature" + }, + { + "__type": "recipe:#Model", + "id": 32282, + "preparation-steps": [ + { + "description": "Achetez les légumes.", + "order": 1 + }, + { + "description": "Préparez le plat. Ustensiles et préchauffez le four.", + "order": 2 + }, + { + "description": "Coupez les pommes en morceaux et disposez-les sur le plat.", + "order": 3 + }, + { + "description": "Mettez enfin le plat au four, puis une fois cuit, dégustez !", + "order": 4 + } + ], + "title": "Gateau au pommes" + }, + { + "__type": "recipe:#Model", + "id": 26518, + "preparation-steps": [ + { + "description": "Ajouter les oeufs.", + "order": 1 + }, + { + "description": "Ajouter la farine.", + "order": 2 + }, + { + "description": "Ajouter 100g de chocolat fondu.", + "order": 3 + }, + { + "description": "Mélanger le tout.", + "order": 4 + }, + { + "description": "Faire cuire 45h au four traditionnel.", + "order": 5 + } + ], + "title": "Gateau au chocolat" + }, + { + "__type": "recipe:#Model", + "id": 21970, + "preparation-steps": [ + { + "description": "Faire une cuisson bien sec de la dinde à la poêle", + "order": 1 + }, + { + "description": "Mettre la dinde au frigo.", + "order": 2 + }, + { + "description": "Mettre le jambon dans le micro-onde.", + "order": 3 + }, + { + "description": "Faire chauffer 3min.", + "order": 4 + }, + { + "description": "Présentez sur un plat la dinde et le jambon : Miam !", + "order": 5 + } + ], + "title": "Dinde au jambon" + }, + { + "__type": "recipe:#Model", + "id": 12830, + "preparation-steps": [ + { + "description": "Trouvez des épices de curry.", + "order": 1 + }, + { + "description": "Trouvez maintenant du poulet.", + "order": 2 + }, + { + "description": "Coupez la tête du poulet et posez-la dans un plat.", + "order": 3 + }, + { + "description": "Parsemez d'épices curry la tête de la poule.", + "order": 4 + }, + { + "description": "Mettre le tout au four traditionnel 30min.", + "order": 5 + }, + { + "description": "Dégustez en famille !", + "order": 6 + } + ], + "title": "Poulet au curry" + } + ] + } +] \ No newline at end of file diff --git a/MCTG/DataPersistence/data/data.xml b/MCTG/DataPersistence/data/data.xml new file mode 100644 index 0000000..731915c --- /dev/null +++ b/MCTG/DataPersistence/data/data.xml @@ -0,0 +1,162 @@ + + + + recipes + + + 3005 + + + Faire cuire. + 1 + + + Manger. + 2 + + + Cookies classiques + + + 28377 + + + Moulinez la pâte. + 1 + + + Faire cuire pendant une bonne heure. + 2 + + + Sortir du four et mettre dans un plat. + 3 + + + Cookies au chocolat + + + 10495 + + + Achetez les ingrédients. + 1 + + + Préparez le matériel. Ustensiles et tout. + 2 + + + Pleurez. + 3 + + + Gateau nature + + + 32282 + + + Achetez les légumes. + 1 + + + Préparez le plat. Ustensiles et préchauffez le four. + 2 + + + Coupez les pommes en morceaux et disposez-les sur le plat. + 3 + + + Mettez enfin le plat au four, puis une fois cuit, dégustez ! + 4 + + + Gateau au pommes + + + 26518 + + + Ajouter les oeufs. + 1 + + + Ajouter la farine. + 2 + + + Ajouter 100g de chocolat fondu. + 3 + + + Mélanger le tout. + 4 + + + Faire cuire 45h au four traditionnel. + 5 + + + Gateau au chocolat + + + 21970 + + + Faire une cuisson bien sec de la dinde à la poêle + 1 + + + Mettre la dinde au frigo. + 2 + + + Mettre le jambon dans le micro-onde. + 3 + + + Faire chauffer 3min. + 4 + + + Présentez sur un plat la dinde et le jambon : Miam ! + 5 + + + Dinde au jambon + + + 12830 + + + Trouvez des épices de curry. + 1 + + + Trouvez maintenant du poulet. + 2 + + + Coupez la tête du poulet et posez-la dans un plat. + 3 + + + Parsemez d'épices curry la tête de la poule. + 4 + + + Mettre le tout au four traditionnel 30min. + 5 + + + Dégustez en famille ! + 6 + + + Poulet au curry + + + + \ No newline at end of file