add: репозиторий групп расходов

This commit is contained in:
mfnefd 2024-11-26 00:15:31 +04:00
parent 3f76874547
commit 6e4e47dc59
4 changed files with 128 additions and 0 deletions

View File

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

View 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!;
}

View 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();
}
}

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