add: репозиторий групп расходов
This commit is contained in:
parent
3f76874547
commit
6e4e47dc59
@ -1,3 +1,4 @@
|
|||||||
|
using Contracts.DTO;
|
||||||
using Infrastructure.Models;
|
using Infrastructure.Models;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
@ -12,4 +13,5 @@ public class DatabaseContext : DbContext
|
|||||||
}
|
}
|
||||||
|
|
||||||
public DbSet<User> Users { get; set; } = null!;
|
public DbSet<User> Users { get; set; } = null!;
|
||||||
|
public DbSet<SpendingGroup> SpendingGroups { get; set; } = null!;
|
||||||
}
|
}
|
10
back/Infrastructure/Models/SpendingGroup.cs
Normal file
10
back/Infrastructure/Models/SpendingGroup.cs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
namespace Infrastructure.Models;
|
||||||
|
|
||||||
|
public class SpendingGroup
|
||||||
|
{
|
||||||
|
public Guid Id { get; set; }
|
||||||
|
public string Name { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
public Guid UserId { get; set; }
|
||||||
|
public User User { get; set; } = null!;
|
||||||
|
}
|
94
back/Infrastructure/Repositories/SpendingGroupRepo.cs
Normal file
94
back/Infrastructure/Repositories/SpendingGroupRepo.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 SpendingGroupRepo : ISpendingGroupRepo
|
||||||
|
{
|
||||||
|
public readonly IDbContextFactory<DatabaseContext> _factory;
|
||||||
|
|
||||||
|
public SpendingGroupRepo(IDbContextFactory<DatabaseContext> factory)
|
||||||
|
{
|
||||||
|
_factory = factory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<SpendingGroupDto> Create(SpendingGroupDto spendingGroup)
|
||||||
|
{
|
||||||
|
using var context = _factory.CreateDbContext();
|
||||||
|
|
||||||
|
var createdGroup = await context.SpendingGroups.AddAsync(spendingGroup.ToModel());
|
||||||
|
await context.SaveChangesAsync();
|
||||||
|
return createdGroup.Entity.ToDto();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<SpendingGroupDto?> Delete(SpendingGroupSearch search)
|
||||||
|
{
|
||||||
|
using var context = _factory.CreateDbContext();
|
||||||
|
|
||||||
|
var group = await context.SpendingGroups
|
||||||
|
.FirstOrDefaultAsync(x => x.Id == search.Id
|
||||||
|
|| x.Name == search.Name);
|
||||||
|
if (group == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
context.SpendingGroups.Remove(group);
|
||||||
|
await context.SaveChangesAsync();
|
||||||
|
return group.ToDto();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<SpendingGroupDto?> Get(SpendingGroupSearch search)
|
||||||
|
{
|
||||||
|
using var context = _factory.CreateDbContext();
|
||||||
|
|
||||||
|
var group = await context.SpendingGroups
|
||||||
|
.FirstOrDefaultAsync(x => x.Id == search.Id
|
||||||
|
|| x.Name == search.Name);
|
||||||
|
|
||||||
|
return group?.ToDto();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IEnumerable<SpendingGroupDto>> GetList(SpendingGroupSearch? search = null)
|
||||||
|
{
|
||||||
|
using var context = _factory.CreateDbContext();
|
||||||
|
|
||||||
|
var query = context.SpendingGroups.AsQueryable();
|
||||||
|
|
||||||
|
if (search != null)
|
||||||
|
{
|
||||||
|
if (search.Id != null)
|
||||||
|
{
|
||||||
|
query = query.Where(x => x.Id == search.Id);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(search.Name))
|
||||||
|
{
|
||||||
|
query = query.Where(x => x.Name.Contains(search.Name, StringComparison.OrdinalIgnoreCase));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return await query.Select(x => x.ToDto()).ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<SpendingGroupDto?> Update(SpendingGroupDto spendingGroup)
|
||||||
|
{
|
||||||
|
using var context = _factory.CreateDbContext();
|
||||||
|
|
||||||
|
var existingGroup = await context.SpendingGroups
|
||||||
|
.FirstOrDefaultAsync(x => x.Id == spendingGroup.Id);
|
||||||
|
|
||||||
|
if (existingGroup == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
existingGroup.Name = spendingGroup.Name;
|
||||||
|
context.SpendingGroups.Update(existingGroup);
|
||||||
|
await context.SaveChangesAsync();
|
||||||
|
return existingGroup.ToDto();
|
||||||
|
}
|
||||||
|
}
|
22
back/Infrastructure/Support/Mappers/SpendingGroupMapper.cs
Normal file
22
back/Infrastructure/Support/Mappers/SpendingGroupMapper.cs
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
using Contracts.DTO;
|
||||||
|
using Infrastructure.Models;
|
||||||
|
|
||||||
|
namespace Infrastructure.Support.Mappers;
|
||||||
|
|
||||||
|
public static class SpendingGroupMapper
|
||||||
|
{
|
||||||
|
public static SpendingGroupDto ToDto(this SpendingGroup group)
|
||||||
|
=> new()
|
||||||
|
{
|
||||||
|
Id = group.Id,
|
||||||
|
Name = group.Name,
|
||||||
|
UserId = group.UserId
|
||||||
|
};
|
||||||
|
public static SpendingGroup ToModel(this SpendingGroupDto group)
|
||||||
|
=> new()
|
||||||
|
{
|
||||||
|
Id = group.Id,
|
||||||
|
Name = group.Name,
|
||||||
|
UserId = group.UserId
|
||||||
|
};
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user