From 9b019c7251e2eb7b5eae4f889b30ed5e6591085f Mon Sep 17 00:00:00 2001 From: mfnefd Date: Sun, 1 Dec 2024 20:32:23 +0400 Subject: [PATCH] =?UTF-8?q?add:=20=D1=81=D0=B5=D1=80=D0=B2=D0=B8=D1=81=20?= =?UTF-8?q?=D0=BE=D1=82=D1=87=D0=B5=D1=82=D0=B0=20=D0=BE=20=D1=81=D0=BC?= =?UTF-8?q?=D0=B5=D1=89=D0=B5=D0=BD=D0=B8=D0=B8=20=D0=B3=D1=80=D1=83=D0=BF?= =?UTF-8?q?=D0=BF=D1=8B=20=D1=82=D1=80=D0=B0=D1=82=20=D0=BE=D1=82=D0=BD?= =?UTF-8?q?=D0=BE=D1=81=D0=B8=D1=82=D0=B5=D0=BB=D1=8C=D0=BD=D0=BE=20=D0=BF?= =?UTF-8?q?=D0=BB=D0=B0=D0=BD=D0=B0,=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=20?= =?UTF-8?q?=D0=B2=20=D1=80=D0=B5=D0=BF=D0=BE=D0=B7=D0=B8=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D0=B8=D0=B8=20=D0=B3=D1=80=D1=83=D0=BF=D0=BF=D1=8B=20=D1=80?= =?UTF-8?q?=D0=B0=D1=81=D1=85=D0=BE=D0=B4=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Repositories/ISpendingGroupRepo.cs | 1 + .../Services/IReportOffsetFromPlanService.cs | 9 ++++++ .../Repositories/SpendingGroupRepo.cs | 24 ++++++++++++++ .../Reports/ReportOffsetFromPlanService.cs | 32 +++++++++++++++++++ .../Exceptions/ReportDataNotFoundException.cs | 10 ++++++ 5 files changed, 76 insertions(+) create mode 100644 back/Contracts/Services/IReportOffsetFromPlanService.cs create mode 100644 back/Services/Reports/ReportOffsetFromPlanService.cs create mode 100644 back/Services/Support/Exceptions/ReportDataNotFoundException.cs diff --git a/back/Contracts/Repositories/ISpendingGroupRepo.cs b/back/Contracts/Repositories/ISpendingGroupRepo.cs index f8f7999..54f4476 100644 --- a/back/Contracts/Repositories/ISpendingGroupRepo.cs +++ b/back/Contracts/Repositories/ISpendingGroupRepo.cs @@ -6,6 +6,7 @@ namespace Contracts.Repositories; public interface ISpendingGroupRepo { Task Get(SpendingGroupSearch search); + Task GetByPlan(SpendingPlanSearch search); Task> GetList(SpendingGroupSearch? search = null); Task Create(SpendingGroupDto spendingGroup); Task Delete(SpendingGroupSearch search); diff --git a/back/Contracts/Services/IReportOffsetFromPlanService.cs b/back/Contracts/Services/IReportOffsetFromPlanService.cs new file mode 100644 index 0000000..f9c51ed --- /dev/null +++ b/back/Contracts/Services/IReportOffsetFromPlanService.cs @@ -0,0 +1,9 @@ +using Contracts.SearchModels; +using Contracts.ViewModels; + +namespace Contracts.Services; + +public interface IReportOffsetFromPlanService +{ + public Task GetReportData(SpendingPlanSearch search); +} \ No newline at end of file diff --git a/back/Infrastructure/Repositories/SpendingGroupRepo.cs b/back/Infrastructure/Repositories/SpendingGroupRepo.cs index 6c8382a..a356914 100644 --- a/back/Infrastructure/Repositories/SpendingGroupRepo.cs +++ b/back/Infrastructure/Repositories/SpendingGroupRepo.cs @@ -54,6 +54,30 @@ public class SpendingGroupRepo : ISpendingGroupRepo return group?.ToDto(); } + public async Task 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> GetList(SpendingGroupSearch? search = null) { using var context = _factory.CreateDbContext(); diff --git a/back/Services/Reports/ReportOffsetFromPlanService.cs b/back/Services/Reports/ReportOffsetFromPlanService.cs new file mode 100644 index 0000000..d2a66b6 --- /dev/null +++ b/back/Services/Reports/ReportOffsetFromPlanService.cs @@ -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 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(); + } +} diff --git a/back/Services/Support/Exceptions/ReportDataNotFoundException.cs b/back/Services/Support/Exceptions/ReportDataNotFoundException.cs new file mode 100644 index 0000000..ffdd60e --- /dev/null +++ b/back/Services/Support/Exceptions/ReportDataNotFoundException.cs @@ -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) { } + +} \ No newline at end of file