diff --git a/back/Contracts/Services/ISpendingGroupService.cs b/back/Contracts/Services/ISpendingGroupService.cs index e6664ee..938806f 100644 --- a/back/Contracts/Services/ISpendingGroupService.cs +++ b/back/Contracts/Services/ISpendingGroupService.cs @@ -7,7 +7,7 @@ namespace Contracts.Services; public interface ISpendingGroupService { Task GetDetails(SpendingGroupSearch search); - Task> GetList(SpendingGroupSearch search); + Task> GetList(SpendingGroupSearch? search = null); Task Create(SpendingGroupDto model); Task Update(SpendingGroupDto model); Task Delete(SpendingGroupSearch search); diff --git a/back/Controllers/Controllers/SpendingGroupController.cs b/back/Controllers/Controllers/SpendingGroupController.cs new file mode 100644 index 0000000..36ad45b --- /dev/null +++ b/back/Controllers/Controllers/SpendingGroupController.cs @@ -0,0 +1,118 @@ +namespace Controllers.Extensions; + +using Contracts.DTO; +using Contracts.SearchModels; +using Contracts.Services; +using Contracts.ViewModels; +using Microsoft.AspNetCore.Mvc; +using Services.Support.Exceptions; + +[ApiController] +[Route("api/[controller]")] +public class SpendingGroupController : ControllerBase +{ + private readonly ISpendingGroupService _spendingGroupService; + + public SpendingGroupController(ISpendingGroupService spendingGroupService) + { + _spendingGroupService = spendingGroupService; + } + [HttpGet("details")] + public async Task> GetSpendingGroup( + [FromQuery] SpendingGroupSearch search) + { + try + { + var group = await _spendingGroupService.GetDetails(search); + return Ok(group); + } + catch (EntityNotFoundException ex) + { + return NotFound(ex.Message); + } + catch (Exception ex) + { + return StatusCode(500, ex.Message); + } + } + + [HttpGet] + public async Task>> GetSpendingGroups() + { + try + { + var groups = await _spendingGroupService.GetList(); + return Ok(groups); + } + catch (Exception ex) + { + return StatusCode(500, ex.Message); + } + } + + [HttpGet("filter")] + public async Task>> GetSpendingGroups( + [FromQuery] SpendingGroupSearch search) + { + try + { + var groups = await _spendingGroupService.GetList(search); + return Ok(groups); + } + catch (Exception ex) + { + return StatusCode(500, ex.Message); + } + } + + [HttpPost] + public async Task> CreateSpendingGroup( + [FromBody] SpendingGroupDto dto) + { + try + { + var group = await _spendingGroupService.Create(dto); + return CreatedAtAction(nameof(GetSpendingGroup), new { id = group.Id }, group); + } + catch (Exception ex) + { + return StatusCode(500, ex.Message); + } + } + + [HttpPatch] + public async Task> UpdateSpendingGroup([FromBody] SpendingGroupDto dto) + { + try + { + var group = await _spendingGroupService.Update(dto); + return Ok(group); + } + catch (EntityNotFoundException ex) + { + return NotFound(ex.Message); + } + catch (Exception ex) + { + return StatusCode(500, ex.Message); + } + } + + [HttpDelete] + public async Task DeleteSpendingGroup([FromQuery] SpendingGroupSearch search) + { + try + { + var group = await _spendingGroupService.Delete(search); + return Ok(group); + } + catch (EntityNotFoundException ex) + { + return NotFound(ex.Message); + } + catch (Exception ex) + { + return StatusCode(500, ex.Message); + } + } +} \ No newline at end of file