Files
DbFirst/DbFirst.API/Controllers/CatalogsController.cs
OlgunR a849a88fa3 Refactor Catalog update/upsert endpoints and service logic
- Update endpoint now returns CatalogReadDto or NotFound
- Replace UpdateWithStoredProcedure with Upsert endpoint (PUT /upsert)
- Rename AddAsync to InsertAsync; UpdateAsync returns DTO or null
- Introduce UpsertAsync for combined insert/update logic
- Remove stored procedure-specific update/delete methods
- Use DTOs consistently in controller/service responses
- Repository methods now return entities instead of booleans
2026-01-14 15:29:09 +01:00

75 lines
2.1 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<CatalogReadDto>>> GetAll(CancellationToken cancellationToken)
{
var result = await _service.GetAllAsync(cancellationToken);
return Ok(result);
}
[HttpGet("{id:int}")]
public async Task<ActionResult<CatalogReadDto>> 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<CatalogReadDto>> Create(CatalogWriteDto 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<ActionResult<CatalogReadDto>> Update(int id, CatalogWriteDto dto, CancellationToken cancellationToken)
{
var updated = await _service.UpdateAsync(id, dto, cancellationToken);
if (updated == null)
{
return NotFound();
}
return Ok(updated);
}
[HttpPut("upsert")]
public async Task<ActionResult<CatalogReadDto>> Upsert(CatalogWriteDto dto, CancellationToken cancellationToken)
{
var result = await _service.UpsertAsync(dto, cancellationToken);
if (result == null)
{
return BadRequest();
}
return Ok(result);
}
[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();
}
}