From 60b5c43571a407e36ba6d00e51ce69ddb0180e8c Mon Sep 17 00:00:00 2001 From: Louis DUFOUR Date: Wed, 22 Mar 2023 21:46:59 +0100 Subject: [PATCH] Update Manager --- Sources/EFManager/EFManager.csproj | 1 + Sources/EFManager/ManagerChampion.cs | 32 +++++++++++++++++++--------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/Sources/EFManager/EFManager.csproj b/Sources/EFManager/EFManager.csproj index d0ace33..9b550c2 100644 --- a/Sources/EFManager/EFManager.csproj +++ b/Sources/EFManager/EFManager.csproj @@ -9,6 +9,7 @@ + diff --git a/Sources/EFManager/ManagerChampion.cs b/Sources/EFManager/ManagerChampion.cs index 968e29f..6cb1aa7 100644 --- a/Sources/EFManager/ManagerChampion.cs +++ b/Sources/EFManager/ManagerChampion.cs @@ -2,6 +2,7 @@ using Microsoft.EntityFrameworkCore; using System.Linq; using Model; +using EFMapping; namespace EFManager { @@ -15,22 +16,33 @@ namespace EFManager public async Task AddItem(Champion? item) { - if (item == null) - return null; - - await parent.DbContext.Champions.AddAsync(item.toEF()); - await parent.DbContext.SaveChangesAsync(); + try + { + await parent.DbContext.Champions.AddAsync(item.toEF(parent.DbContext)); + parent.DbContext.SaveChangesAsync(); + } + catch (OperationCanceledException){} + catch(DbUpdateException){} return item; } public async Task DeleteItem(Champion? item) { - if (item == null) + try + { + var toDelete = parent.DbContext.Champions.Find(item.Name); + if (toDelete != null) + { + parent.DbContext.Champions.Remove(item.toEF(parent.DbContext)); + parent.DbContext.SaveChangesAsync(); + return true; + } return false; - - parent.DbContext.Champions.Remove(item.toEF()); - await parent.DbContext.SaveChangesAsync(); - return true; + } + catch (DbUpdateException) + { + return false; + } } public async Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false)