From 4c2417f27124cca79fc12d9fd82c2f59badf08af Mon Sep 17 00:00:00 2001 From: Corentin LEMAIRE Date: Sat, 13 May 2023 15:51:11 +0200 Subject: [PATCH] Finish the Serialization development --- Sources/Model/IDataManager.cs | 20 ++ .../Serialization/LINQ_XML_Serialization.cs | 184 ++++++++++++++---- Sources/Model/Stub/StubManager.cs | 120 ++++++++++++ 3 files changed, 282 insertions(+), 42 deletions(-) diff --git a/Sources/Model/IDataManager.cs b/Sources/Model/IDataManager.cs index 257e0b9..e0907ce 100644 --- a/Sources/Model/IDataManager.cs +++ b/Sources/Model/IDataManager.cs @@ -100,4 +100,24 @@ public interface IDataManager void SaveSerialization(); + // Exists + bool ExistsPlaylist(Playlist playlist); + + bool ExistsPlaylistByUrl(string url); + + bool ExistsAlbum(Album album); + + bool ExistsAlbumByUrl(string url); + + bool ExistsArtist(Artist artist); + + bool ExistsArtistByName(string name); + + bool ExistsCustomTitle(CustomTitle title); + + bool ExistsCustomTitleByUrl(string url); + + bool ExistsInfoTitle(InfoTitle title); + + bool ExistsInfoTitleByUrl(string url); } \ No newline at end of file diff --git a/Sources/Model/Serialization/LINQ_XML_Serialization.cs b/Sources/Model/Serialization/LINQ_XML_Serialization.cs index b82a511..c8c3b3f 100644 --- a/Sources/Model/Serialization/LINQ_XML_Serialization.cs +++ b/Sources/Model/Serialization/LINQ_XML_Serialization.cs @@ -13,17 +13,17 @@ namespace Model.Serialization; public class LINQ_XML_Serialization : IDataManager { - private static string XMLPATH = @"Data\"; + private static string XMLPATH = Path.Combine(Environment.CurrentDirectory, "Data"); - private static string XMLFILEPLAYLISTS = "playlists.xml"; + private static string XMLFILEPLAYLISTS = Path.Combine(XMLPATH, "playlists.xml"); - private static string XMLFILEALBUMS = "albums.xml"; + private static string XMLFILEALBUMS = Path.Combine(XMLPATH, "albums.xml"); - private static string XMLFILECUSTOMS = "customs.xml"; + private static string XMLFILECUSTOMS = Path.Combine(XMLPATH, "customs.xml"); - private static string XMLFILEINFOS = "infos.xml"; + private static string XMLFILEINFOS = Path.Combine(XMLPATH, "infos.xml"); - private static string XMLFILEARTISTS = "artists.xml"; + private static string XMLFILEARTISTS = Path.Combine(XMLPATH, "artists.xml"); private List artists; @@ -185,11 +185,7 @@ public class LINQ_XML_Serialization : IDataManager public void LoadPlaylists() { - if(!Directory.Exists(XMLPATH)) - { - Directory.CreateDirectory(XMLPATH); - } - if (!File.Exists(Path.Combine(XMLPATH + XMLFILEPLAYLISTS))) + if (!File.Exists(XMLFILEPLAYLISTS)) { XDocument PlaylistFile = new XDocument(); Playlist p1 = new Playlist(); @@ -206,7 +202,7 @@ public class LINQ_XML_Serialization : IDataManager XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; - using (TextWriter tw = File.CreateText(Path.Combine(XMLPATH + XMLFILEPLAYLISTS))) + using (TextWriter tw = File.CreateText(XMLFILEPLAYLISTS)) { using (XmlWriter writer = XmlWriter.Create(tw, settings)) { @@ -214,7 +210,7 @@ public class LINQ_XML_Serialization : IDataManager } } } - XDocument PlaylistsFichier = XDocument.Load(Path.Combine(XMLPATH, XMLFILEPLAYLISTS)); + XDocument PlaylistsFichier = XDocument.Load(XMLFILEPLAYLISTS); playlists = PlaylistsFichier.Descendants("Playlist") .Select(eltPlaylist => new Playlist( @@ -266,14 +262,10 @@ public class LINQ_XML_Serialization : IDataManager } } } - + public void LoadArtists() { - if (!Directory.Exists(XMLPATH)) - { - Directory.CreateDirectory(XMLPATH); - } - if (!File.Exists(Path.Combine(XMLPATH + XMLFILEARTISTS))) + if (!File.Exists(XMLFILEARTISTS)) { XDocument ArtistFile = new XDocument(); Artist a1 = new Artist("a1"); @@ -290,7 +282,7 @@ public class LINQ_XML_Serialization : IDataManager XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; - using (TextWriter tw = File.CreateText(Path.Combine(XMLPATH + XMLFILEARTISTS))) + using (TextWriter tw = File.CreateText(XMLFILEARTISTS)) { using (XmlWriter writer = XmlWriter.Create(tw, settings)) { @@ -299,7 +291,7 @@ public class LINQ_XML_Serialization : IDataManager } } - XDocument ArtistsFile = XDocument.Load(Path.Combine(XMLPATH, XMLFILEARTISTS)); + XDocument ArtistsFile = XDocument.Load(XMLFILEARTISTS); artists = ArtistsFile.Descendants("Artist") .Select(eltArtist => new Artist( eltArtist.Attribute("Name")!.Value @@ -331,11 +323,7 @@ public class LINQ_XML_Serialization : IDataManager public void LoadCustomTitles() { - if (!Directory.Exists(XMLPATH)) - { - Directory.CreateDirectory(XMLPATH); - } - if (!File.Exists(Path.Combine(XMLPATH + XMLFILECUSTOMS))) + if (!File.Exists(XMLFILECUSTOMS)) { XDocument CustomsFile2 = new XDocument(); @@ -356,14 +344,14 @@ public class LINQ_XML_Serialization : IDataManager XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; - using (TextWriter tw = File.CreateText(Path.Combine(XMLPATH + XMLFILECUSTOMS))) + using (TextWriter tw = File.CreateText(XMLFILECUSTOMS)) { using (XmlWriter writer = XmlWriter.Create(tw, settings)) CustomsFile2.Save(writer); } } - XDocument CustomsFile = XDocument.Load(Path.Combine(XMLPATH, XMLFILECUSTOMS)); + XDocument CustomsFile = XDocument.Load(XMLFILECUSTOMS); customTitles = CustomsFile.Descendants("CustomTitle") .Select(eltPlaylist => new CustomTitle( @@ -401,11 +389,7 @@ public class LINQ_XML_Serialization : IDataManager public void LoadAlbums() { - if (!Directory.Exists(XMLPATH)) - { - Directory.CreateDirectory(XMLPATH); - } - if (!File.Exists(Path.Combine(XMLPATH + XMLFILEALBUMS))) + if (!File.Exists(XMLFILEALBUMS)) { XDocument AlbumFile = new XDocument(); Album a1 = new Album(); @@ -424,7 +408,7 @@ public class LINQ_XML_Serialization : IDataManager XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; - using (TextWriter tw = File.CreateText(Path.Combine(XMLPATH + XMLFILEALBUMS))) + using (TextWriter tw = File.CreateText(XMLFILEALBUMS)) { using (XmlWriter writer = XmlWriter.Create(tw, settings)) { @@ -432,7 +416,7 @@ public class LINQ_XML_Serialization : IDataManager } } } - XDocument AlbumsFile = XDocument.Load(Path.Combine(XMLPATH, XMLFILEALBUMS)); + XDocument AlbumsFile = XDocument.Load(XMLFILEALBUMS); albums = AlbumsFile.Descendants("Album") .Select(eltPlaylist => new Album( @@ -492,11 +476,7 @@ public class LINQ_XML_Serialization : IDataManager public void LoadInfoTitles() { - if (!Directory.Exists(XMLPATH)) - { - Directory.CreateDirectory(XMLPATH); - } - if (!File.Exists(Path.Combine(XMLPATH + XMLFILEINFOS))) + if (!File.Exists(XMLFILEINFOS)) { XDocument InfoFile = new XDocument(); InfoTitle it1 = new InfoTitle(); @@ -515,7 +495,7 @@ public class LINQ_XML_Serialization : IDataManager XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; - using (TextWriter tw = File.CreateText(Path.Combine(XMLPATH + XMLFILEINFOS))) + using (TextWriter tw = File.CreateText(XMLFILEINFOS)) { using (XmlWriter writer = XmlWriter.Create(tw, settings)) { @@ -523,7 +503,7 @@ public class LINQ_XML_Serialization : IDataManager } } } - XDocument InfosFile = XDocument.Load(Path.Combine(XMLPATH, XMLFILEINFOS)); + XDocument InfosFile = XDocument.Load(XMLFILEINFOS); infoTitles = InfosFile.Descendants("InfoTitle") .Select(eltPlaylist => new InfoTitle( @@ -901,4 +881,124 @@ public class LINQ_XML_Serialization : IDataManager infoTitles.Remove(infoTitle); } } + + public bool ExistsPlaylist(Playlist playlist) + { + foreach(Playlist p in playlists) + { + if (p == playlist) + { + return true; + } + } + return false; + } + + public bool ExistsPlaylistByUrl(string url) + { + foreach(Playlist p in playlists) + { + if (p.ImageURL == url) + { + return true; + } + } + return false; + } + + public bool ExistsAlbum(Album album) + { + foreach(Album a in albums) + { + if(a == album) + { + return true; + } + } + return false; + } + + public bool ExistsAlbumByUrl(string url) + { + foreach(Album a in albums) + { + if (a.ImageURL == url) + { + return true; + } + } + return false; + } + + public bool ExistsArtist(Artist artist) + { + foreach (Artist a in artists) + { + if (a == artist) + { + return true; + } + } + return false; + } + + public bool ExistsArtistByName(string name) + { + foreach(Artist a in artists) + { + if(a.Name == name) + { + return true; + } + } + return false; + } + + public bool ExistsCustomTitle(CustomTitle title) + { + foreach(CustomTitle ct in customTitles) + { + if(title == ct) + { + return true; + } + } + return false; + } + + public bool ExistsCustomTitleByUrl(string url) + { + foreach(CustomTitle ct in customTitles) + { + if(ct.ImageURL == url) + { + return true; + } + } + return false; + } + + public bool ExistsInfoTitle(InfoTitle title) + { + foreach(InfoTitle it in infoTitles) + { + if(it == title) + { + return true; + } + } + return false; + } + + public bool ExistsInfoTitleByUrl(string url) + { + foreach(InfoTitle it in infoTitles) + { + if(it.ImageURL == url) + { + return true; + } + } + return false; + } } diff --git a/Sources/Model/Stub/StubManager.cs b/Sources/Model/Stub/StubManager.cs index 9405d37..7b48b03 100644 --- a/Sources/Model/Stub/StubManager.cs +++ b/Sources/Model/Stub/StubManager.cs @@ -433,4 +433,124 @@ public class StubManager : IDataManager StubInfoTitle.RemoveInfoTitle(infoTitle); } } + + public bool ExistsPlaylist(Playlist playlist) + { + foreach (Playlist p in StubPlaylist.Playlists) + { + if (p == playlist) + { + return true; + } + } + return false; + } + + public bool ExistsPlaylistByUrl(string url) + { + foreach (Playlist p in StubPlaylist.Playlists) + { + if (p.ImageURL == url) + { + return true; + } + } + return false; + } + + public bool ExistsAlbum(Album album) + { + foreach (Album a in StubAlbum.Albums) + { + if (a == album) + { + return true; + } + } + return false; + } + + public bool ExistsAlbumByUrl(string url) + { + foreach (Album a in StubAlbum.Albums) + { + if (a.ImageURL == url) + { + return true; + } + } + return false; + } + + public bool ExistsArtist(Artist artist) + { + foreach (Artist a in StubArtist.Artists) + { + if (a == artist) + { + return true; + } + } + return false; + } + + public bool ExistsArtistByName(string name) + { + foreach (Artist a in StubArtist.Artists) + { + if (a.Name == name) + { + return true; + } + } + return false; + } + + public bool ExistsCustomTitle(CustomTitle title) + { + foreach (CustomTitle ct in StubCustomTitle.CustomTitles) + { + if (title == ct) + { + return true; + } + } + return false; + } + + public bool ExistsCustomTitleByUrl(string url) + { + foreach (CustomTitle ct in StubCustomTitle.CustomTitles) + { + if (ct.ImageURL == url) + { + return true; + } + } + return false; + } + + public bool ExistsInfoTitle(InfoTitle title) + { + foreach (InfoTitle it in StubInfoTitle.InfoTitles) + { + if (it == title) + { + return true; + } + } + return false; + } + + public bool ExistsInfoTitleByUrl(string url) + { + foreach (InfoTitle it in StubInfoTitle.InfoTitles) + { + if (it.ImageURL == url) + { + return true; + } + } + return false; + } }