domBudg/back/Services/Domain/SpendingPlanService.cs

62 lines
1.9 KiB
C#
Raw Normal View History

using Contracts.DTO;
using Contracts.Mappers;
using Contracts.Repositories;
using Contracts.SearchModels;
using Contracts.Services;
using Contracts.ViewModels;
using Services.Support.Exceptions;
namespace Services.Domain;
public class SpendingPlanService : ISpendingPlanService
{
private readonly ISpendingPlanRepo _spendingPlanRepo;
public SpendingPlanService(ISpendingPlanRepo spendingPlanRepo)
{
_spendingPlanRepo = spendingPlanRepo;
}
public async Task<SpendingPlanViewModel> Create(SpendingPlanDto spendingPlan)
{
var plan = await _spendingPlanRepo.Create(spendingPlan);
return plan.ToView();
}
public async Task<SpendingPlanViewModel> Delete(SpendingPlanSearch search)
{
var plan = await _spendingPlanRepo.Delete(search);
if (plan == null)
{
throw new EntityNotFoundException("При удалении не получилось найти план");
}
return plan.ToView();
}
public async Task<SpendingPlanViewModel> GetDetails(SpendingPlanSearch search)
{
var plan = await _spendingPlanRepo.Get(search);
if (plan == null)
{
throw new EntityNotFoundException("Не удалось найти план по таким параметрам");
}
return plan.ToView();
}
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<SpendingPlanViewModel> Update(SpendingPlanDto spendingPlan)
{
var plan = await _spendingPlanRepo.Update(spendingPlan);
if (plan == null)
{
throw new EntityNotFoundException("При обновлении не получилось найти план");
}
return plan.ToView();
}
}