0.2.0 #4

Merged
mfnefd merged 15 commits from dev into main 2024-12-11 04:42:35 +04:00
8 changed files with 84 additions and 6 deletions
Showing only changes of commit 13dbcc5bc1 - Show all commits

View File

@ -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; }
}

View File

@ -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
};
}

View File

@ -0,0 +1,8 @@
using Contracts.ViewModels;
namespace Contracts.Services;
public interface IReportPeriodService
{
Task<IEnumerable<ChangeRecordViewModel>> GetReportData(DateTime from, DateTime to);
}

View File

@ -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;
}

View File

@ -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<ActionResult<IEnumerable<ChangeRecordViewModel>>> 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);
}
}
}

View File

@ -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<IAuthService, AuthService>();
services.AddTransient<IUserService, UserService>();
services.AddTransient<ISpendingGroupService, SpendingGroupService>();
services.AddTransient<IChangeRecordService, ChangeRecordService>();
services.AddTransient<ISpendingPlanService, SpendingPlanService>();
services.AddTransient<IReportPeriodService, ReportPeriodService>();
}
}

View File

@ -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<ChangeRecordDto?> Update(ChangeRecordDto changeRecord)

View File

@ -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<IEnumerable<ChangeRecordViewModel>> 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();
}
}