From e6849cd9c9f33b8c89ddbc2618e7400b37020627 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Wed, 11 Sep 2024 09:43:01 +0200 Subject: [PATCH] =?UTF-8?q?Fix:=20=C3=9Cberpr=C3=BCfung=20hinzugef=C3=BCgt?= =?UTF-8?q?,=20ob=20die=20Entit=C3=A4t=20in=20UpdateAsync=20vorhanden=20is?= =?UTF-8?q?t?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Eine Überprüfung hinzugefügt, um sicherzustellen, dass die Entität vor dem Aktualisieren existiert. - Eine Warnung wird protokolliert, wenn die Entität im Aktualisierungsprozess nicht gefunden wird. - Das `updateDto` wird auf die bestehende Entität gemappt, anstatt eine neue zu erstellen. --- DigitalData.Core.Application/CRUDService.cs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/DigitalData.Core.Application/CRUDService.cs b/DigitalData.Core.Application/CRUDService.cs index c4ddd0a..6c7f8fb 100644 --- a/DigitalData.Core.Application/CRUDService.cs +++ b/DigitalData.Core.Application/CRUDService.cs @@ -5,6 +5,7 @@ using System.Reflection; using System.ComponentModel.DataAnnotations; using DigitalData.Core.DTO; using DigitalData.Core.Abstractions; +using Microsoft.Extensions.Logging; namespace DigitalData.Core.Application { @@ -80,9 +81,16 @@ namespace DigitalData.Core.Application /// A service message indicating success or failure. public virtual async Task UpdateAsync(TUpdateDto updateDto) { - var entity = _mapper.MapOrThrow(updateDto); - bool isUpdated = await _repository.UpdateAsync(entity); - return isUpdated ? Result.Success() : Result.Fail(); + var currentEntitiy = await _repository.ReadByIdAsync(updateDto.Id); + + if (currentEntitiy is null) + return Result.Fail().Notice(LogLevel.Warning, Flag.NotFound, $"{updateDto.Id} is not found in update process of {GetType()} entity."); + + var entity = _mapper.Map(updateDto, currentEntitiy); + + return await _repository.UpdateAsync(entity) + ? Result.Success() + : Result.Fail(); } ///