Enforce business rules for catalog title changes: only allow CatTitle to be edited when UpdateProcedure permits, with checks in the API, service, handler, and UI. This ensures consistent validation and user experience across backend and frontend.
83 lines
2.5 KiB
C#
83 lines
2.5 KiB
C#
using DbFirst.Application.Catalogs;
|
|
using DbFirst.Application.Catalogs.Commands;
|
|
using DbFirst.Application.Catalogs.Queries;
|
|
using DbFirst.Domain;
|
|
using MediatR;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace DbFirst.API.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class CatalogsController : ControllerBase
|
|
{
|
|
private readonly IMediator _mediator;
|
|
|
|
public CatalogsController(IMediator mediator)
|
|
{
|
|
_mediator = mediator;
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<ActionResult<IEnumerable<CatalogReadDto>>> GetAll(CancellationToken cancellationToken)
|
|
{
|
|
var result = await _mediator.Send(new GetAllCatalogsQuery(), cancellationToken);
|
|
return Ok(result);
|
|
}
|
|
|
|
[HttpGet("{id:int}")]
|
|
public async Task<ActionResult<CatalogReadDto>> GetById(int id, CancellationToken cancellationToken)
|
|
{
|
|
var result = await _mediator.Send(new GetCatalogByIdQuery(id), cancellationToken);
|
|
if (result == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
return Ok(result);
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<ActionResult<CatalogReadDto>> Create(CatalogWriteDto dto, CancellationToken cancellationToken)
|
|
{
|
|
var created = await _mediator.Send(new CreateCatalogCommand(dto), cancellationToken);
|
|
if (created == null)
|
|
{
|
|
return Conflict();
|
|
}
|
|
return CreatedAtAction(nameof(GetById), new { id = created.Guid }, created);
|
|
}
|
|
|
|
[HttpPut("{id:int}")]
|
|
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);
|
|
if (updated == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
return Ok(updated);
|
|
}
|
|
|
|
[HttpDelete("{id:int}")]
|
|
public async Task<IActionResult> Delete(int id, CancellationToken cancellationToken)
|
|
{
|
|
var deleted = await _mediator.Send(new DeleteCatalogCommand(id), cancellationToken);
|
|
if (!deleted)
|
|
{
|
|
return NotFound();
|
|
}
|
|
return NoContent();
|
|
}
|
|
}
|