add: сервис, репозиторий и модель записи изменения баланса
This commit is contained in:
parent
27df19201a
commit
f94d4ed246
@ -1,6 +1,6 @@
|
||||
namespace Contracts.DTO;
|
||||
|
||||
public interface ChangeRecordDto
|
||||
public class ChangeRecordDto
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public Guid UserId { get; set; }
|
||||
|
@ -13,4 +13,5 @@ public class DatabaseContext : DbContext
|
||||
|
||||
public DbSet<User> Users { get; set; } = null!;
|
||||
public DbSet<SpendingGroup> SpendingGroups { get; set; } = null!;
|
||||
public DbSet<ChangeRecord> ChangeRecords { get; set; } = null!;
|
||||
}
|
24
back/Infrastructure/Models/Changerecord.cs
Normal file
24
back/Infrastructure/Models/Changerecord.cs
Normal file
@ -0,0 +1,24 @@
|
||||
using Contracts.DTO;
|
||||
|
||||
namespace Infrastructure.Models;
|
||||
|
||||
public class ChangeRecord
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public decimal Sum { get; set; }
|
||||
public DateTime ChangedAt { get; set; }
|
||||
|
||||
public Guid UserId { get; set; }
|
||||
public User User { get; set; } = null!;
|
||||
|
||||
public Guid SpendingGroupId { get; set; }
|
||||
public SpendingGroup SpendingGroup { get; set; } = null!;
|
||||
|
||||
public void Update(ChangeRecordDto changeRecordDto)
|
||||
{
|
||||
Id = changeRecordDto.Id;
|
||||
Sum = changeRecordDto.Sum;
|
||||
ChangedAt = changeRecordDto.ChangedAt;
|
||||
SpendingGroupId = changeRecordDto.SpendingGroupId;
|
||||
}
|
||||
}
|
94
back/Infrastructure/Repositories/ChangeRecordRepo.cs
Normal file
94
back/Infrastructure/Repositories/ChangeRecordRepo.cs
Normal file
@ -0,0 +1,94 @@
|
||||
using Contracts.DTO;
|
||||
using Contracts.Repositories;
|
||||
using Contracts.SearchModels;
|
||||
using Infrastructure.Support.Mappers;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Infrastructure.Repositories;
|
||||
|
||||
public class ChangeRecordRepo : IChangeRecordRepo
|
||||
{
|
||||
public readonly IDbContextFactory<DatabaseContext> _factory;
|
||||
|
||||
public ChangeRecordRepo(IDbContextFactory<DatabaseContext> factory)
|
||||
{
|
||||
_factory = factory;
|
||||
}
|
||||
|
||||
public async Task<ChangeRecordDto> Create(ChangeRecordDto changeRecord)
|
||||
{
|
||||
using var context = _factory.CreateDbContext();
|
||||
|
||||
var createdRecord = await context.ChangeRecords.AddAsync(changeRecord.ToModel());
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
return createdRecord.Entity.ToDto();
|
||||
}
|
||||
|
||||
public async Task<ChangeRecordDto?> Delete(ChangeRecordSearch search)
|
||||
{
|
||||
using var context = _factory.CreateDbContext();
|
||||
|
||||
var record = await context.ChangeRecords
|
||||
.FirstOrDefaultAsync(x => x.Id == search.Id);
|
||||
if (record == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
context.ChangeRecords.Remove(record);
|
||||
await context.SaveChangesAsync();
|
||||
return record.ToDto();
|
||||
}
|
||||
|
||||
public async Task<ChangeRecordDto?> Get(ChangeRecordSearch search)
|
||||
{
|
||||
using var context = _factory.CreateDbContext();
|
||||
|
||||
var record = await context.ChangeRecords
|
||||
.FirstOrDefaultAsync(x => x.Id == search.Id);
|
||||
if (record == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return record.ToDto();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<ChangeRecordDto>> GetList(ChangeRecordSearch? search = null)
|
||||
{
|
||||
using var context = _factory.CreateDbContext();
|
||||
|
||||
var query = context.ChangeRecords.AsQueryable();
|
||||
|
||||
if (search != null)
|
||||
{
|
||||
if (search.SpendingGroupId.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.SpendingGroupId == search.SpendingGroupId);
|
||||
}
|
||||
if (search.From.HasValue && search.To.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.ChangedAt >= search.From && x.ChangedAt <= search.To);
|
||||
}
|
||||
}
|
||||
return query.Select(x => x.ToDto());
|
||||
}
|
||||
|
||||
public async Task<ChangeRecordDto?> Update(ChangeRecordDto changeRecord)
|
||||
{
|
||||
using var context = _factory.CreateDbContext();
|
||||
|
||||
var existingRecord = await context.ChangeRecords
|
||||
.FirstOrDefaultAsync(x => x.Id == changeRecord.Id);
|
||||
|
||||
if (existingRecord == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
existingRecord.Update(changeRecord);
|
||||
context.ChangeRecords.Update(existingRecord);
|
||||
await context.SaveChangesAsync();
|
||||
return existingRecord.ToDto();
|
||||
}
|
||||
}
|
27
back/Infrastructure/Support/Mappers/ChangeRecordMapper.cs
Normal file
27
back/Infrastructure/Support/Mappers/ChangeRecordMapper.cs
Normal file
@ -0,0 +1,27 @@
|
||||
using Contracts.DTO;
|
||||
using Infrastructure.Models;
|
||||
|
||||
namespace Infrastructure.Support.Mappers;
|
||||
|
||||
public static class ChangeRecordMapper
|
||||
{
|
||||
public static ChangeRecordDto ToDto(this ChangeRecord changeRecord)
|
||||
=> new()
|
||||
{
|
||||
Id = changeRecord.Id,
|
||||
Sum = changeRecord.Sum,
|
||||
ChangedAt = changeRecord.ChangedAt,
|
||||
SpendingGroupId = changeRecord.SpendingGroupId,
|
||||
UserId = changeRecord.UserId
|
||||
};
|
||||
|
||||
public static ChangeRecord ToModel(this ChangeRecordDto changeRecord)
|
||||
=> new()
|
||||
{
|
||||
Id = changeRecord.Id,
|
||||
Sum = changeRecord.Sum,
|
||||
ChangedAt = changeRecord.ChangedAt,
|
||||
SpendingGroupId = changeRecord.SpendingGroupId,
|
||||
UserId = changeRecord.UserId
|
||||
};
|
||||
}
|
52
back/Services/Domain/ChangeRecordService.cs
Normal file
52
back/Services/Domain/ChangeRecordService.cs
Normal file
@ -0,0 +1,52 @@
|
||||
using Contracts.DTO;
|
||||
using Contracts.Mappers;
|
||||
using Contracts.Repositories;
|
||||
using Contracts.SearchModels;
|
||||
using Contracts.Services;
|
||||
using Contracts.ViewModels;
|
||||
|
||||
namespace Services.Domain;
|
||||
|
||||
public class ChangeRecordService : IChangeRecordService
|
||||
{
|
||||
private readonly IChangeRecordRepo _changeRecordRepo;
|
||||
|
||||
public ChangeRecordService(IChangeRecordRepo changeRecordRepo)
|
||||
{
|
||||
_changeRecordRepo = changeRecordRepo;
|
||||
}
|
||||
|
||||
public async Task<ChangeRecordViewModel> Create(ChangeRecordDto model)
|
||||
{
|
||||
var record = await _changeRecordRepo.Create(model);
|
||||
|
||||
return record.ToView();
|
||||
}
|
||||
|
||||
public async Task<ChangeRecordViewModel> Delete(ChangeRecordSearch search)
|
||||
{
|
||||
var record = await _changeRecordRepo.Delete(search);
|
||||
if (record == null)
|
||||
{
|
||||
throw new EntryPointNotFoundException("При удалении не получилось найти запись измнения баланса");
|
||||
}
|
||||
return record.ToView();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<ChangeRecordViewModel>> GetList(ChangeRecordSearch search)
|
||||
{
|
||||
var records = await _changeRecordRepo.GetList(search);
|
||||
|
||||
return records.Select(x => x.ToView()).ToList();
|
||||
}
|
||||
|
||||
public async Task<ChangeRecordViewModel> Update(ChangeRecordDto model)
|
||||
{
|
||||
var record = await _changeRecordRepo.Update(model);
|
||||
if (record == null)
|
||||
{
|
||||
throw new EntryPointNotFoundException("При изменении не получилось найти запись измнения баланса");
|
||||
}
|
||||
return record.ToView();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user