Added support for updating and deleting Catalog records via SQL Server stored procedures. Introduced PUT /catalogs/sp and DELETE /catalogs/sp/{id} endpoints in CatalogsController. Extended service and repository interfaces and implementations to handle stored procedure operations. Added Microsoft.Data.SqlClient package for direct SQL execution. Repository checks for record existence before SP calls to prevent unintended behavior.
86 lines
2.5 KiB
C#
86 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")]
|
|
public async Task<ActionResult<CatalogDto>> UpdateWithStoredProcedure(CatalogDto dto, CancellationToken cancellationToken)
|
|
{
|
|
var updated = await _service.UpdateWithStoredProcedureAsync(dto, cancellationToken);
|
|
if (updated == null)
|
|
{
|
|
return BadRequest();
|
|
}
|
|
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();
|
|
}
|
|
}
|