From 0bd6261e21319d6001a898bc14fd4506f5ba92fd Mon Sep 17 00:00:00 2001 From: mfnefd Date: Wed, 27 Nov 2024 02:13:01 +0400 Subject: [PATCH] =?UTF-8?q?add:=20=D1=81=D0=B5=D1=80=D0=B2=D0=B8=D1=81=20?= =?UTF-8?q?=D0=B8=20=D1=80=D0=B5=D0=BF=D0=BE=D0=B7=D0=B8=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D0=B8=D0=B9=20=D0=BF=D0=BB=D0=B0=D0=BD=D0=B0=20=D1=80=D0=B0?= =?UTF-8?q?=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 --- .../Repositories/ISpendingPlanRepo.cs | 4 +- ...pendingPlan.cs => ISpendingPlanService.cs} | 6 +- back/Infrastructure/DatabaseContext.cs | 1 + back/Infrastructure/Models/SpendingPlan.cs | 22 +++++ .../Repositories/SpendingPlanRepo.cs | 86 +++++++++++++++++++ .../Support/Mappers/SpendingPlanMapper.cs | 27 ++++++ back/Services/Domain/SpendingPlanService.cs | 61 +++++++++++++ 7 files changed, 202 insertions(+), 5 deletions(-) rename back/Contracts/Services/{ISpendingPlan.cs => ISpendingPlanService.cs} (66%) create mode 100644 back/Infrastructure/Models/SpendingPlan.cs create mode 100644 back/Infrastructure/Repositories/SpendingPlanRepo.cs create mode 100644 back/Infrastructure/Support/Mappers/SpendingPlanMapper.cs create mode 100644 back/Services/Domain/SpendingPlanService.cs diff --git a/back/Contracts/Repositories/ISpendingPlanRepo.cs b/back/Contracts/Repositories/ISpendingPlanRepo.cs index 229bb7f..8dcfd4b 100644 --- a/back/Contracts/Repositories/ISpendingPlanRepo.cs +++ b/back/Contracts/Repositories/ISpendingPlanRepo.cs @@ -8,6 +8,6 @@ public interface ISpendingPlanRepo Task Create(SpendingPlanDto dto); Task Update(SpendingPlanDto dto); Task Delete(SpendingPlanSearch search); - Task GetDetails(SpendingPlanSearch search); - Task> GetList(SpendingGroupSearch? search = null); + Task Get(SpendingPlanSearch search); + Task> GetList(SpendingPlanSearch? search = null); } \ No newline at end of file diff --git a/back/Contracts/Services/ISpendingPlan.cs b/back/Contracts/Services/ISpendingPlanService.cs similarity index 66% rename from back/Contracts/Services/ISpendingPlan.cs rename to back/Contracts/Services/ISpendingPlanService.cs index 932c4ca..b2ee0aa 100644 --- a/back/Contracts/Services/ISpendingPlan.cs +++ b/back/Contracts/Services/ISpendingPlanService.cs @@ -4,11 +4,11 @@ using Contracts.ViewModels; namespace Contracts.Services; -public interface ISpendingPlan +public interface ISpendingPlanService { - Task GetDetails(SpendingPlanSearch search); + Task GetDetails(SpendingPlanSearch search); Task> GetList(SpendingPlanSearch? search = null); Task Create(SpendingPlanDto spendingPlan); - Task Delete(SpendingPlanSearch search); + Task Delete(SpendingPlanSearch search); Task Update(SpendingPlanDto spendingPlan); } \ No newline at end of file diff --git a/back/Infrastructure/DatabaseContext.cs b/back/Infrastructure/DatabaseContext.cs index 02afad7..d787750 100644 --- a/back/Infrastructure/DatabaseContext.cs +++ b/back/Infrastructure/DatabaseContext.cs @@ -14,4 +14,5 @@ public class DatabaseContext : DbContext public DbSet Users { get; set; } = null!; public DbSet SpendingGroups { get; set; } = null!; public DbSet ChangeRecords { get; set; } = null!; + public DbSet SpendingPlans { get; set; } = null!; } \ No newline at end of file diff --git a/back/Infrastructure/Models/SpendingPlan.cs b/back/Infrastructure/Models/SpendingPlan.cs new file mode 100644 index 0000000..8162016 --- /dev/null +++ b/back/Infrastructure/Models/SpendingPlan.cs @@ -0,0 +1,22 @@ +using Contracts.DTO; + +namespace Infrastructure.Models; + +public class SpendingPlan +{ + public Guid Id { get; set; } + public DateTime StartAt { get; set; } + public DateTime EndAt { get; set; } + public decimal Sum { get; set; } + + public Guid SpendingGroupId { get; set; } + public SpendingGroup SpendingGroup { get; set; } = null!; + + public void Update(SpendingPlanDto spendingPlan) + { + StartAt = spendingPlan.StartAt; + EndAt = spendingPlan.EndAt; + Sum = spendingPlan.Sum; + SpendingGroupId = spendingPlan.SpendingGroupId; + } +} \ No newline at end of file diff --git a/back/Infrastructure/Repositories/SpendingPlanRepo.cs b/back/Infrastructure/Repositories/SpendingPlanRepo.cs new file mode 100644 index 0000000..05c03f3 --- /dev/null +++ b/back/Infrastructure/Repositories/SpendingPlanRepo.cs @@ -0,0 +1,86 @@ +using Contracts.DTO; +using Contracts.Repositories; +using Contracts.SearchModels; +using Infrastructure.Support.Mappers; +using Microsoft.EntityFrameworkCore; + +namespace Infrastructure.Repositories; + +public class SpendingPlanRepo : ISpendingPlanRepo +{ + public readonly IDbContextFactory _factory; + + public SpendingPlanRepo(IDbContextFactory factory) + { + _factory = factory; + } + + public async Task Create(SpendingPlanDto dto) + { + using var context = _factory.CreateDbContext(); + + var plan = await context.SpendingPlans.AddAsync(dto.ToModel()); + + await context.SaveChangesAsync(); + return plan.Entity.ToDto(); + } + + public async Task Delete(SpendingPlanSearch search) + { + using var context = _factory.CreateDbContext(); + + var plan = await context.SpendingPlans.FirstOrDefaultAsync(x => x.Id == search.Id); + + if (plan == null) + { + return null; + } + + context.SpendingPlans.Remove(plan); + await context.SaveChangesAsync(); + return plan.ToDto(); + } + + public async Task Get(SpendingPlanSearch search) + { + using var context = _factory.CreateDbContext(); + + var plan = await context.SpendingPlans.FirstOrDefaultAsync(x => x.Id == search.Id); + + return plan?.ToDto(); + } + + public async Task> GetList(SpendingPlanSearch? search = null) + { + using var context = _factory.CreateDbContext(); + + var query = context.SpendingPlans.AsQueryable(); + + if (search != null) + { + if (search.Id != null) + { + query = query.Where(x => x.Id == search.Id); + } + } + + return await query.Select(x => x.ToDto()).ToListAsync(); + } + + public async Task Update(SpendingPlanDto dto) + { + using var context = _factory.CreateDbContext(); + + var plan = await context.SpendingPlans.FirstOrDefaultAsync(x => x.Id == dto.Id); + + if (plan == null) + { + return null; + } + + plan.Update(dto); + context.SpendingPlans.Update(plan); + await context.SaveChangesAsync(); + return plan.ToDto(); + } +} diff --git a/back/Infrastructure/Support/Mappers/SpendingPlanMapper.cs b/back/Infrastructure/Support/Mappers/SpendingPlanMapper.cs new file mode 100644 index 0000000..22fe884 --- /dev/null +++ b/back/Infrastructure/Support/Mappers/SpendingPlanMapper.cs @@ -0,0 +1,27 @@ +using Contracts.DTO; +using Infrastructure.Models; + +namespace Infrastructure.Support.Mappers; + +public static class SpendingPlanMapper +{ + public static SpendingPlan ToModel(this SpendingPlanDto plan) + => new() + { + Id = plan.Id, + StartAt = plan.StartAt, + EndAt = plan.EndAt, + Sum = plan.Sum, + SpendingGroupId = plan.SpendingGroupId + }; + + public static SpendingPlanDto ToDto(this SpendingPlan plan) + => new() + { + Id = plan.Id, + StartAt = plan.StartAt, + EndAt = plan.EndAt, + Sum = plan.Sum, + SpendingGroupId = plan.SpendingGroupId + }; +} \ No newline at end of file diff --git a/back/Services/Domain/SpendingPlanService.cs b/back/Services/Domain/SpendingPlanService.cs new file mode 100644 index 0000000..d565869 --- /dev/null +++ b/back/Services/Domain/SpendingPlanService.cs @@ -0,0 +1,61 @@ +using Contracts.DTO; +using Contracts.Mappers; +using Contracts.Repositories; +using Contracts.SearchModels; +using Contracts.Services; +using Contracts.ViewModels; +using Services.Support.Exceptions; + +namespace Services.Domain; + +public class SpendingPlanService : ISpendingPlanService +{ + private readonly ISpendingPlanRepo _spendingPlanRepo; + + public SpendingPlanService(ISpendingPlanRepo spendingPlanRepo) + { + _spendingPlanRepo = spendingPlanRepo; + } + + public async Task Create(SpendingPlanDto spendingPlan) + { + var plan = await _spendingPlanRepo.Create(spendingPlan); + return plan.ToView(); + } + + public async Task Delete(SpendingPlanSearch search) + { + var plan = await _spendingPlanRepo.Delete(search); + if (plan == null) + { + throw new EntityNotFoundException("При удалении не получилось найти план"); + } + return plan.ToView(); + } + + public async Task GetDetails(SpendingPlanSearch search) + { + var plan = await _spendingPlanRepo.Get(search); + if (plan == null) + { + throw new EntityNotFoundException("Не удалось найти план по таким параметрам"); + } + return plan.ToView(); + } + + public async Task> GetList(SpendingPlanSearch? search = null) + { + var plans = await _spendingPlanRepo.GetList(search); + return plans.Select(x => x.ToView()).ToList(); + } + + public async Task Update(SpendingPlanDto spendingPlan) + { + var plan = await _spendingPlanRepo.Update(spendingPlan); + if (plan == null) + { + throw new EntityNotFoundException("При обновлении не получилось найти план"); + } + return plan.ToView(); + } +}