add: контроллер плана расходов

This commit is contained in:
mfnefd 2024-11-27 02:20:17 +04:00
parent 0bd6261e21
commit 6ca4576a6b
7 changed files with 143 additions and 12 deletions

View File

@ -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,

View File

@ -6,9 +6,9 @@ namespace Contracts.Services;
public interface ISpendingPlanService
{
Task<SpendingPlanView> GetDetails(SpendingPlanSearch search);
Task<IEnumerable<SpendingPlanView>> GetList(SpendingPlanSearch? search = null);
Task<SpendingPlanView> Create(SpendingPlanDto spendingPlan);
Task<SpendingPlanView> Delete(SpendingPlanSearch search);
Task<SpendingPlanView> Update(SpendingPlanDto spendingPlan);
Task<SpendingPlanViewModel> GetDetails(SpendingPlanSearch search);
Task<IEnumerable<SpendingPlanViewModel>> GetList(SpendingPlanSearch? search = null);
Task<SpendingPlanViewModel> Create(SpendingPlanDto spendingPlan);
Task<SpendingPlanViewModel> Delete(SpendingPlanSearch search);
Task<SpendingPlanViewModel> Update(SpendingPlanDto spendingPlan);
}

View File

@ -1,6 +1,6 @@
namespace Contracts.ViewModels;
public class SpendingPlanView
public class SpendingPlanViewModel
{
public Guid Id { get; set; }
public DateTime StartAt { get; set; }

View File

@ -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<ActionResult<SpendingPlanViewModel>> 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<ActionResult<List<SpendingPlanViewModel>>> GetSpendingPlans()
{
try
{
var plans = await _spendingPlanService.GetList();
return Ok(plans);
}
catch (Exception ex)
{
return StatusCode(500, ex.Message);
}
}
[HttpGet("filter")]
public async Task<ActionResult<List<SpendingPlanViewModel>>> 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<ActionResult<SpendingPlanViewModel>> 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<ActionResult<SpendingPlanViewModel>> 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<ActionResult> 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);
}
}
}

View File

@ -13,5 +13,7 @@ public static class AddDomainServicesExtension
services.AddTransient<ISpendingGroupService, SpendingGroupService>();
services.AddTransient<IChangeRecordService, ChangeRecordService>();
services.AddTransient<ISpendingPlanService, SpendingPlanService>();
}
}

View File

@ -12,5 +12,6 @@ public static class AddReposExtension
services.AddTransient<IUserRepo, UserRepo>();
services.AddTransient<ISpendingGroupRepo, SpendingGroupRepo>();
services.AddTransient<IChangeRecordRepo, ChangeRecordRepo>();
services.AddTransient<ISpendingPlanRepo, SpendingPlanRepo>();
}
}

View File

@ -17,13 +17,13 @@ public class SpendingPlanService : ISpendingPlanService
_spendingPlanRepo = spendingPlanRepo;
}
public async Task<SpendingPlanView> Create(SpendingPlanDto spendingPlan)
public async Task<SpendingPlanViewModel> Create(SpendingPlanDto spendingPlan)
{
var plan = await _spendingPlanRepo.Create(spendingPlan);
return plan.ToView();
}
public async Task<SpendingPlanView> Delete(SpendingPlanSearch search)
public async Task<SpendingPlanViewModel> 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<SpendingPlanView> GetDetails(SpendingPlanSearch search)
public async Task<SpendingPlanViewModel> 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<IEnumerable<SpendingPlanView>> GetList(SpendingPlanSearch? search = null)
public async Task<IEnumerable<SpendingPlanViewModel>> GetList(SpendingPlanSearch? search = null)
{
var plans = await _spendingPlanRepo.GetList(search);
return plans.Select(x => x.ToView()).ToList();
}
public async Task<SpendingPlanView> Update(SpendingPlanDto spendingPlan)
public async Task<SpendingPlanViewModel> Update(SpendingPlanDto spendingPlan)
{
var plan = await _spendingPlanRepo.Update(spendingPlan);
if (plan == null)