From 7d820c8a21e88ea24b78a0fe31dc7b07d7891b1f Mon Sep 17 00:00:00 2001 From: mfnefd Date: Wed, 27 Nov 2024 14:24:59 +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=B2=D1=8B=D0=B1=D0=BE=D1=80=D0=BA=D0=B8=20=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=BD=D1=8B=D1=85=20=D0=B4=D0=BB=D1=8F=20=D0=BE=D1=82=D1=87?= =?UTF-8?q?=D0=B5=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/Contracts/DTOs/ChangeRecordDto.cs | 1 + back/Contracts/Mappers/ChangeRecordMapper.cs | 5 ++- .../Services/IReportPeriodService.cs | 8 ++++ .../ViewModels/ChangeRecordViewModel.cs | 1 + .../Controllers/ReportController.cs | 37 +++++++++++++++++++ .../Extensions/AddDomainServicesExt.cs | 6 +-- .../Repositories/ChangeRecordRepo.cs | 3 +- back/Services/Reports/ReportPeriodService.cs | 29 +++++++++++++++ .../Exceptions/ReportDataNotFoundException.cs | 10 +++++ 9 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 back/Contracts/Services/IReportPeriodService.cs create mode 100644 back/Controllers/Controllers/ReportController.cs create mode 100644 back/Services/Reports/ReportPeriodService.cs create mode 100644 back/Services/Support/Exceptions/ReportDataNotFoundException.cs diff --git a/back/Contracts/DTOs/ChangeRecordDto.cs b/back/Contracts/DTOs/ChangeRecordDto.cs index 21d140c..6e2fe42 100644 --- a/back/Contracts/DTOs/ChangeRecordDto.cs +++ b/back/Contracts/DTOs/ChangeRecordDto.cs @@ -5,6 +5,7 @@ public class ChangeRecordDto public Guid Id { get; set; } public Guid UserId { get; set; } public Guid? SpendingGroupId { get; set; } + public string SpendingGroupName { get; set; } = string.Empty; public decimal Sum { get; set; } public DateTime ChangedAt { get; set; } } \ No newline at end of file diff --git a/back/Contracts/Mappers/ChangeRecordMapper.cs b/back/Contracts/Mappers/ChangeRecordMapper.cs index 606344d..065eff0 100644 --- a/back/Contracts/Mappers/ChangeRecordMapper.cs +++ b/back/Contracts/Mappers/ChangeRecordMapper.cs @@ -8,8 +8,9 @@ public static class ChangeRecordMapper public static ChangeRecordViewModel ToView(this ChangeRecordDto dto) => new() { - Id = dto.Id, + Id = dto.Id, Sum = dto.Sum, - ChangedAt = dto.ChangedAt + ChangedAt = dto.ChangedAt, + SpendingGroupName = dto.SpendingGroupName }; } \ No newline at end of file diff --git a/back/Contracts/Services/IReportPeriodService.cs b/back/Contracts/Services/IReportPeriodService.cs new file mode 100644 index 0000000..46719a4 --- /dev/null +++ b/back/Contracts/Services/IReportPeriodService.cs @@ -0,0 +1,8 @@ +using Contracts.ViewModels; + +namespace Contracts.Services; + +public interface IReportPeriodService +{ + Task> GetReportData(DateTime from, DateTime to); +} \ No newline at end of file diff --git a/back/Contracts/ViewModels/ChangeRecordViewModel.cs b/back/Contracts/ViewModels/ChangeRecordViewModel.cs index 3a75eb3..99e6d82 100644 --- a/back/Contracts/ViewModels/ChangeRecordViewModel.cs +++ b/back/Contracts/ViewModels/ChangeRecordViewModel.cs @@ -5,4 +5,5 @@ public class ChangeRecordViewModel public Guid Id { get; set; } public decimal Sum { get; set; } public DateTime ChangedAt { get; set; } + public string SpendingGroupName { get; set; } = string.Empty; } \ No newline at end of file diff --git a/back/Controllers/Controllers/ReportController.cs b/back/Controllers/Controllers/ReportController.cs new file mode 100644 index 0000000..3ff6690 --- /dev/null +++ b/back/Controllers/Controllers/ReportController.cs @@ -0,0 +1,37 @@ +using Contracts.Services; +using Contracts.ViewModels; +using Microsoft.AspNetCore.Mvc; +using Services.Support; + +namespace Controllers.Controllers; + +[ApiController] +[Route("api/[controller]")] +public class ReportController : ControllerBase +{ + private readonly IReportPeriodService _reportPeriodService; + + public ReportController(IReportPeriodService reportPeriodService) + { + _reportPeriodService = reportPeriodService; + } + + [HttpGet("period")] + public async Task>> GetReportData( + [FromQuery] DateTime from, [FromQuery] DateTime to) + { + try + { + var periodData = await _reportPeriodService.GetReportData(from, to); + return Ok(periodData); + } + catch (ReportDataNotFoundException ex) + { + return NotFound(ex.Message); + } + catch (Exception ex) + { + return StatusCode(500, ex.Message); + } + } +} \ No newline at end of file diff --git a/back/Controllers/Extensions/AddDomainServicesExt.cs b/back/Controllers/Extensions/AddDomainServicesExt.cs index 7d6cc0a..1a7fc59 100644 --- a/back/Controllers/Extensions/AddDomainServicesExt.cs +++ b/back/Controllers/Extensions/AddDomainServicesExt.cs @@ -1,5 +1,6 @@ using Contracts.Services; using Services.Domain; +using Services.Reports; namespace Controllers.Extensions; @@ -9,11 +10,10 @@ public static class AddDomainServicesExtension { services.AddTransient(); services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); + + services.AddTransient(); } } \ No newline at end of file diff --git a/back/Infrastructure/Repositories/ChangeRecordRepo.cs b/back/Infrastructure/Repositories/ChangeRecordRepo.cs index 6da1839..94aab20 100644 --- a/back/Infrastructure/Repositories/ChangeRecordRepo.cs +++ b/back/Infrastructure/Repositories/ChangeRecordRepo.cs @@ -46,6 +46,7 @@ public class ChangeRecordRepo : IChangeRecordRepo using var context = _factory.CreateDbContext(); var record = await context.ChangeRecords + .Include(x => x.SpendingGroup) .FirstOrDefaultAsync(x => x.Id == search.Id); if (record == null) { @@ -71,7 +72,7 @@ public class ChangeRecordRepo : IChangeRecordRepo query = query.Where(x => x.ChangedAt >= search.From && x.ChangedAt <= search.To); } } - return await query.Select(x => x.ToDto()).ToListAsync(); + return await query.Include(x => x.SpendingGroup).Select(x => x.ToDto()).ToListAsync(); } public async Task Update(ChangeRecordDto changeRecord) diff --git a/back/Services/Reports/ReportPeriodService.cs b/back/Services/Reports/ReportPeriodService.cs new file mode 100644 index 0000000..51b64a0 --- /dev/null +++ b/back/Services/Reports/ReportPeriodService.cs @@ -0,0 +1,29 @@ +using Contracts.Mappers; +using Contracts.Repositories; +using Contracts.SearchModels; +using Contracts.Services; +using Contracts.ViewModels; +using Services.Support; + +namespace Services.Reports; + +public class ReportPeriodService : IReportPeriodService +{ + private readonly IChangeRecordRepo _changeRecordRepo; + + public ReportPeriodService(IChangeRecordRepo changeRecordRepo) + { + _changeRecordRepo = changeRecordRepo; + } + + public async Task> GetReportData(DateTime from, DateTime to) + { + var records = await _changeRecordRepo.GetList(new ChangeRecordSearch() { From = from, To = to }); + + if (!records.Any()) + { + throw new ReportDataNotFoundException("Нет данных за указанный период"); + } + return records.Select(x => x.ToView()).ToList(); + } +} diff --git a/back/Services/Support/Exceptions/ReportDataNotFoundException.cs b/back/Services/Support/Exceptions/ReportDataNotFoundException.cs new file mode 100644 index 0000000..915e419 --- /dev/null +++ b/back/Services/Support/Exceptions/ReportDataNotFoundException.cs @@ -0,0 +1,10 @@ +using Services.Support.Exceptions; + +namespace Services.Support; + +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