From 6e4e47dc595688c31fac9a8be0a388622702996f Mon Sep 17 00:00:00 2001 From: mfnefd Date: Tue, 26 Nov 2024 00:15:31 +0400 Subject: [PATCH] =?UTF-8?q?add:=20=D1=80=D0=B5=D0=BF=D0=BE=D0=B7=D0=B8?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D0=B8=D0=B9=20=D0=B3=D1=80=D1=83=D0=BF=D0=BF?= =?UTF-8?q?=20=D1=80=D0=B0=D1=81=D1=85=D0=BE=D0=B4=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/Infrastructure/DatabaseContext.cs | 2 + back/Infrastructure/Models/SpendingGroup.cs | 10 ++ .../Repositories/SpendingGroupRepo.cs | 94 +++++++++++++++++++ .../Support/Mappers/SpendingGroupMapper.cs | 22 +++++ 4 files changed, 128 insertions(+) create mode 100644 back/Infrastructure/Models/SpendingGroup.cs create mode 100644 back/Infrastructure/Repositories/SpendingGroupRepo.cs create mode 100644 back/Infrastructure/Support/Mappers/SpendingGroupMapper.cs 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