add: сервис и репозиторий плана расходов

This commit is contained in:
mfnefd 2024-11-27 02:13:01 +04:00
parent 64566065fc
commit 0bd6261e21
7 changed files with 202 additions and 5 deletions

View File

@ -8,6 +8,6 @@ public interface ISpendingPlanRepo
Task<SpendingPlanDto> Create(SpendingPlanDto dto);
Task<SpendingPlanDto?> Update(SpendingPlanDto dto);
Task<SpendingPlanDto?> Delete(SpendingPlanSearch search);
Task<SpendingPlanDto?> GetDetails(SpendingPlanSearch search);
Task<IEnumerable<SpendingPlanDto>> GetList(SpendingGroupSearch? search = null);
Task<SpendingPlanDto?> Get(SpendingPlanSearch search);
Task<IEnumerable<SpendingPlanDto>> GetList(SpendingPlanSearch? search = null);
}

View File

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

View File

@ -14,4 +14,5 @@ public class DatabaseContext : DbContext
public DbSet<User> Users { get; set; } = null!;
public DbSet<SpendingGroup> SpendingGroups { get; set; } = null!;
public DbSet<ChangeRecord> ChangeRecords { get; set; } = null!;
public DbSet<SpendingPlan> SpendingPlans { get; set; } = null!;
}

View File

@ -0,0 +1,22 @@
using Contracts.DTO;
namespace Infrastructure.Models;
public class SpendingPlan
{
public Guid Id { get; set; }
public DateTime StartAt { get; set; }
public DateTime EndAt { get; set; }
public decimal Sum { get; set; }
public Guid SpendingGroupId { get; set; }
public SpendingGroup SpendingGroup { get; set; } = null!;
public void Update(SpendingPlanDto spendingPlan)
{
StartAt = spendingPlan.StartAt;
EndAt = spendingPlan.EndAt;
Sum = spendingPlan.Sum;
SpendingGroupId = spendingPlan.SpendingGroupId;
}
}

View File

@ -0,0 +1,86 @@
using Contracts.DTO;
using Contracts.Repositories;
using Contracts.SearchModels;
using Infrastructure.Support.Mappers;
using Microsoft.EntityFrameworkCore;
namespace Infrastructure.Repositories;
public class SpendingPlanRepo : ISpendingPlanRepo
{
public readonly IDbContextFactory<DatabaseContext> _factory;
public SpendingPlanRepo(IDbContextFactory<DatabaseContext> factory)
{
_factory = factory;
}
public async Task<SpendingPlanDto> Create(SpendingPlanDto dto)
{
using var context = _factory.CreateDbContext();
var plan = await context.SpendingPlans.AddAsync(dto.ToModel());
await context.SaveChangesAsync();
return plan.Entity.ToDto();
}
public async Task<SpendingPlanDto?> Delete(SpendingPlanSearch search)
{
using var context = _factory.CreateDbContext();
var plan = await context.SpendingPlans.FirstOrDefaultAsync(x => x.Id == search.Id);
if (plan == null)
{
return null;
}
context.SpendingPlans.Remove(plan);
await context.SaveChangesAsync();
return plan.ToDto();
}
public async Task<SpendingPlanDto?> Get(SpendingPlanSearch search)
{
using var context = _factory.CreateDbContext();
var plan = await context.SpendingPlans.FirstOrDefaultAsync(x => x.Id == search.Id);
return plan?.ToDto();
}
public async Task<IEnumerable<SpendingPlanDto>> GetList(SpendingPlanSearch? search = null)
{
using var context = _factory.CreateDbContext();
var query = context.SpendingPlans.AsQueryable();
if (search != null)
{
if (search.Id != null)
{
query = query.Where(x => x.Id == search.Id);
}
}
return await query.Select(x => x.ToDto()).ToListAsync();
}
public async Task<SpendingPlanDto?> Update(SpendingPlanDto dto)
{
using var context = _factory.CreateDbContext();
var plan = await context.SpendingPlans.FirstOrDefaultAsync(x => x.Id == dto.Id);
if (plan == null)
{
return null;
}
plan.Update(dto);
context.SpendingPlans.Update(plan);
await context.SaveChangesAsync();
return plan.ToDto();
}
}

View File

@ -0,0 +1,27 @@
using Contracts.DTO;
using Infrastructure.Models;
namespace Infrastructure.Support.Mappers;
public static class SpendingPlanMapper
{
public static SpendingPlan ToModel(this SpendingPlanDto plan)
=> new()
{
Id = plan.Id,
StartAt = plan.StartAt,
EndAt = plan.EndAt,
Sum = plan.Sum,
SpendingGroupId = plan.SpendingGroupId
};
public static SpendingPlanDto ToDto(this SpendingPlan plan)
=> new()
{
Id = plan.Id,
StartAt = plan.StartAt,
EndAt = plan.EndAt,
Sum = plan.Sum,
SpendingGroupId = plan.SpendingGroupId
};
}

View File

@ -0,0 +1,61 @@
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<SpendingPlanView> Create(SpendingPlanDto spendingPlan)
{
var plan = await _spendingPlanRepo.Create(spendingPlan);
return plan.ToView();
}
public async Task<SpendingPlanView> Delete(SpendingPlanSearch search)
{
var plan = await _spendingPlanRepo.Delete(search);
if (plan == null)
{
throw new EntityNotFoundException("При удалении не получилось найти план");
}
return plan.ToView();
}
public async Task<SpendingPlanView> GetDetails(SpendingPlanSearch search)
{
var plan = await _spendingPlanRepo.Get(search);
if (plan == null)
{
throw new EntityNotFoundException("Не удалось найти план по таким параметрам");
}
return plan.ToView();
}
public async Task<IEnumerable<SpendingPlanView>> GetList(SpendingPlanSearch? search = null)
{
var plans = await _spendingPlanRepo.GetList(search);
return plans.Select(x => x.ToView()).ToList();
}
public async Task<SpendingPlanView> Update(SpendingPlanDto spendingPlan)
{
var plan = await _spendingPlanRepo.Update(spendingPlan);
if (plan == null)
{
throw new EntityNotFoundException("При обновлении не получилось найти план");
}
return plan.ToView();
}
}