diff --git a/API/ROLAC.API/Controllers/GivingsController.cs b/API/ROLAC.API/Controllers/GivingsController.cs new file mode 100644 index 0000000..78d3fcd --- /dev/null +++ b/API/ROLAC.API/Controllers/GivingsController.cs @@ -0,0 +1,52 @@ +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using ROLAC.API.DTOs.Giving; +using ROLAC.API.Services; + +namespace ROLAC.API.Controllers; + +[ApiController] +[Route("api/givings")] +[Authorize(Roles = "finance,super_admin")] +public class GivingsController : ControllerBase +{ + private readonly IGivingService _svc; + public GivingsController(IGivingService svc) => _svc = svc; + + [HttpGet] + public async Task GetPaged( + [FromQuery] int page = 1, [FromQuery] int pageSize = 20, + [FromQuery] string? search = null, [FromQuery] int? categoryId = null, + [FromQuery] DateOnly? from = null, [FromQuery] DateOnly? to = null) + => Ok(await _svc.GetPagedAsync(page, pageSize, search, categoryId, from, to)); + + [HttpGet("{id:int}")] + public async Task GetById(int id) + { + var dto = await _svc.GetByIdAsync(id); + return dto is null ? NotFound() : Ok(dto); + } + + [HttpPost] + public async Task Create([FromBody] CreateGivingRequest request) + { + var id = await _svc.CreateAsync(request); + return CreatedAtAction(nameof(GetById), new { id }, new { id }); + } + + [HttpPut("{id:int}")] + public async Task Update(int id, [FromBody] UpdateGivingRequest request) + { + try { await _svc.UpdateAsync(id, request); return NoContent(); } + catch (KeyNotFoundException) { return NotFound(); } + catch (InvalidOperationException ex) { return Conflict(new { message = ex.Message }); } + } + + [HttpDelete("{id:int}")] + public async Task Delete(int id) + { + try { await _svc.DeleteAsync(id); return NoContent(); } + catch (KeyNotFoundException) { return NotFound(); } + catch (InvalidOperationException ex) { return Conflict(new { message = ex.Message }); } + } +}