From d8423da7206775c05eeeae1d6cb7f3468772b1c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Strau=C3=9F?= Date: Thu, 15 Aug 2024 19:16:25 +0200 Subject: [PATCH] Added DeleteById in Repository. This deletes without any checks. --- src/DotNetElements.Core/Core/Repository.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/DotNetElements.Core/Core/Repository.cs b/src/DotNetElements.Core/Core/Repository.cs index c0ce282..8fecdb1 100644 --- a/src/DotNetElements.Core/Core/Repository.cs +++ b/src/DotNetElements.Core/Core/Repository.cs @@ -169,6 +169,21 @@ public virtual async Task DeleteAsync(TEntityToDele return CrudResult.Ok(); } + public async Task DeleteByIdAsync(TKey id) + { + TEntity? entityToDelete = await Entities.FirstOrDefaultAsync(WithId(id)); + + if (entityToDelete is null) + return CrudResult.NotFound(id); + + Entities.Remove(entityToDelete); + + await DbContext.SaveChangesAsync(); + + return CrudResult.Ok(); + } + + public virtual async Task ClearTable() { await Entities.ExecuteDeleteAsync();