Refactor UpdateWithStoredProcedure to accept id from the route and set dto.Guid accordingly. In the repository, use Guid for existence checks instead of CAT_TITLE, and ensure CatTitle is not changed via the SP. Improve error handling to return NotFound when appropriate.
87 lines
2.5 KiB
C#
87 lines
2.5 KiB
C#
using DbFirst.Application.Catalogs;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace DbFirst.API.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class CatalogsController : ControllerBase
|
|
{
|
|
private readonly ICatalogService _service;
|
|
|
|
public CatalogsController(ICatalogService service)
|
|
{
|
|
_service = service;
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<ActionResult<IEnumerable<CatalogDto>>> GetAll(CancellationToken cancellationToken)
|
|
{
|
|
var result = await _service.GetAllAsync(cancellationToken);
|
|
return Ok(result);
|
|
}
|
|
|
|
[HttpGet("{id:int}")]
|
|
public async Task<ActionResult<CatalogDto>> GetById(int id, CancellationToken cancellationToken)
|
|
{
|
|
var result = await _service.GetByIdAsync(id, cancellationToken);
|
|
if (result == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
return Ok(result);
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<ActionResult<CatalogDto>> Create(CatalogDto dto, CancellationToken cancellationToken)
|
|
{
|
|
var created = await _service.CreateAsync(dto, cancellationToken);
|
|
return CreatedAtAction(nameof(GetById), new { id = created.Guid }, created);
|
|
}
|
|
|
|
[HttpPut("{id:int}")]
|
|
public async Task<IActionResult> Update(int id, CatalogDto dto, CancellationToken cancellationToken)
|
|
{
|
|
var updated = await _service.UpdateAsync(id, dto, cancellationToken);
|
|
if (!updated)
|
|
{
|
|
return NotFound();
|
|
}
|
|
return NoContent();
|
|
}
|
|
|
|
[HttpPut("sp/{id:int}")]
|
|
public async Task<ActionResult<CatalogDto>> UpdateWithStoredProcedure(int id, CatalogDto dto, CancellationToken cancellationToken)
|
|
{
|
|
dto.Guid = id;
|
|
var updated = await _service.UpdateWithStoredProcedureAsync(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 _service.DeleteAsync(id, cancellationToken);
|
|
if (!deleted)
|
|
{
|
|
return NotFound();
|
|
}
|
|
return NoContent();
|
|
}
|
|
|
|
[HttpDelete("sp/{id:int}")]
|
|
public async Task<IActionResult> DeleteWithStoredProcedure(int id, CancellationToken cancellationToken)
|
|
{
|
|
var deleted = await _service.DeleteWithStoredProcedureAsync(id, cancellationToken);
|
|
if (!deleted)
|
|
{
|
|
return NotFound();
|
|
}
|
|
return NoContent();
|
|
}
|
|
}
|