diff --git a/back/Infrastructure/DatabaseContext.cs b/back/Infrastructure/DatabaseContext.cs index 603765e..051dcb7 100644 --- a/back/Infrastructure/DatabaseContext.cs +++ b/back/Infrastructure/DatabaseContext.cs @@ -1,3 +1,4 @@ +using Contracts.DTO; using Infrastructure.Models; using Microsoft.EntityFrameworkCore; @@ -12,4 +13,5 @@ public class DatabaseContext : DbContext } public DbSet Users { get; set; } = null!; + public DbSet SpendingGroups { get; set; } = null!; } \ No newline at end of file diff --git a/back/Infrastructure/Models/SpendingGroup.cs b/back/Infrastructure/Models/SpendingGroup.cs new file mode 100644 index 0000000..b3d5d20 --- /dev/null +++ b/back/Infrastructure/Models/SpendingGroup.cs @@ -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!; +} \ No newline at end of file diff --git a/back/Infrastructure/Repositories/SpendingGroupRepo.cs b/back/Infrastructure/Repositories/SpendingGroupRepo.cs new file mode 100644 index 0000000..38a69d0 --- /dev/null +++ b/back/Infrastructure/Repositories/SpendingGroupRepo.cs @@ -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 _factory; + + public SpendingGroupRepo(IDbContextFactory factory) + { + _factory = factory; + } + + public async Task 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 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 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> 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 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(); + } +} diff --git a/back/Infrastructure/Support/Mappers/SpendingGroupMapper.cs b/back/Infrastructure/Support/Mappers/SpendingGroupMapper.cs new file mode 100644 index 0000000..94a39b6 --- /dev/null +++ b/back/Infrastructure/Support/Mappers/SpendingGroupMapper.cs @@ -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 + }; +} \ No newline at end of file