using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using ROLAC.API.DTOs.Giving; using ROLAC.API.Services; namespace ROLAC.API.Controllers; [ApiController] [Route("api/offering-sessions")] [Authorize(Roles = "finance,super_admin")] public class OfferingSessionsController : ControllerBase { private readonly IOfferingSessionService _svc; public OfferingSessionsController(IOfferingSessionService svc) => _svc = svc; [HttpGet] public async Task GetPaged( [FromQuery] int page = 1, [FromQuery] int pageSize = 20, [FromQuery] DateOnly? from = null, [FromQuery] DateOnly? to = null) => Ok(await _svc.GetPagedAsync(page, pageSize, from, to)); [HttpGet("check-date")] public async Task CheckDate([FromQuery] DateOnly date) => Ok(new { exists = await _svc.DateExistsAsync(date) }); [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] CreateOfferingSessionRequest request) { try { var id = await _svc.CreateAsync(request); return CreatedAtAction(nameof(GetById), new { id }, new { id }); } catch (InvalidOperationException ex) { return Conflict(new { message = ex.Message }); } } [HttpPost("{id:int}/reopen")] public async Task Reopen(int id) { try { await _svc.ReopenAsync(id); return NoContent(); } catch (KeyNotFoundException) { return NotFound(); } catch (InvalidOperationException ex) { return Conflict(new { message = ex.Message }); } } [HttpPut("{id:int}")] public async Task Replace(int id, [FromBody] CreateOfferingSessionRequest request) { try { await _svc.ReplaceAsync(id, request); return NoContent(); } catch (KeyNotFoundException) { return NotFound(); } catch (InvalidOperationException ex) { return Conflict(new { message = ex.Message }); } } }