0.2.0 #4

Merged
mfnefd merged 15 commits from dev into main 2024-12-11 04:42:35 +04:00
5 changed files with 76 additions and 0 deletions
Showing only changes of commit 9b019c7251 - Show all commits

View File

@ -6,6 +6,7 @@ namespace Contracts.Repositories;
public interface ISpendingGroupRepo
{
Task<SpendingGroupDto?> Get(SpendingGroupSearch search);
Task<SpendingGroupDto?> GetByPlan(SpendingPlanSearch search);
Task<IEnumerable<SpendingGroupDto>> GetList(SpendingGroupSearch? search = null);
Task<SpendingGroupDto> Create(SpendingGroupDto spendingGroup);
Task<SpendingGroupDto?> Delete(SpendingGroupSearch search);

View File

@ -0,0 +1,9 @@
using Contracts.SearchModels;
using Contracts.ViewModels;
namespace Contracts.Services;
public interface IReportOffsetFromPlanService
{
public Task<SpendingGroupViewModel> GetReportData(SpendingPlanSearch search);
}

View File

@ -54,6 +54,30 @@ public class SpendingGroupRepo : ISpendingGroupRepo
return group?.ToDto();
}
public async Task<SpendingGroupDto?> GetByPlan(SpendingPlanSearch search)
{
using var context = _factory.CreateDbContext();
var plan = await context.SpendingPlans
.FirstOrDefaultAsync(x => x.Id == search.Id);
if (plan == null)
{
return null;
}
var group = await context.SpendingGroups
.Where(x => x.Id == plan.SpendingGroupId)
.Include(x => x.ChangeRecords != null
? x.ChangeRecords
.Where(cg => cg.ChangedAt >= plan.StartAt && cg.ChangedAt <= plan.EndAt)
.OrderBy(cg => cg.ChangedAt)
: null)
.LastAsync();
group.SpendingPlans = [plan];
return group.ToDto();
}
public async Task<IEnumerable<SpendingGroupDto>> GetList(SpendingGroupSearch? search = null)
{
using var context = _factory.CreateDbContext();

View File

@ -0,0 +1,32 @@
using Contracts.Mappers;
using Contracts.Repositories;
using Contracts.SearchModels;
using Contracts.Services;
using Contracts.ViewModels;
using Services.Support.Exceptions;
namespace Services.Reports;
public class ReportOffsetFromPlanService : IReportOffsetFromPlanService
{
private readonly ISpendingGroupRepo _spendingGroupRepo;
public ReportOffsetFromPlanService(ISpendingGroupRepo spendingGroupRepo)
{
_spendingGroupRepo = spendingGroupRepo;
}
public async Task<SpendingGroupViewModel> GetReportData(SpendingPlanSearch search)
{
var group = await _spendingGroupRepo.GetByPlan(search);
if (group == null)
{
throw new EntityNotFoundException("Не удалось найти группу по такому плану");
}
if (!group.ChangeRecords.Any())
{
throw new ReportDataNotFoundException("Данные об изменении баланса отсутствуют");
}
return group.ToView();
}
}

View File

@ -0,0 +1,10 @@
namespace Services.Support.Exceptions;
public class ReportDataNotFoundException : EntityNotFoundException
{
public ReportDataNotFoundException(string message)
: base(message) { }
public ReportDataNotFoundException(string message, Exception innerException)
: base(message, innerException) { }
}