From bbaae88fb08f982c634f1af117a4488c9c73fa88 Mon Sep 17 00:00:00 2001 From: Arafamamadouelaphi <92931011+Arafamamadouelaphi@users.noreply.github.com> Date: Mon, 24 Oct 2022 14:11:13 +0100 Subject: [PATCH] AsyncAwait #63 --- Sources/BowlingMaping/EquipeDbDataManager.cs | 27 +++++++++--------- Sources/BowlingMaping/JoueurDbDataManager.cs | 30 +++++++++----------- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/Sources/BowlingMaping/EquipeDbDataManager.cs b/Sources/BowlingMaping/EquipeDbDataManager.cs index 63f737e..ce00454 100644 --- a/Sources/BowlingMaping/EquipeDbDataManager.cs +++ b/Sources/BowlingMaping/EquipeDbDataManager.cs @@ -3,6 +3,7 @@ using BowlingEF.Context; using BowlingEF.Entities; using BowlingLib.Model; using Business; +using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; @@ -14,7 +15,7 @@ namespace BowlingMaping public class EquipeDbDataManager : IDataManager { #region Méthodes - public bool Add(Equipe _equipe) + public async Task Add(Equipe _equipe) { bool result = false; using (var context = new BowlingContext()) @@ -30,62 +31,62 @@ namespace BowlingMaping }).ToList() }; context.Equipes.Add(entity); - result = context.SaveChanges() == 1; + result = await context.SaveChangesAsync() == 1; } return result; } - public bool Delete(Equipe _equipe) + public async Task 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; + result =await context.SaveChangesAsync() == 1; } return result; } - public IEnumerable GetAll() + public async Task> GetAll() { using (var context = new BowlingContext()) { - return context.Equipes.Select(e => new Equipe + return await context.Equipes.Select(e => new Equipe ( e.Id, e.Nom, e.Joueurs.Select(j => new Joueur(j.Id, j.Pseudo)).ToArray() - )).ToList(); + )).ToListAsync(); } } - public Equipe GetDataWithName(string name) + public async Task GetDataWithName(string name) { using (var context = new BowlingContext()) { - return context.Equipes.Where(e => e.Nom == name).Select(e => new Equipe + return await 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)).ToArray() - )).FirstOrDefault(); + )).FirstOrDefaultAsync(); } } - public bool Update(Equipe data) + public async Task< bool> Update(Equipe data) { bool result = false; using (var context = new BowlingContext()) { - EquipeEntity entity = context.Equipes.Find(data.Id); + 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; + result = await context.SaveChangesAsync() == 1; } return result; } diff --git a/Sources/BowlingMaping/JoueurDbDataManager.cs b/Sources/BowlingMaping/JoueurDbDataManager.cs index fea1f31..fc90c5c 100644 --- a/Sources/BowlingMaping/JoueurDbDataManager.cs +++ b/Sources/BowlingMaping/JoueurDbDataManager.cs @@ -2,6 +2,7 @@ using BowlingEF.Entities; using BowlingLib.Model; using Business; +using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; @@ -21,7 +22,7 @@ namespace BowlingMaping /// /// /// - public bool Add(Joueur _joueur) + public async Task Add(Joueur _joueur) { bool result = false; using (var context = new BowlingContext()) @@ -32,7 +33,7 @@ namespace BowlingMaping Pseudo = _joueur.Pseudo, }; context.Joueurs.Add(entity); - result = context.SaveChanges() == 1; + result =await context.SaveChangesAsync() == 1; } return result; } @@ -42,7 +43,7 @@ namespace BowlingMaping /// /// /// - public bool Delete(Joueur _joueur) + public async Task< bool> Delete(Joueur _joueur) { bool result = false; @@ -52,7 +53,7 @@ namespace BowlingMaping { JoueurEntity entity = context.Joueurs.Find(_joueur.Id); context.Joueurs.Remove(entity); - result = context.SaveChanges() == 1; + result = await context.SaveChangesAsync() == 1; } return result; @@ -64,12 +65,12 @@ namespace BowlingMaping /// recupère tous les joueurs de la Base de données /// /// - public IEnumerable GetAll() + public async Task< IEnumerable >GetAll() { using (var context = new BowlingContext()) { - List joueurs = new List(); - foreach (var item in context.Joueurs) + List joueurs = new List(); + foreach (var item in await context.Joueurs.ToListAsync()) joueurs.Add(new Joueur(item.Id, item.Pseudo)); return joueurs; } @@ -80,22 +81,19 @@ namespace BowlingMaping /// /// /// - public Joueur GetDataWithName(string name) + public async Task 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); + + var query = await context.Joueurs.FirstOrDefaultAsync(n => n.Pseudo == n.Pseudo); + _joueur = new Joueur(query.Id, query.Pseudo); return _joueur; } } - public bool Update(Joueur _joueur) + public async Task Update(Joueur _joueur) { bool result = false; using (var context = new BowlingContext()) @@ -104,7 +102,7 @@ namespace BowlingMaping if (entity!=null) { entity.Pseudo = _joueur.Pseudo; - result = context.SaveChanges() == 1; + result = await context.SaveChangesAsync() == 1; } } return result;