première version de la persistance

pull/26/head
Vianney JOURDY 2 years ago
parent 10e304ba4a
commit cf42e8ed39

@ -1,4 +1,5 @@
using MangaMap.Model;
using MangaMap.Stub;
using MangaMap.Views;
namespace MangaMap;
@ -11,6 +12,9 @@ public partial class App : Application
public App()
{
MyManager.charger();
MyManager.Persistance = new DataContract();
MyManager.sauvegarder();
InitializeComponent();
MyManager.Admins.Add(MyAdmin);

@ -1,20 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace MangaMap.Model
{
[DataContract]
public class Oeuvre
{
[DataMember]
public string Nom { get; private set; }
[DataMember]
public List <string> Genre { get; private set; }
[DataMember]
public string Type { get; private set; }
[DataMember]
public string Description { get; private set; }
[DataMember]
public int Note { get; private set; }
[DataMember]
public int NbEpisodes { get; private set; }
public string Affiche { get; private set; }
[DataMember]
public string Affiche { get; private set; } //Comment enregistrer l'image avec la persistance ?
public Oeuvre(string nom, List<string> genre, string type, string description, int note, int nbEpisode, string affiche)
{

@ -0,0 +1,57 @@
using MangaMap.Model;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace MangaMap.Stub
{
public class DataContract : IPersistanceManager
{
public string FilePath2 { get; set; } = Path.Combine(Directory.GetCurrentDirectory(),"//Dossier1_XML");
public string FilePath { get; set; } = "C:\\Users\\vjour\\UCA\\MapManga\\MangaMap";
public string FileName { get; set; } = "Test1.xml";
public (List<Oeuvre>, List<Utilisateur>) chargeDonne()
{
var serializer = new DataContractSerializer(typeof(List<Oeuvre>));
List<Oeuvre> liste;
using (Stream s = File.OpenRead(Path.Combine(FilePath, FileName)))
{
liste = serializer.ReadObject(s) as List<Oeuvre>;
}
return (liste, new List<Utilisateur>());
}
public void sauvegarder(List<Oeuvre> o, List<Utilisateur> u)
{
var serializer = new DataContractSerializer(typeof(List<Oeuvre>));
if(!Directory.Exists(FilePath))
{
Debug.WriteLine("Directory doesn't exist.");
Directory.CreateDirectory(FilePath);
}
/*using (Stream s = File.Create(Path.Combine(FilePath, FileName)))
{
serializer.WriteObject(s, o);
}*/
var settings = new XmlWriterSettings() { Indent = true };
using (TextWriter tw = File.CreateText(Path.Combine(FilePath, FileName)))
{
using (XmlWriter w = XmlWriter.Create(tw, settings))
{
serializer.WriteObject(w, o); //data to persist
}
}
}
}
}

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfOeuvre xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/MangaMap.Model">
<Oeuvre>
<Affiche>test.jpg</Affiche>
<Description>C'est une bonne série</Description>
<Genre xmlns:d3p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<d3p1:string>Action</d3p1:string>
<d3p1:string>Future</d3p1:string>
</Genre>
<NbEpisodes>150</NbEpisodes>
<Nom>test</Nom>
<Note>4</Note>
<Type>TV</Type>
</Oeuvre>
<Oeuvre>
<Affiche>test2.png</Affiche>
<Description>A la fin il meurt</Description>
<Genre xmlns:d3p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<d3p1:string>Action</d3p1:string>
<d3p1:string>Future</d3p1:string>
</Genre>
<NbEpisodes>24</NbEpisodes>
<Nom>test2</Nom>
<Note>2</Note>
<Type>DVD</Type>
</Oeuvre>
</ArrayOfOeuvre>
Loading…
Cancel
Save