using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Authorization; using vendorApproverBackend.Entities; using vendorApproverBackend.Services; using vendorApproverBackend.Requests; using vendorApproverBackend.Exceptions; namespace vendorApproverBackend.Controllers { [ApiController] [Route("api/v1/certification-packs")] public class CertificationPackController : ControllerBase { private readonly CertificationPackService _service; public CertificationPackController(CertificationPackService service) { _service = service; } [HttpPost("add")] [Authorize(Roles = "ADMIN,EMPLOYEE,VENDOR,FINANCIAL")] public async Task> AddPack([FromBody] AddCertificationPackRequest request) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var newPack = await _service.AddPackAsync(request); return CreatedAtAction(nameof(GetPackById), new { id = newPack.Id }, newPack); } [HttpGet("get_pack_by_id/{id}")] [Authorize(Roles = "ADMIN,EMPLOYEE,VENDOR,FINANCIAL")] public async Task> GetPackById(long id) { try { var pack = await _service.GetPackByIdAsync(id); return Ok(pack); } catch (NotFoundException ex) { return NotFound(ex.Message); } } [HttpGet("get_all_packs")] [Authorize(Roles = "ADMIN,EMPLOYEE,VENDOR,FINANCIAL")] public async Task>> GetAllPacks() { var packs = await _service.GetAllPacksAsync(); return Ok(packs); } [HttpPut("update_pack/{id}")] [Authorize(Roles = "ADMIN,EMPLOYEE,VENDOR,FINANCIAL")] public async Task> UpdatePack(long id, [FromBody] UpdateCertificationPackRequest request) { if (!ModelState.IsValid) { return BadRequest(ModelState); } try { var updatedPack = await _service.UpdatePackAsync(id, request); return Ok(updatedPack); } catch (NotFoundException ex) { return NotFound(ex.Message); } } [HttpDelete("delete_pack/{id}")] [Authorize(Roles = "ADMIN,EMPLOYEE,VENDOR,FINANCIAL")] public async Task DeletePack(long id) { try { await _service.DeletePackAsync(id); return NoContent(); } catch (NotFoundException ex) { return NotFound(ex.Message); } } } }