From a2bcf9e35dcc3d9723bc565757f018cf89cac458 Mon Sep 17 00:00:00 2001 From: Whoisthatjulia Date: Sun, 28 Apr 2024 19:31:02 +0400 Subject: [PATCH] =?UTF-8?q?=D1=87=D1=82=D0=BE-=D1=82=D0=BE=20=D0=BD=D0=B0?= =?UTF-8?q?=D0=BC=D1=83=D1=82=D0=B8=D0=BB=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bank/BankDatabaseImplement/BankDB.cs | 2 +- .../Implements/CostStorage.cs | 96 ++++++++++++++- .../Implements/EmployeeStorage.cs | 41 ++++++- .../Implements/OperationStorage.cs | 111 +++++++++++++++++- Bank/BankDatabaseImplement/Models/Cost.cs | 81 ++++++++++++- Bank/BankDatabaseImplement/Models/Employee.cs | 62 ++++++++-- .../BankDatabaseImplement/Models/Operation.cs | 62 +++++++++- 7 files changed, 417 insertions(+), 38 deletions(-) diff --git a/Bank/BankDatabaseImplement/BankDB.cs b/Bank/BankDatabaseImplement/BankDB.cs index adfe519..fa00fa1 100644 --- a/Bank/BankDatabaseImplement/BankDB.cs +++ b/Bank/BankDatabaseImplement/BankDB.cs @@ -15,7 +15,7 @@ namespace BankDatabaseImplement Port=5432; Database=BankFullNew; Username=postgres; - Password=55256636a;"); + Password=postgres;"); } base.OnConfiguring(optionsBuilder); } diff --git a/Bank/BankDatabaseImplement/Implements/CostStorage.cs b/Bank/BankDatabaseImplement/Implements/CostStorage.cs index 9828d8c..b963c94 100644 --- a/Bank/BankDatabaseImplement/Implements/CostStorage.cs +++ b/Bank/BankDatabaseImplement/Implements/CostStorage.cs @@ -1,12 +1,96 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using BankContracts.BindingModels; +using BankContracts.SearchModels; +using BankContracts.StoragesContracts; +using BankContracts.ViewModels; +using BankDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; namespace BankDatabaseImplement.Implements { - internal class CostStorage + public class CostStorage : ICostStorage { + private void CheckSearchModel(CostSearchModel model) + { + if (model == null) + throw new ArgumentNullException("Передаваемая модель для поиска равна нулю", nameof(model)); + if (!model.Id.HasValue && !model.EmployeeId.HasValue) + throw new ArgumentException("Все передаваемые поля поисковой модели оказались пусты или равны null"); + } + public CostViewModel? Delete(CostBindingModel model) + { + using var context = new BankDB(); + var element = context.Costs.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + context.Costs.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public CostViewModel? GetElement(CostSearchModel model) + { + CheckSearchModel(model); + using var context = new BankDB(); + if (!model.Id.HasValue) + { + return null; + } + return context.Costs + .Include(cost => cost.Employee) + .Include(cost => cost.Purchases).ThenInclude(costByPurchase => costByPurchase.Purchase) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel; + } + + public List GetFilteredList(CostSearchModel model) + { + CheckSearchModel(model); + using var context = new BankDB(); + return context.Costs + .Include(cost => cost.Employee) + .Include(cost => cost.Purchases).ThenInclude(costByPurchase => costByPurchase.Purchase) + .Where(x => x.EmployeeId == model.EmployeeId || x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new BankDB(); + return context.Costs + .Include(cost => cost.Employee) + .Include(cost => cost.Purchases).ThenInclude(costByPurchase => costByPurchase.Purchase) + .Select(x => x.GetViewModel) + .ToList(); + } + + public CostViewModel? Insert(CostBindingModel model) + { + var newCost = Cost.Create(model); + if (newCost == null) + { + return null; + } + using var context = new BankDB(); + + context.Costs.Add(newCost); + context.SaveChanges(); + return newCost.GetViewModel; + } + public CostViewModel? Update(CostBindingModel model) + { + using var context = new BankDB(); + var cost = context.Costs.FirstOrDefault(x => x.Id == model.Id); + if (cost == null) + { + return null; + } + cost.Update(model); + context.SaveChanges(); + cost.UpdatePurchases(context, model); + context.SaveChanges(); + return cost.GetViewModel; + } } } diff --git a/Bank/BankDatabaseImplement/Implements/EmployeeStorage.cs b/Bank/BankDatabaseImplement/Implements/EmployeeStorage.cs index fda6e46..f27ca1a 100644 --- a/Bank/BankDatabaseImplement/Implements/EmployeeStorage.cs +++ b/Bank/BankDatabaseImplement/Implements/EmployeeStorage.cs @@ -1,12 +1,41 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using BankContracts.BindingModels; +using BankContracts.SearchModels; +using BankContracts.StoragesContracts; +using BankContracts.ViewModels; +using BankDatabaseImplement.Models; namespace BankDatabaseImplement.Implements { - internal class EmployeeStorage + public class EmployeeStorage : IEmployeeStorage { + private void CheckSearchModel(EmployeeSearchModel model) + { + if (model == null) + throw new ArgumentNullException("Передаваемая модель для поиска равна нулю", nameof(model)); + if (!model.Id.HasValue && string.IsNullOrEmpty(model.PhoneNumber) && string.IsNullOrEmpty(model.Password)) + throw new ArgumentException("Все передаваемые поля поисковой модели оказались пусты или равны null"); + if (!model.Id.HasValue && (string.IsNullOrEmpty(model.PhoneNumber) && !string.IsNullOrEmpty(model.Password))) + throw new ArgumentException("Для нахождения соответствующего пользователя вместе с паролем нужен логин"); + } + public EmployeeViewModel? GetElement(EmployeeSearchModel model) + { + CheckSearchModel(model); + using var context = new BankDB(); + + return context.Employees + .FirstOrDefault(x => x.PhoneNumber.Equals(model.PhoneNumber) && (string.IsNullOrEmpty(model.Password) || x.Password.Equals(model.Password)))?.GetViewModel; + } + public EmployeeViewModel? Insert(EmployeeBindingModel model) + { + if (model == null) + { + return null; + } + var newEmployee = Employee.Create(model); + using var context = new BankDB(); + context.Employees.Add(newEmployee); + context.SaveChanges(); + return newEmployee.GetViewModel; + } } } diff --git a/Bank/BankDatabaseImplement/Implements/OperationStorage.cs b/Bank/BankDatabaseImplement/Implements/OperationStorage.cs index 1918692..7925cc9 100644 --- a/Bank/BankDatabaseImplement/Implements/OperationStorage.cs +++ b/Bank/BankDatabaseImplement/Implements/OperationStorage.cs @@ -1,12 +1,111 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using BankContracts.BindingModels; +using BankContracts.SearchModels; +using BankContracts.StoragesContracts; +using BankContracts.ViewModels; +using BankDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; namespace BankDatabaseImplement.Implements { - internal class OperationStorage + public class OperationStorage : IOperationStorage { + private void CheckSearchModel(OperationSearchModel model) + { + if (model == null) + throw new ArgumentNullException("Передаваемая модель для поиска равна нулю", nameof(model)); + if (!model.Id.HasValue && !model.EmployeeId.HasValue && model.PurchasesIds == null) + throw new ArgumentException("Все передаваемые поля поисковой модели оказались пусты или равны null"); + } + + public OperationViewModel? Delete(OperationBindingModel model) + { + using var context = new BankDB(); + var element = context.Operations.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + context.Operations.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public OperationViewModel? GetElement(OperationSearchModel model) + { + CheckSearchModel(model); + if (!model.Id.HasValue) + { + return null; + } + using var context = new BankDB(); + return context.Operations + .Include(x => x.Employee) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel; + } + + public List GetFilteredList(OperationSearchModel model) + { + CheckSearchModel(model); + if (model.Id.HasValue) + { + var res = GetElement(model); + return res != null ? new() { res } : new(); + } + using var context = new BankDB(); + + var query = context.Operations.Include(x => x.Employee); + if (model.EmployeeId.HasValue) + { + + return query + .Where(x => model.EmployeeId == x.EmployeeId) + .Select(x => x.GetViewModel) + .ToList(); + } + + if (model.PurchasesIds != null) + return query + .Include(x => x.Purchases)! + .ThenInclude(x => x.Purchase) + .ThenInclude(x => x.Operations) + .Where(x => x.Purchases.Any(y => model.PurchasesIds.Contains(y.PurchaseId))) + .Select(x => x.GetViewModel) + .ToList(); + return new(); + } + + public List GetFullList() + { + using var context = new BankDB(); + return context.Operations.Include(x => x.Employee) + .Select(x => x.GetViewModel) + .ToList(); + } + + public OperationViewModel? Insert(OperationBindingModel model) + { + var newOperation = Operation.Create(model); + if (newOperation == null) + { + return null; + } + using var context = new BankDB(); + context.Operations.Add(newOperation); + context.SaveChanges(); + return newOperation.GetViewModel; + } + + public OperationViewModel? Update(OperationBindingModel model) + { + using var context = new BankDB(); + var car = context.Operations.FirstOrDefault(x => x.Id == model.Id); + if (car == null) + { + return null; + } + car.Update(model); + context.SaveChanges(); + return car.GetViewModel; + } } } diff --git a/Bank/BankDatabaseImplement/Models/Cost.cs b/Bank/BankDatabaseImplement/Models/Cost.cs index 4ab1fda..4d004e5 100644 --- a/Bank/BankDatabaseImplement/Models/Cost.cs +++ b/Bank/BankDatabaseImplement/Models/Cost.cs @@ -1,12 +1,81 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using BankContracts.BindingModels; +using BankContracts.ViewModels; +using BankDataModels; +using System.ComponentModel.DataAnnotations; +using BankDataModels.ProxyModels; namespace BankDatabaseImplement.Models { - internal class Cost + public class Cost : ICostModel { + [Required] + public int EmployeeId { get; private set; } + [Required] + public string NameOfCost { get; private set; } = string.Empty; + public int Id { get; private set; } + [Required] + public double Price { get; private set; } + private Dictionary? _cachedPurchases; + public Dictionary PurchasesModels => _cachedPurchases ??= Purchases.Select(x => (CostByPurchaseModel)x).ToDictionary(x => x.PurchaseId, x => x); + [Required] + public Employee? Employee { get; private set; } + [Required] + public List Purchases { get; private set; } = new(); + + public static Cost Create(CostBindingModel model) + { + if (model == null) + { + return null; + } + return new Cost() + { + EmployeeId = model.EmployeeId, + NameOfCost = model.NameOfCost, + Price = model.Price, + //PurchasesModels = model.PurchasesModels, + Id = model.Id + }; + } + + public CostViewModel GetViewModel => new() + { + PhoneNumber = Employee?.PhoneNumber ?? string.Empty, + PurchaseModels = PurchasesModels, + EmployeeId = EmployeeId, + NameOfCost = NameOfCost, + Price = Price, + Id = Id + }; + + public void Update(CostBindingModel model) + { + NameOfCost = model.NameOfCost; + Price = model.Price; + } + + /// + /// Привязка статей затрат к покупкам + /// + public void UpdatePurchases(BankDB context, CostBindingModel model) + { + var oldPurchases = context.CostByPurchases.Where(x => x.CostId == model.Id).ToDictionary(x => x.PurchaseId, x => x); + var newPurchases = model.PurchasesModels.ToDictionary( + x => x.Key, + x => new CostByPurchase() + { + CostId = model.Id, + PurchaseId = x.Key, + Count = x.Value.Count, + } + ); + context.RemoveRange(oldPurchases.Where(x => !newPurchases.ContainsKey(x.Key)).Select(x => x.Value)); + context.AddRange(newPurchases.Where(x => !oldPurchases.ContainsKey(x.Key)).Select(x => x.Value)); + oldPurchases.Where(x => newPurchases.ContainsKey(x.Key)) + .Select(x => x.Value).ToList() + .ForEach(x => x.Count = newPurchases[x.PurchaseId].Count); + context.SaveChanges(); + _cachedPurchases = null; + } } } diff --git a/Bank/BankDatabaseImplement/Models/Employee.cs b/Bank/BankDatabaseImplement/Models/Employee.cs index 5aafd89..3dd9259 100644 --- a/Bank/BankDatabaseImplement/Models/Employee.cs +++ b/Bank/BankDatabaseImplement/Models/Employee.cs @@ -1,12 +1,60 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using BankContracts.BindingModels; +using BankContracts.ViewModels; +using BankDataModels; +using System.ComponentModel.DataAnnotations; namespace BankDatabaseImplement.Models { - internal class Employee + public class Employee : IEmployeeModel { + public int Id { get; private set; } + [Required] + public string FirstName { get; private set; } = string.Empty; + [Required] + public string LastName { get; private set; } = string.Empty; + public string? MiddleName { get; private set; } + [Required] + public string Post { get; private set; } = string.Empty; + [Required] + public string PhoneNumber { get; private set; } = string.Empty; + [Required] + public string Password { get; private set; } = string.Empty; + [Required] + public string Email { get; set; } = string.Empty; + [Required] + public List? Operations { get; private set; } + [Required] + public List? Costs { get; private set; } + + public static Employee Create(EmployeeBindingModel model) + { + if (model == null) + { + return null; + } + return new Employee() + { + FirstName = model.FirstName, + LastName = model.LastName, + MiddleName = model.MiddleName, + PhoneNumber = model.PhoneNumber, + Password = model.Password, + Id = model.Id, + Post = model.Post, + Email = model.Email, + }; + } + + public EmployeeViewModel GetViewModel => new() + { + FirstName = FirstName, + LastName = LastName, + MiddleName = MiddleName, + PhoneNumber = PhoneNumber, + Password = Password, + Id = Id, + Post = Post, + Email = Email, + }; } -} +} \ No newline at end of file diff --git a/Bank/BankDatabaseImplement/Models/Operation.cs b/Bank/BankDatabaseImplement/Models/Operation.cs index 4e604f0..754a152 100644 --- a/Bank/BankDatabaseImplement/Models/Operation.cs +++ b/Bank/BankDatabaseImplement/Models/Operation.cs @@ -1,12 +1,62 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using BankContracts.BindingModels; +using BankContracts.SearchModels; +using BankContracts.ViewModels; +using BankDatabaseImplement.Implements; +using BankDataModels; +using BankDataModels.ProxyModels; +using System.ComponentModel; +using System.ComponentModel.DataAnnotations; namespace BankDatabaseImplement.Models { - internal class Operation + public class Operation : IOperationModel { + [Required] + public int EmployeeId { get; private set; } + [Required] + public string Model { get; private set; } = string.Empty; + [Required] + public double Price { get; private set; } + [Required] + public string Mark { get; private set; } = string.Empty; + + public int Id { get; private set; } + [Required] + public Employee? Employee { get; private set; } + + [Required] + public List Purchases { get; private set; } = new(); + + public static Operation Create(OperationBindingModel model) + { + if (model == null) + { + return null; + } + return new Operation() + { + EmployeeId = model.EmployeeId, + Model = model.Model, + Price = model.Price, + Mark = model.Mark, + Id = model.Id, + }; + } + + public OperationViewModel GetViewModel => new() + { + EmployeePhoneNumber = Employee?.PhoneNumber ?? string.Empty, + EmployeeId = EmployeeId, + Model = Model, + Price = Price, + Mark = Mark, + Id = Id, + Purchases = Purchases.Select(x => x.Purchase?.GetViewModel2).ToList(), + }; + + public void Update(OperationBindingModel model) + { + Price = model.Price; + } } }