From 5cb4937bcf4dd0659d96098a4e5cea2c0c86222d Mon Sep 17 00:00:00 2001 From: mfnefd Date: Sun, 1 Dec 2024 23:49:16 +0400 Subject: [PATCH] =?UTF-8?q?fix:=20=D1=82=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20?= =?UTF-8?q?=D0=BE=D1=82=D1=87=D0=B5=D1=82=20=D1=81=D0=BC=D0=B5=D1=89=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5?= =?UTF-8?q?=D1=82=20=D0=BD=D0=BE=D1=80=D0=BC=D0=B0=D0=BB=D1=8C=D0=BD=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extensions/AddRepotServicesExt.cs | 13 +++++++++++++ back/Controllers/Program.cs | 1 + .../Repositories/SpendingGroupRepo.cs | 19 ++++++++++++++----- back/Services/Reports/ReportPeriodService.cs | 2 +- 4 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 back/Controllers/Extensions/AddRepotServicesExt.cs diff --git a/back/Controllers/Extensions/AddRepotServicesExt.cs b/back/Controllers/Extensions/AddRepotServicesExt.cs new file mode 100644 index 0000000..6c2832b --- /dev/null +++ b/back/Controllers/Extensions/AddRepotServicesExt.cs @@ -0,0 +1,13 @@ +using Contracts.Services; +using Services.Reports; + +namespace Controllers.Extensions; + +public static class AddReportServicesExtension +{ + public static void AddReportServices(this IServiceCollection services) + { + services.AddTransient(); + services.AddTransient(); + } +} \ No newline at end of file diff --git a/back/Controllers/Program.cs b/back/Controllers/Program.cs index 2e20712..73d61e5 100644 --- a/back/Controllers/Program.cs +++ b/back/Controllers/Program.cs @@ -6,6 +6,7 @@ var builder = WebApplication.CreateBuilder(args); builder.Services.AddDbConnectionService(builder.Configuration); builder.Services.AddRepos(); builder.Services.AddDomainServices(); +builder.Services.AddReportServices(); builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle diff --git a/back/Infrastructure/Repositories/SpendingGroupRepo.cs b/back/Infrastructure/Repositories/SpendingGroupRepo.cs index a356914..a04ee9b 100644 --- a/back/Infrastructure/Repositories/SpendingGroupRepo.cs +++ b/back/Infrastructure/Repositories/SpendingGroupRepo.cs @@ -46,6 +46,7 @@ public class SpendingGroupRepo : ISpendingGroupRepo using var context = _factory.CreateDbContext(); var group = await context.SpendingGroups + .AsNoTracking() .Include(x => x.ChangeRecords) .Include(x => x.SpendingPlans) .FirstOrDefaultAsync(x => x.Id == search.Id @@ -58,6 +59,7 @@ public class SpendingGroupRepo : ISpendingGroupRepo { using var context = _factory.CreateDbContext(); var plan = await context.SpendingPlans + .AsNoTracking() .FirstOrDefaultAsync(x => x.Id == search.Id); if (plan == null) @@ -66,13 +68,20 @@ public class SpendingGroupRepo : ISpendingGroupRepo } var group = await context.SpendingGroups + .AsNoTracking() .Where(x => x.Id == plan.SpendingGroupId) - .Include(x => x.ChangeRecords != null - ? x.ChangeRecords + .Include(x => x.ChangeRecords + // Выбираем из них только те, которые попадают в диапазон дат плана .Where(cg => cg.ChangedAt >= plan.StartAt && cg.ChangedAt <= plan.EndAt) + // И сортируем их по дате .OrderBy(cg => cg.ChangedAt) - : null) - .LastAsync(); + ) + .FirstOrDefaultAsync(); + if (group == null) + { + return null; + } + group.SpendingPlans = [plan]; return group.ToDto(); @@ -82,7 +91,7 @@ public class SpendingGroupRepo : ISpendingGroupRepo { using var context = _factory.CreateDbContext(); - var query = context.SpendingGroups.AsQueryable(); + var query = context.SpendingGroups.AsNoTracking().AsQueryable(); if (search != null) { diff --git a/back/Services/Reports/ReportPeriodService.cs b/back/Services/Reports/ReportPeriodService.cs index 51b64a0..96539fa 100644 --- a/back/Services/Reports/ReportPeriodService.cs +++ b/back/Services/Reports/ReportPeriodService.cs @@ -3,7 +3,7 @@ using Contracts.Repositories; using Contracts.SearchModels; using Contracts.Services; using Contracts.ViewModels; -using Services.Support; +using Services.Support.Exceptions; namespace Services.Reports;