Fix: Überprüfung hinzugefügt, ob die Entität in UpdateAsync vorhanden ist
- 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.
This commit is contained in:
parent
d59350174c
commit
e6849cd9c9
@ -5,6 +5,7 @@ using System.Reflection;
|
|||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
using DigitalData.Core.DTO;
|
using DigitalData.Core.DTO;
|
||||||
using DigitalData.Core.Abstractions;
|
using DigitalData.Core.Abstractions;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace DigitalData.Core.Application
|
namespace DigitalData.Core.Application
|
||||||
{
|
{
|
||||||
@ -80,9 +81,16 @@ namespace DigitalData.Core.Application
|
|||||||
/// <returns>A service message indicating success or failure.</returns>
|
/// <returns>A service message indicating success or failure.</returns>
|
||||||
public virtual async Task<Result> UpdateAsync(TUpdateDto updateDto)
|
public virtual async Task<Result> UpdateAsync(TUpdateDto updateDto)
|
||||||
{
|
{
|
||||||
var entity = _mapper.MapOrThrow<TEntity>(updateDto);
|
var currentEntitiy = await _repository.ReadByIdAsync(updateDto.Id);
|
||||||
bool isUpdated = await _repository.UpdateAsync(entity);
|
|
||||||
return isUpdated ? Result.Success() : Result.Fail();
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user