From 69a3e4a327586cd11fa04550ca2a1c87b719be42 Mon Sep 17 00:00:00 2001 From: victor perez ngounou Date: Sat, 15 Oct 2022 14:41:08 +0200 Subject: [PATCH] Final EF #40 --- Sources/BowlingEF/Entities/FrameEntity.cs | 4 + Sources/BowlingEF/Entities/PartieEntity.cs | 9 +- Sources/BowlingLib/Model/Equipe.cs | 2 +- Sources/BowlingLib/Model/Frame.cs | 10 +- Sources/BowlingLib/Model/Joueur.cs | 2 +- Sources/BowlingLib/Model/Partie.cs | 18 ++- Sources/BowlingMaping/DbDataManager.cs | 38 ----- Sources/BowlingMaping/EquipeDbDataManager.cs | 91 ++++++++++++ Sources/BowlingMaping/JoueurDbDataManager.cs | 101 +++++++++++++ Sources/BowlingMaping/PartieDbDataManager.cs | 147 +++++++++++++++++++ Sources/BowlingStub/StubEquipe.cs | 15 +- Sources/Business/IDataManager.cs | 2 - Sources/Business/IPartieDbDataManager.cs | 14 ++ Sources/Business/Manager.cs | 18 --- 14 files changed, 398 insertions(+), 73 deletions(-) delete mode 100644 Sources/BowlingMaping/DbDataManager.cs create mode 100644 Sources/BowlingMaping/EquipeDbDataManager.cs create mode 100644 Sources/BowlingMaping/JoueurDbDataManager.cs create mode 100644 Sources/BowlingMaping/PartieDbDataManager.cs create mode 100644 Sources/Business/IPartieDbDataManager.cs diff --git a/Sources/BowlingEF/Entities/FrameEntity.cs b/Sources/BowlingEF/Entities/FrameEntity.cs index 2d45d46..7345301 100644 --- a/Sources/BowlingEF/Entities/FrameEntity.cs +++ b/Sources/BowlingEF/Entities/FrameEntity.cs @@ -22,6 +22,10 @@ namespace BowlingEF.Entities public bool IsStrike { get; set; } [Required] public bool IsSpare { get; set; } + + [ForeignKey("PartieId")] + [Required] + public long PartieId { get; set; } public PartieEntity Partie { get; set; } } } \ No newline at end of file diff --git a/Sources/BowlingEF/Entities/PartieEntity.cs b/Sources/BowlingEF/Entities/PartieEntity.cs index 4c7e4f1..20fc896 100644 --- a/Sources/BowlingEF/Entities/PartieEntity.cs +++ b/Sources/BowlingEF/Entities/PartieEntity.cs @@ -17,8 +17,15 @@ namespace BowlingEF.Entities [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public long Id { get; set; } public JoueurEntity Joueur { get; set; } + + [ForeignKey("JoueurId")] + [Required] + public long JoueurId { get; set; } + [Required] + public DateTime Date { get; set; } public ICollection Frames { get; set; } - public int Score { get; set; } + [Required] + public int? Score { get; set; } public PartieEntity() { diff --git a/Sources/BowlingLib/Model/Equipe.cs b/Sources/BowlingLib/Model/Equipe.cs index 4bc907c..e22a31a 100644 --- a/Sources/BowlingLib/Model/Equipe.cs +++ b/Sources/BowlingLib/Model/Equipe.cs @@ -44,7 +44,7 @@ namespace BowlingLib.Model this.nom = nom; } - public Equipe(string nom, long id, List joueurs, int numero) + public Equipe(long id, string nom, List joueurs) { this.id = id; Joueurs = joueurs; diff --git a/Sources/BowlingLib/Model/Frame.cs b/Sources/BowlingLib/Model/Frame.cs index d740431..ff00887 100644 --- a/Sources/BowlingLib/Model/Frame.cs +++ b/Sources/BowlingLib/Model/Frame.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -142,15 +143,14 @@ namespace BowlingLib.Model /// /// /// - public Frame(int numero, long id, bool isStrike, bool isSpare, bool isPark, bool isFinished, Lancer lancer1, Lancer lancer2, Lancer? lancer3) : this(numero) + public Frame(long id, int numero, bool isStrike, bool isSpare, int lancer1, int lancer2, [AllowNull] int lancer3) : this(numero) { this.id = id; IsStrike = isStrike; IsSpare = isSpare; - IsFinished = isFinished; - Lancer1 = lancer1; - Lancer2 = lancer2; - Lancer3 = lancer3; + Lancer1 = new Lancer(lancer1); + Lancer2 = new Lancer(lancer2); + Lancer3 = new Lancer(lancer3); } /// diff --git a/Sources/BowlingLib/Model/Joueur.cs b/Sources/BowlingLib/Model/Joueur.cs index 6f7ef91..11fef50 100644 --- a/Sources/BowlingLib/Model/Joueur.cs +++ b/Sources/BowlingLib/Model/Joueur.cs @@ -20,7 +20,7 @@ namespace BowlingLib.Model this.Pseudo = pseudo; } - public Joueur(string pseudo, long id) : this(pseudo) + public Joueur(long id,string pseudo) : this(pseudo) { this.id = id; } diff --git a/Sources/BowlingLib/Model/Partie.cs b/Sources/BowlingLib/Model/Partie.cs index eefedf4..d602ed3 100644 --- a/Sources/BowlingLib/Model/Partie.cs +++ b/Sources/BowlingLib/Model/Partie.cs @@ -12,6 +12,19 @@ namespace BowlingLib.Model public ReadOnlyCollection Frames { get; } public Joueur Joueur { get; private set; } private readonly long id; + private DateTime date; + public int? Score { + get + { + return GetScore(); + } + private set { } + } + public DateTime Date + { + get { return date; } + private set { date = value; } + } public long Id => id; private readonly List frames=new(); @@ -23,13 +36,16 @@ namespace BowlingLib.Model public Partie(Joueur joueur) { this.Joueur = joueur; + Frames = new ReadOnlyCollection(frames); } - public Partie(Joueur joueur, long id, List frames) : this(joueur) + public Partie(long id, Joueur joueur, List frames,DateTime date, int? score) : this(joueur) { this.id = id; this.frames = frames; + this.date = date; + this.Score = score; } /// diff --git a/Sources/BowlingMaping/DbDataManager.cs b/Sources/BowlingMaping/DbDataManager.cs deleted file mode 100644 index a3641cd..0000000 --- a/Sources/BowlingMaping/DbDataManager.cs +++ /dev/null @@ -1,38 +0,0 @@ -using BowlingEF.Context; -using Business; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace BowlingMaping -{ - //public class DbDataManager : IDataManager - //{ - // private readonly BowlingContext context; - - // public DbDataManager(BowlingContext context) - // { - // this.context = context; - // } - - // public void Add(Manager data) - // { - // context.Add(data); - // context.SaveChanges(); - // } - - // public void Delete(Manager data) - // { - // context.Remove(data); - // context.SaveChanges(); - // } - - // public void Update(Manager data) - // { - // context.Update(data); - // context.SaveChanges(); - // } - //} -} diff --git a/Sources/BowlingMaping/EquipeDbDataManager.cs b/Sources/BowlingMaping/EquipeDbDataManager.cs new file mode 100644 index 0000000..b1f583d --- /dev/null +++ b/Sources/BowlingMaping/EquipeDbDataManager.cs @@ -0,0 +1,91 @@ +using BowlingEF.Context; +using BowlingEF.Entities; +using BowlingLib.Model; +using Business; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BowlingMaping +{ + public class EquipeDbDataManager : IDataManager + { + public bool Add(Equipe _equipe) + { + bool result = false; + using (var context = new BowlingContext()) + { + EquipeEntity entity = new EquipeEntity + { + Id = _equipe.Id, + Nom = _equipe.Nom, + Joueurs = _equipe.Joueurs.Select(j => new JoueurEntity + { + Id = j.Id, + Pseudo = j.Pseudo + }).ToList() + }; + context.Equipes.Add(entity); + result = context.SaveChanges() == 1; + } + return result; + } + + public bool Delete(Equipe _equipe) + { + bool result = false; + using (var context = new BowlingContext()) + { + EquipeEntity entity = context.Equipes.Find(_equipe.Id); + context.Equipes.Remove(entity); + result = context.SaveChanges() == 1; + } + return result; + } + + public IEnumerable GetAll() + { + using (var context = new BowlingContext()) + { + return context.Equipes.Select(e => new Equipe + ( + e.Id, + e.Nom, + e.Joueurs.Select(j => new Joueur(j.Id, j.Pseudo)).ToList() + )).ToList(); + } + } + + public Equipe GetDataWithName(string name) + { + using (var context = new BowlingContext()) + { + return context.Equipes.Where(e => e.Nom == name).Select(e => new Equipe + ( + e.Id, + e.Nom, + e.Joueurs.Select(j => new Joueur(j.Id, j.Pseudo)).ToList() + )).FirstOrDefault(); + } + } + + public bool Update(Equipe data) + { + bool result = false; + using (var context = new BowlingContext()) + { + EquipeEntity entity = context.Equipes.Find(data.Id); + entity.Nom = data.Nom; + entity.Joueurs = data.Joueurs.Select(j => new JoueurEntity + { + Id = j.Id, + Pseudo = j.Pseudo + }).ToList(); + result = context.SaveChanges() == 1; + } + return result; + } + } +} diff --git a/Sources/BowlingMaping/JoueurDbDataManager.cs b/Sources/BowlingMaping/JoueurDbDataManager.cs new file mode 100644 index 0000000..6c041b0 --- /dev/null +++ b/Sources/BowlingMaping/JoueurDbDataManager.cs @@ -0,0 +1,101 @@ +using BowlingEF.Context; +using BowlingEF.Entities; +using BowlingLib.Model; +using Business; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BowlingMaping +{ + public class JoueurDbDataManager : IDataManager + { + /// + /// Ajoute un joueur à la liste des joueurs + /// + /// + /// + public bool Add(Joueur _joueur) + { + bool result = false; + using (var context = new BowlingContext()) + { + JoueurEntity entity=new JoueurEntity + { + Id = _joueur.Id, + Pseudo = _joueur.Pseudo, + }; + context.Joueurs.Add(entity); + result = context.SaveChanges() == 1; + } + return result; + } + + /// + /// Supprime un joueur de la liste des joueurs + /// + /// + /// + public bool Delete(Joueur _joueur) + { + bool result = false; + using (var context = new BowlingContext()) + { + JoueurEntity entity = context.Joueurs.Find(_joueur.Id); + context.Joueurs.Remove(entity); + result = context.SaveChanges() == 1; + } + return result; + } + + /// + /// recupère tous les joueurs de la Base de données + /// + /// + public IEnumerable GetAll() + { + using (var context = new BowlingContext()) + { + List joueurs = new List(); + foreach (var item in context.Joueurs) + joueurs.Add(new Joueur(item.Id, item.Pseudo)); + return joueurs; + } + } + + /// + /// recupère un joueur de la Base de données par son pseudo + /// + /// + /// + public Joueur GetDataWithName(string name) + { + using (var context = new BowlingContext()) + { + Joueur _joueur = null; + + var query = from joueur in context.Joueurs + where joueur.Pseudo == name + select joueur; + foreach (var item in query) + _joueur = new Joueur(item.Id, item.Pseudo); + return _joueur; + } + } + + public bool Update(Joueur _joueur) + { + bool result = false; + using (var context = new BowlingContext()) + { + JoueurEntity entity = context.Joueurs?.Find(_joueur.Id); + entity.Pseudo = _joueur.Pseudo; + result = context.SaveChanges() == 1; + } + return result; + } + + } +} diff --git a/Sources/BowlingMaping/PartieDbDataManager.cs b/Sources/BowlingMaping/PartieDbDataManager.cs new file mode 100644 index 0000000..b9e0d11 --- /dev/null +++ b/Sources/BowlingMaping/PartieDbDataManager.cs @@ -0,0 +1,147 @@ +using BowlingEF.Context; +using BowlingEF.Entities; +using BowlingLib.Model; +using Business; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BowlingMaping +{ + public class PartieDbDataManager : IPartieDbDataManager + { + + /// + /// Ajoute une partie Dans la base de données + /// + /// + /// + public bool Add(Partie _partie) + { + bool result = false; + using (var context = new BowlingContext()) + { + PartieEntity entity = new PartieEntity + { + Id = _partie.Id, + Date = _partie.Date, + JoueurId = _partie.Joueur.Id, + Score = _partie.Score + }; + context.Parties.Add(entity); + result = context.SaveChanges() == 1; + } + return result; + } + + /// + /// Supprime une partie de la base de données + /// + /// + /// + public bool Delete(Partie _partie) + { + bool result = false; + using (var context = new BowlingContext()) + { + PartieEntity entity = context.Parties.Find(_partie.Id); + context.Parties.Remove(entity); + result = context.SaveChanges() == 1; + } + return result; + } + + /// + /// Modifie une partie dans la base de données + /// + /// + /// + public bool Update(Partie _partie) + { + bool result = false; + using (var context = new BowlingContext()) + { + PartieEntity entity = context.Parties.Find(_partie.Id); + entity.Date = _partie.Date; + entity.JoueurId = _partie.Joueur.Id; + entity.Score = _partie.Score; + result = context.SaveChanges() == 1; + } + return result; + } + + /// + /// Retourne une partie de la base de données en fonction de son nom + /// + /// + /// + public Partie GetDataWithName(string name) + { + Partie result = null; + using (var context = new BowlingContext()) + { + PartieEntity entity = context.Parties.Find(name); + Joueur joueur = new Joueur(entity.Joueur.Id,entity.Joueur.Pseudo); + List frames = new List(); + foreach (FrameEntity frameEntity in entity.Frames) + { + Frame frame = new Frame(frameEntity.Id, frameEntity.Numero, frameEntity.IsStrike, frameEntity.IsSpare,frameEntity.Lancer1,frameEntity.Lancer2,frameEntity.Lancer3); + frames.Add(frame); + } + result = new Partie(entity.Id, joueur,frames, entity.Date, entity.Score); + } + return result; + } + + /// + /// Retourne Toutes les parties en base de donné + /// + /// + public IEnumerable GetAll() + { + List result = new List(); + using (var context = new BowlingContext()) + { + foreach (PartieEntity entity in context.Parties.OrderBy(item=>item.Date)) + { + Joueur joueur = new Joueur(entity.Joueur.Id, entity.Joueur.Pseudo); + List frames = new List(); + foreach (FrameEntity frameEntity in entity.Frames) + { + Frame frame = new Frame(frameEntity.Id, frameEntity.Numero, frameEntity.IsStrike, frameEntity.IsSpare, frameEntity.Lancer1, frameEntity.Lancer2, frameEntity.Lancer3); + frames.Add(frame); + } + result.Add(new Partie(entity.Id, joueur, frames, entity.Date, entity.Score)); + } + } + return result; + } + + + public IEnumerable GetAllWithDate(DateTime date) + { + List result = new List(); + using (var context = new BowlingContext()) + { + foreach (PartieEntity entity in context.Parties.OrderBy(item=>item.Date)) + { + if (entity.Date == date) + { + Joueur joueur = new Joueur(entity.Joueur.Id, entity.Joueur.Pseudo); + List frames = new List(); + foreach (FrameEntity frameEntity in entity.Frames) + { + Frame frame = new Frame(frameEntity.Id, frameEntity.Numero, frameEntity.IsStrike, frameEntity.IsSpare, frameEntity.Lancer1, frameEntity.Lancer2, frameEntity.Lancer3); + frames.Add(frame); + } + result.Add(new Partie(entity.Id, joueur, frames, entity.Date, entity.Score)); + } + } + } + return result; + } + + } +} diff --git a/Sources/BowlingStub/StubEquipe.cs b/Sources/BowlingStub/StubEquipe.cs index a820696..bbfd5b5 100644 --- a/Sources/BowlingStub/StubEquipe.cs +++ b/Sources/BowlingStub/StubEquipe.cs @@ -7,6 +7,7 @@ namespace BowlingStub public class StubEquipe : IDataManager { private List listEquipes = new List(); + public int nbrJ = 10,nbrE = 2; public StubEquipe() { //listEquipes.Add(new Equipe("Equipe 1", new Joueur("Joueur 1"), new Joueur("Joueur 2"))); @@ -34,23 +35,24 @@ namespace BowlingStub return false; } - public IEnumerable GetAll(int n = 10, int j = 2) + public void Load() { - for (int i = 0; i < n; i++) + for (int i = 0; i < nbrJ; i++) { this.Add(new Equipe("Equipe " + i + 1)); - for (int k = 0; k < j; k++) + for (int k = 0; k < nbrE; k++) { listEquipes.ElementAt(i).AjouterJoueur(new Joueur("Joueur " + i + 1 + "-" + k + 1)); } } - return listEquipes; } + public IEnumerable GetAll() { + Load(); return listEquipes; } @@ -68,13 +70,14 @@ namespace BowlingStub } + - public Equipe GetDataWithId(int id) + public Equipe GetDataWithName(string name) { throw new NotImplementedException(); } - public Equipe GetDataWithName(string name) + public IEnumerable GetAllWithDate(DateTime date) { throw new NotImplementedException(); } diff --git a/Sources/Business/IDataManager.cs b/Sources/Business/IDataManager.cs index 3ceeae9..6e41d26 100644 --- a/Sources/Business/IDataManager.cs +++ b/Sources/Business/IDataManager.cs @@ -12,9 +12,7 @@ namespace Business bool Add(Data data); bool Delete(Data data); bool Update(Data data); - Data GetDataWithId(int id); Data GetDataWithName(string name); IEnumerable GetAll(); - IEnumerable GetAll(int n, int j); } } diff --git a/Sources/Business/IPartieDbDataManager.cs b/Sources/Business/IPartieDbDataManager.cs new file mode 100644 index 0000000..6d8fd07 --- /dev/null +++ b/Sources/Business/IPartieDbDataManager.cs @@ -0,0 +1,14 @@ +using BowlingLib.Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Business +{ + public interface IPartieDbDataManager : IDataManager + { + IEnumerable GetAllWithDate(DateTime date); + } +} diff --git a/Sources/Business/Manager.cs b/Sources/Business/Manager.cs index 7de4c4f..71c819c 100644 --- a/Sources/Business/Manager.cs +++ b/Sources/Business/Manager.cs @@ -192,23 +192,5 @@ namespace Business return equipeDataManager.GetAll(); } - //retourne le joueur avec l'id - public Joueur GetJoueurWithId(int id) - { - return JoueurDataManager.GetDataWithId(id); - } - - //retourne la partie avec l'id - public Partie GetPartieWithId(int id) - { - return partieDataManager.GetDataWithId(id); - } - - //retourne l'équipe avec l'id - public Equipe GetEquipeWithId(int id) - { - return equipeDataManager.GetDataWithId(id); - } - } }