Move catalog title update validation to handler
Validation preventing CatTitle changes during updates is now enforced in UpdateCatalogHandler instead of the controller. The handler throws an exception if a title change is attempted. Also, streamlined UpdateProcedure handling and removed redundant CatTitle mapping logic.
This commit is contained in:
@@ -50,17 +50,6 @@ public class CatalogsController : ControllerBase
|
|||||||
[HttpPut("{id:int}")]
|
[HttpPut("{id:int}")]
|
||||||
public async Task<ActionResult<CatalogReadDto>> Update(int id, CatalogWriteDto dto, CancellationToken cancellationToken)
|
public async Task<ActionResult<CatalogReadDto>> Update(int id, CatalogWriteDto dto, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var current = await _mediator.Send(new GetCatalogByIdQuery(id), cancellationToken);
|
|
||||||
if (current == null)
|
|
||||||
{
|
|
||||||
return NotFound();
|
|
||||||
}
|
|
||||||
if (dto.UpdateProcedure == CatalogUpdateProcedure.Update &&
|
|
||||||
!string.Equals(current.CatTitle, dto.CatTitle, StringComparison.OrdinalIgnoreCase))
|
|
||||||
{
|
|
||||||
return BadRequest("Titel kann nicht geändert werden.");
|
|
||||||
}
|
|
||||||
|
|
||||||
var updated = await _mediator.Send(new UpdateCatalogCommand(id, dto), cancellationToken);
|
var updated = await _mediator.Send(new UpdateCatalogCommand(id, dto), cancellationToken);
|
||||||
if (updated == null)
|
if (updated == null)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -25,18 +25,20 @@ public class UpdateCatalogHandler : IRequestHandler<UpdateCatalogCommand, Catalo
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (request.Dto.UpdateProcedure == CatalogUpdateProcedure.Update &&
|
||||||
|
!string.Equals(existing.CatTitle, request.Dto.CatTitle, StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Titel kann nicht geändert werden.");
|
||||||
|
}
|
||||||
|
|
||||||
var entity = _mapper.Map<VwmyCatalog>(request.Dto);
|
var entity = _mapper.Map<VwmyCatalog>(request.Dto);
|
||||||
entity.Guid = request.Id;
|
entity.Guid = request.Id;
|
||||||
entity.CatTitle = request.Dto.UpdateProcedure == CatalogUpdateProcedure.Update
|
|
||||||
? existing.CatTitle
|
|
||||||
: request.Dto.CatTitle;
|
|
||||||
entity.AddedWho = existing.AddedWho;
|
entity.AddedWho = existing.AddedWho;
|
||||||
entity.AddedWhen = existing.AddedWhen;
|
entity.AddedWhen = existing.AddedWhen;
|
||||||
entity.ChangedWho = "system";
|
entity.ChangedWho = "system";
|
||||||
entity.ChangedWhen = DateTime.UtcNow;
|
entity.ChangedWhen = DateTime.UtcNow;
|
||||||
|
|
||||||
var procedure = request.Dto.UpdateProcedure;
|
var updated = await _repository.UpdateAsync(request.Id, entity, request.Dto.UpdateProcedure, cancellationToken);
|
||||||
var updated = await _repository.UpdateAsync(request.Id, entity, procedure, cancellationToken);
|
|
||||||
return updated == null ? null : _mapper.Map<CatalogReadDto>(updated);
|
return updated == null ? null : _mapper.Map<CatalogReadDto>(updated);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user