diff --git a/back/Contracts/Mappers/SpendingPlanMapper.cs b/back/Contracts/Mappers/SpendingPlanMapper.cs index 4e4e144..84504b9 100644 --- a/back/Contracts/Mappers/SpendingPlanMapper.cs +++ b/back/Contracts/Mappers/SpendingPlanMapper.cs @@ -5,7 +5,7 @@ namespace Contracts.Mappers; public static class SpendingPlanMapper { - public static SpendingPlanView ToView(this SpendingPlanDto dto) + public static SpendingPlanViewModel ToView(this SpendingPlanDto dto) => new() { Id = dto.Id, diff --git a/back/Contracts/Services/ISpendingPlanService.cs b/back/Contracts/Services/ISpendingPlanService.cs index b2ee0aa..086ec5b 100644 --- a/back/Contracts/Services/ISpendingPlanService.cs +++ b/back/Contracts/Services/ISpendingPlanService.cs @@ -6,9 +6,9 @@ namespace Contracts.Services; public interface ISpendingPlanService { - Task GetDetails(SpendingPlanSearch search); - Task> GetList(SpendingPlanSearch? search = null); - Task Create(SpendingPlanDto spendingPlan); - Task Delete(SpendingPlanSearch search); - Task Update(SpendingPlanDto spendingPlan); + Task GetDetails(SpendingPlanSearch search); + Task> GetList(SpendingPlanSearch? search = null); + Task Create(SpendingPlanDto spendingPlan); + Task Delete(SpendingPlanSearch search); + Task Update(SpendingPlanDto spendingPlan); } \ No newline at end of file diff --git a/back/Contracts/ViewModels/SpendingPlanView.cs b/back/Contracts/ViewModels/SpendingPlanViewModel.cs similarity index 83% rename from back/Contracts/ViewModels/SpendingPlanView.cs rename to back/Contracts/ViewModels/SpendingPlanViewModel.cs index 74ed725..285cdd8 100644 --- a/back/Contracts/ViewModels/SpendingPlanView.cs +++ b/back/Contracts/ViewModels/SpendingPlanViewModel.cs @@ -1,6 +1,6 @@ namespace Contracts.ViewModels; -public class SpendingPlanView +public class SpendingPlanViewModel { public Guid Id { get; set; } public DateTime StartAt { get; set; } diff --git a/back/Controllers/Controllers/SpendingPlanController.cs b/back/Controllers/Controllers/SpendingPlanController.cs new file mode 100644 index 0000000..5f457b1 --- /dev/null +++ b/back/Controllers/Controllers/SpendingPlanController.cs @@ -0,0 +1,128 @@ +using Contracts.DTO; +using Contracts.SearchModels; +using Contracts.Services; +using Contracts.ViewModels; +using Microsoft.AspNetCore.Mvc; +using Services.Support.Exceptions; + +namespace Controllers.Controllers; + +[ApiController] +[Route("api/[controller]")] +public class SpendingPlanController : ControllerBase +{ + private readonly ISpendingPlanService _spendingPlanService; + + public SpendingPlanController(ISpendingPlanService spendingPlanService) + { + _spendingPlanService = spendingPlanService; + } + + [HttpGet("{id}")] + public async Task> GetSpendingPlan( + Guid id, + [FromQuery] SpendingPlanSearch search) + { + try + { + search ??= new(); + search.Id = id; + + var plan = await _spendingPlanService.GetDetails(search); + return Ok(plan); + } + catch (EntityNotFoundException ex) + { + return NotFound(ex.Message); + } + catch (Exception ex) + { + return StatusCode(500, ex.Message); + } + } + + [HttpGet] + public async Task>> GetSpendingPlans() + { + try + { + var plans = await _spendingPlanService.GetList(); + return Ok(plans); + } + catch (Exception ex) + { + return StatusCode(500, ex.Message); + } + } + + [HttpGet("filter")] + public async Task>> GetSpendingPlans( + [FromQuery] SpendingPlanSearch search) + { + try + { + var plans = await _spendingPlanService.GetList(search); + return Ok(plans); + } + catch (Exception ex) + { + return StatusCode(500, ex.Message); + } + } + + [HttpPost] + public async Task> CreateSpendingPlan( + [FromBody] SpendingPlanDto dto) + { + try + { + var plan = await _spendingPlanService.Create(dto); + return CreatedAtAction(nameof(GetSpendingPlan), new { id = plan.Id }, plan); + } + catch (EntityNotFoundException ex) + { + return NotFound(ex.Message); + } + catch (Exception ex) + { + return StatusCode(500, ex.Message); + } + } + + [HttpPatch] + public async Task> UpdateSpendingPlan( + [FromBody] SpendingPlanDto dto) + { + try + { + var plan = await _spendingPlanService.Update(dto); + return Ok(plan); + } + catch (EntityNotFoundException ex) + { + return NotFound(ex.Message); + } + catch (Exception ex) + { + return StatusCode(500, ex.Message); + } + } + + [HttpDelete] + public async Task DeleteSpendingPlan([FromQuery] SpendingPlanSearch search) + { + try + { + var plan = await _spendingPlanService.Delete(search); + return Ok(plan); + } + catch (EntityNotFoundException ex) + { + return NotFound(ex.Message); + } + catch (Exception ex) + { + return StatusCode(500, ex.Message); + } + } +} \ No newline at end of file diff --git a/back/Controllers/Extensions/AddDomainServicesExt.cs b/back/Controllers/Extensions/AddDomainServicesExt.cs index 97d9065..7d6cc0a 100644 --- a/back/Controllers/Extensions/AddDomainServicesExt.cs +++ b/back/Controllers/Extensions/AddDomainServicesExt.cs @@ -13,5 +13,7 @@ public static class AddDomainServicesExtension services.AddTransient(); services.AddTransient(); + + services.AddTransient(); } } \ No newline at end of file diff --git a/back/Controllers/Extensions/AddReposExt.cs b/back/Controllers/Extensions/AddReposExt.cs index 9793b6e..5da333a 100644 --- a/back/Controllers/Extensions/AddReposExt.cs +++ b/back/Controllers/Extensions/AddReposExt.cs @@ -12,5 +12,6 @@ public static class AddReposExtension services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } \ No newline at end of file diff --git a/back/Services/Domain/SpendingPlanService.cs b/back/Services/Domain/SpendingPlanService.cs index d565869..ff655d0 100644 --- a/back/Services/Domain/SpendingPlanService.cs +++ b/back/Services/Domain/SpendingPlanService.cs @@ -17,13 +17,13 @@ public class SpendingPlanService : ISpendingPlanService _spendingPlanRepo = spendingPlanRepo; } - public async Task Create(SpendingPlanDto spendingPlan) + public async Task Create(SpendingPlanDto spendingPlan) { var plan = await _spendingPlanRepo.Create(spendingPlan); return plan.ToView(); } - public async Task Delete(SpendingPlanSearch search) + public async Task Delete(SpendingPlanSearch search) { var plan = await _spendingPlanRepo.Delete(search); if (plan == null) @@ -33,7 +33,7 @@ public class SpendingPlanService : ISpendingPlanService return plan.ToView(); } - public async Task GetDetails(SpendingPlanSearch search) + public async Task GetDetails(SpendingPlanSearch search) { var plan = await _spendingPlanRepo.Get(search); if (plan == null) @@ -43,13 +43,13 @@ public class SpendingPlanService : ISpendingPlanService return plan.ToView(); } - public async Task> GetList(SpendingPlanSearch? search = null) + public async Task> GetList(SpendingPlanSearch? search = null) { var plans = await _spendingPlanRepo.GetList(search); return plans.Select(x => x.ToView()).ToList(); } - public async Task Update(SpendingPlanDto spendingPlan) + public async Task Update(SpendingPlanDto spendingPlan) { var plan = await _spendingPlanRepo.Update(spendingPlan); if (plan == null)