add: контроллер плана расходов
This commit is contained in:
parent
0bd6261e21
commit
6ca4576a6b
@ -5,7 +5,7 @@ namespace Contracts.Mappers;
|
|||||||
|
|
||||||
public static class SpendingPlanMapper
|
public static class SpendingPlanMapper
|
||||||
{
|
{
|
||||||
public static SpendingPlanView ToView(this SpendingPlanDto dto)
|
public static SpendingPlanViewModel ToView(this SpendingPlanDto dto)
|
||||||
=> new()
|
=> new()
|
||||||
{
|
{
|
||||||
Id = dto.Id,
|
Id = dto.Id,
|
||||||
|
@ -6,9 +6,9 @@ namespace Contracts.Services;
|
|||||||
|
|
||||||
public interface ISpendingPlanService
|
public interface ISpendingPlanService
|
||||||
{
|
{
|
||||||
Task<SpendingPlanView> GetDetails(SpendingPlanSearch search);
|
Task<SpendingPlanViewModel> GetDetails(SpendingPlanSearch search);
|
||||||
Task<IEnumerable<SpendingPlanView>> GetList(SpendingPlanSearch? search = null);
|
Task<IEnumerable<SpendingPlanViewModel>> GetList(SpendingPlanSearch? search = null);
|
||||||
Task<SpendingPlanView> Create(SpendingPlanDto spendingPlan);
|
Task<SpendingPlanViewModel> Create(SpendingPlanDto spendingPlan);
|
||||||
Task<SpendingPlanView> Delete(SpendingPlanSearch search);
|
Task<SpendingPlanViewModel> Delete(SpendingPlanSearch search);
|
||||||
Task<SpendingPlanView> Update(SpendingPlanDto spendingPlan);
|
Task<SpendingPlanViewModel> Update(SpendingPlanDto spendingPlan);
|
||||||
}
|
}
|
@ -1,6 +1,6 @@
|
|||||||
namespace Contracts.ViewModels;
|
namespace Contracts.ViewModels;
|
||||||
|
|
||||||
public class SpendingPlanView
|
public class SpendingPlanViewModel
|
||||||
{
|
{
|
||||||
public Guid Id { get; set; }
|
public Guid Id { get; set; }
|
||||||
public DateTime StartAt { get; set; }
|
public DateTime StartAt { get; set; }
|
128
back/Controllers/Controllers/SpendingPlanController.cs
Normal file
128
back/Controllers/Controllers/SpendingPlanController.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -13,5 +13,7 @@ public static class AddDomainServicesExtension
|
|||||||
services.AddTransient<ISpendingGroupService, SpendingGroupService>();
|
services.AddTransient<ISpendingGroupService, SpendingGroupService>();
|
||||||
|
|
||||||
services.AddTransient<IChangeRecordService, ChangeRecordService>();
|
services.AddTransient<IChangeRecordService, ChangeRecordService>();
|
||||||
|
|
||||||
|
services.AddTransient<ISpendingPlanService, SpendingPlanService>();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -12,5 +12,6 @@ public static class AddReposExtension
|
|||||||
services.AddTransient<IUserRepo, UserRepo>();
|
services.AddTransient<IUserRepo, UserRepo>();
|
||||||
services.AddTransient<ISpendingGroupRepo, SpendingGroupRepo>();
|
services.AddTransient<ISpendingGroupRepo, SpendingGroupRepo>();
|
||||||
services.AddTransient<IChangeRecordRepo, ChangeRecordRepo>();
|
services.AddTransient<IChangeRecordRepo, ChangeRecordRepo>();
|
||||||
|
services.AddTransient<ISpendingPlanRepo, SpendingPlanRepo>();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -17,13 +17,13 @@ public class SpendingPlanService : ISpendingPlanService
|
|||||||
_spendingPlanRepo = spendingPlanRepo;
|
_spendingPlanRepo = spendingPlanRepo;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<SpendingPlanView> Create(SpendingPlanDto spendingPlan)
|
public async Task<SpendingPlanViewModel> Create(SpendingPlanDto spendingPlan)
|
||||||
{
|
{
|
||||||
var plan = await _spendingPlanRepo.Create(spendingPlan);
|
var plan = await _spendingPlanRepo.Create(spendingPlan);
|
||||||
return plan.ToView();
|
return plan.ToView();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<SpendingPlanView> Delete(SpendingPlanSearch search)
|
public async Task<SpendingPlanViewModel> Delete(SpendingPlanSearch search)
|
||||||
{
|
{
|
||||||
var plan = await _spendingPlanRepo.Delete(search);
|
var plan = await _spendingPlanRepo.Delete(search);
|
||||||
if (plan == null)
|
if (plan == null)
|
||||||
@ -33,7 +33,7 @@ public class SpendingPlanService : ISpendingPlanService
|
|||||||
return plan.ToView();
|
return plan.ToView();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<SpendingPlanView> GetDetails(SpendingPlanSearch search)
|
public async Task<SpendingPlanViewModel> GetDetails(SpendingPlanSearch search)
|
||||||
{
|
{
|
||||||
var plan = await _spendingPlanRepo.Get(search);
|
var plan = await _spendingPlanRepo.Get(search);
|
||||||
if (plan == null)
|
if (plan == null)
|
||||||
@ -43,13 +43,13 @@ public class SpendingPlanService : ISpendingPlanService
|
|||||||
return plan.ToView();
|
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);
|
var plans = await _spendingPlanRepo.GetList(search);
|
||||||
return plans.Select(x => x.ToView()).ToList();
|
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);
|
var plan = await _spendingPlanRepo.Update(spendingPlan);
|
||||||
if (plan == null)
|
if (plan == null)
|
||||||
|
Loading…
Reference in New Issue
Block a user