diff --git a/MCTG/ConsoleApp/Program.cs b/MCTG/ConsoleApp/Program.cs index d12e076..b0eb3f4 100644 --- a/MCTG/ConsoleApp/Program.cs +++ b/MCTG/ConsoleApp/Program.cs @@ -11,18 +11,21 @@ Console.WriteLine("Hello, World!\n\n"); // TESTS: -//DataManager dataMgr = new DataManager(new Stubs()); - +DataManager dataMgr = new DataManager(new Stubs()); //DataManager dataMgr = new DataManager(new DataContractXML(xmlFolderPath: "../../../../DataPersistence/data")); -DataManager dataMgr = new DataManager(new DataContractJSON(jsonFolderPath: "../../../../DataPersistence/data")); +//DataManager dataMgr = new DataManager(new DataContractJSON(jsonFolderPath: "../../../../DataPersistence/data")); + +dataMgr.Serializer = new DataContractXML(xmlFolderPath: "../../../../DataPersistence/data"); +//dataMgr.Serializer = new DataContractJSON(jsonFolderPath: "../../../../DataPersistence/data"); -RecipeCollection rc = new RecipeCollection("All recipes", dataMgr.Data["recipes"].Cast().ToArray()); +//dataMgr.Export(rc[2], "C:\\Users\\alex6\\Downloads\\recipe2.xml"); +dataMgr.Import("C:\\Users\\alex6\\Downloads\\recipe2.xml"); + +RecipeCollection rc = new RecipeCollection("All recipes", dataMgr.Data[nameof(Recipe)].Cast().ToArray()); Recipe? ret = SearcherRecipe.ResearchOn(rc); Console.WriteLine(ret); -//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 index 524eaac..ef4eab8 100644 --- a/MCTG/DataPersistence/DataContractJSON.cs +++ b/MCTG/DataPersistence/DataContractJSON.cs @@ -28,6 +28,16 @@ namespace DataPersistence _dataContractJsonSerializerSettings = dataContractJsonSerializerSettings; } + public void Export(T obj, string pathToExport) where T : class + { + throw new NotImplementedException(); + } + + public KeyValuePair Import(string pathToImport) where T : class + { + throw new NotImplementedException(); + } + public Dictionary> Load() { Dictionary>? elements = new Dictionary>(); diff --git a/MCTG/DataPersistence/DataContractXML.cs b/MCTG/DataPersistence/DataContractXML.cs index 833919e..b805ec6 100644 --- a/MCTG/DataPersistence/DataContractXML.cs +++ b/MCTG/DataPersistence/DataContractXML.cs @@ -6,6 +6,7 @@ using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; using System.Xml; +using System.Xml.Linq; namespace DataPersistence { @@ -36,9 +37,42 @@ namespace DataPersistence _dataContractSerializerSettings = dataContractSerializerSettings; } + 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 = new Dictionary>(); + Dictionary>? elements; var serializer = new DataContractSerializer(typeof(Dictionary>), _dataContractSerializerSettings); using (Stream s = File.OpenRead(Path.Combine(_xmlFolderPath, "data.xml"))) { diff --git a/MCTG/DataPersistence/DataManager.cs b/MCTG/DataPersistence/DataManager.cs index 7520704..2ef82af 100644 --- a/MCTG/DataPersistence/DataManager.cs +++ b/MCTG/DataPersistence/DataManager.cs @@ -23,5 +23,16 @@ namespace DataPersistence public void Save() => Serializer.Save(Data); + + public void Import(string pathOfTheFile) + where T : class + { + KeyValuePair import = Serializer.Import(pathOfTheFile); + Data[import.Key].Add(import.Value); + } + + public void Export(T obj, string pathToExport) + where T : class + => Serializer.Export(obj, pathToExport); } } diff --git a/MCTG/DataPersistence/IDataManager.cs b/MCTG/DataPersistence/IDataManager.cs index dcfe514..49d8553 100644 --- a/MCTG/DataPersistence/IDataManager.cs +++ b/MCTG/DataPersistence/IDataManager.cs @@ -8,10 +8,13 @@ namespace DataPersistence { public interface IDataManager { - //void Save(List values); - //List Load(); - void Save(Dictionary> elements); Dictionary> Load(); + + public KeyValuePair Import(string pathToImport) + where T : class; + + public void Export(T obj, string pathToExport) + where T : class; } } diff --git a/MCTG/DataPersistence/Stubs.cs b/MCTG/DataPersistence/Stubs.cs index 3bbb239..89fe5b9 100644 --- a/MCTG/DataPersistence/Stubs.cs +++ b/MCTG/DataPersistence/Stubs.cs @@ -14,7 +14,7 @@ namespace DataPersistence Dictionary> data = new Dictionary> { { - "recipes", + nameof(Recipe), new List(new[] { new Recipe( @@ -89,7 +89,17 @@ namespace DataPersistence public void Save(Dictionary> elements) { - // Stubs dont' have to save. + throw new NotSupportedException(); + } + + public void Export(T obj, string pathToExport) where T : class + { + throw new NotSupportedException(); + } + + public KeyValuePair Import(string pathToImport) where T : class + { + throw new NotSupportedException(); } } } diff --git a/MCTG/DataPersistence/data/data.json b/MCTG/DataPersistence/data/data.json deleted file mode 100644 index 7b89454..0000000 --- a/MCTG/DataPersistence/data/data.json +++ /dev/null @@ -1,168 +0,0 @@ -[ - { - "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 index 731915c..bc21b40 100644 --- a/MCTG/DataPersistence/data/data.xml +++ b/MCTG/DataPersistence/data/data.xml @@ -1,10 +1,10 @@ - recipes - + Recipe + - 3005 + 9062 Faire cuire. @@ -18,7 +18,7 @@ Cookies classiques - 28377 + 27627 Moulinez la pâte. @@ -36,7 +36,7 @@ Cookies au chocolat - 10495 + 8517 Achetez les ingrédients. @@ -54,7 +54,7 @@ Gateau nature - 32282 + 6161 Achetez les légumes. @@ -76,7 +76,7 @@ Gateau au pommes - 26518 + 17869 Ajouter les oeufs. @@ -102,7 +102,7 @@ Gateau au chocolat - 21970 + 4810 Faire une cuisson bien sec de la dinde à la poêle @@ -128,7 +128,7 @@ Dinde au jambon - 12830 + 13570 Trouvez des épices de curry. @@ -157,6 +157,28 @@ Poulet au curry + + 26918 + + + Achetez les ingrédients. + 1 + + + Préparez le matériel. Ustensiles et tout. + 2 + + + Pleurez. + 3 + + + Aprenez de vos echecs. + 4 + + + Gateau nature v2 + \ No newline at end of file