using Microsoft.AspNetCore.Mvc; using vendorApproverBackend.Entities; using vendorApproverBackend.Services; using vendorApproverBackend.Requests; using vendorApproverBackend.Exceptions; using Microsoft.AspNetCore.Authorization; namespace vendorApproverBackend.Controllers { [ApiController] [Route("api/v1/app-categories")] public class ApplicationCategoryController : ControllerBase { private readonly ApplicationCategoryService _service; public ApplicationCategoryController(ApplicationCategoryService service) { _service = service; } [HttpPost("add_app_category")] [Authorize(Roles = "ADMIN,EMPLOYEE,VENDOR,FINANCIAL,PERFERMANCE_MANAGER")] public async Task> AddAppCategory([FromBody] AddApplicationCategoryRequest request) { if (!ModelState.IsValid) return BadRequest(ModelState); try { var newAppCategory = await _service.AddApplicationCategoryAsync(request); return CreatedAtAction(nameof(GetAppCategoryById), new { id = newAppCategory.Id }, newAppCategory); } catch (BadRequestException ex) { return BadRequest(ex.Message); } } [HttpGet("get_app_category_by_id/{id}")] [Authorize(Roles = "ADMIN,EMPLOYEE,VENDOR,FINANCIAL,PERFERMANCE_MANAGER")] public async Task> GetAppCategoryById(long id) { try { var appCategory = await _service.FindApplicationCategoryByIdAsync(id); return Ok(appCategory); } catch (NotFoundException ex) { return NotFound(ex.Message); } } [HttpGet("get_all_app_categories")] [Authorize(Roles = "ADMIN,EMPLOYEE,VENDOR,FINANCIAL,PERFERMANCE_MANAGER")] public async Task>> GetAllAppCategories() { var appCategories = await _service.FetchAllApplicationCategoryAsync(); return Ok(appCategories); } [HttpPatch("update_app_category/{id}")] [Authorize(Roles = "ADMIN,EMPLOYEE,VENDOR,FINANCIAL,PERFERMANCE_MANAGER")] public async Task> UpdateAppCategory(long id, [FromBody] UpdateApplicationCategoryRequest request) { if (!ModelState.IsValid) return BadRequest(ModelState); try { var updatedAppCategory = await _service.UpdateApplicationCategoryAsync(id, request); return Ok(updatedAppCategory); } catch (NotFoundException ex) { return NotFound(ex.Message); } catch (BadRequestException ex) { return BadRequest(ex.Message); } } [HttpDelete("/delete_app_category{id}")] [Authorize(Roles = "ADMIN,EMPLOYEE,VENDOR,FINANCIAL,PERFERMANCE_MANAGER")] public async Task DeleteAppCategory(long id) { try { await _service.DeleteApplicationCategoryAsync(id); return NoContent(); // 204 No Content } catch (NotFoundException ex) { return NotFound(ex.Message); } } [HttpPost("add_category_to_app_categories/{appCategoryId}/categories")] [Authorize(Roles = "ADMIN,EMPLOYEE,VENDOR,FINANCIAL,PERFERMANCE_MANAGER")] public async Task> AddCategoryToAppCategory(long appCategoryId, [FromBody] CategoryRequest request) { if (!ModelState.IsValid) return BadRequest(ModelState); try { var newCategory = await _service.AddCategoriesToAppCategoryAsync(appCategoryId, request); return CreatedAtAction(nameof(GetCategoryById), new { id = newCategory.Id }, newCategory); } catch (NotFoundException ex) { return NotFound(ex.Message); } } [HttpGet("get_categories_by_id/{id}")] [Authorize(Roles = "ADMIN,EMPLOYEE,VENDOR,FINANCIAL,PERFERMANCE_MANAGER")] public async Task> GetCategoryById(long id) { try { var category = await _service.FindCategoryByIdAsync(id); return Ok(category); } catch (NotFoundException ex) { return NotFound(ex.Message); } } [HttpPatch("update_categories/{id}")] [Authorize(Roles = "ADMIN,EMPLOYEE,VENDOR,FINANCIAL,PERFERMANCE_MANAGER")] public async Task> UpdateCategory(long id, [FromBody] UpdateCategoryByIdRequest request) { if (!ModelState.IsValid) return BadRequest(ModelState); try { var updatedCategory = await _service.UpdateCategoryAsync(id, request); return Ok(updatedCategory); } catch (NotFoundException ex) { return NotFound(ex.Message); } } [HttpDelete("delete_categories/{id}")] [Authorize(Roles = "ADMIN,EMPLOYEE,VENDOR,FINANCIAL,PERFERMANCE_MANAGER")] public async Task DeleteCategory(long id) { try { await _service.DeleteCategoryAsync(id); return NoContent(); } catch (NotFoundException ex) { return NotFound(ex.Message); } } } }