From c90a62cdda9d8c052eed509c3835a2e9ffe9ebfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=9A=D1=83=D0=BA?= =?UTF-8?q?=D0=BB=D0=B5=D0=B2?= Date: Tue, 30 Apr 2024 19:10:25 +0400 Subject: [PATCH] =?UTF-8?q?=D1=8F=20=D0=B5=D0=B5=20=D0=B2=D1=81=D0=B5,=20?= =?UTF-8?q?=D0=BE=D0=BD=D0=B0=20=D0=BF=D0=B0=D0=BD=D1=82=D0=B5=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/ClientStorage.cs | 109 ++++++++++++++++++ .../Implements/RequirementStorage.cs | 95 +++++++++++++++ 2 files changed, 204 insertions(+) create mode 100644 CarCenterDatabaseImplement/Implements/ClientStorage.cs create mode 100644 CarCenterDatabaseImplement/Implements/RequirementStorage.cs diff --git a/CarCenterDatabaseImplement/Implements/ClientStorage.cs b/CarCenterDatabaseImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..85e8c78 --- /dev/null +++ b/CarCenterDatabaseImplement/Implements/ClientStorage.cs @@ -0,0 +1,109 @@ +using System.Security.Cryptography.X509Certificates; +using CarCenterContracts.BindingModels; +using CarCenterContracts.SearchModels; +using CarCenterContracts.StoragesContracts; +using CarCenterContracts.ViewModels; +using CarCenterDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace CarCenterDatabaseImplement.Implements +{ + public class ClientStorage : IClientStorage + { + private void CheckSearchModel(ClientSearchModel model) + { + if (model == null) + throw new ArgumentNullException("Передаваемая модель для поиска равна нулю", nameof(model)); + if (!model.Id.HasValue && !model.DirectorId.HasValue && model.CarsIds == null) + throw new ArgumentException("Все передаваемые поля поисковой модели оказались пусты или равны null"); + } + + public ClientViewModel? Delete(ClientBindingModel model) + { + using var context = new CarCenterDb(); + var element = context.Clients.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + context.Clients.Remove(element); + context.SaveChanges(); + return element; + } + return null; + } + + public ClientViewModel? GetElement(ClientSearchModel model) + { + CheckSearchModel(model); + if (!model.Id.HasValue) + { + return null; + } + using var context = new CarCenterDb(); + return context.Clients + .Include(x => x.Director) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id); + } + + public List GetFilteredList(ClientSearchModel model) + { + CheckSearchModel(model); + if (model.Id.HasValue) + { + var res = GetElement(model); + return res != null ? new() { res } : new(); + } + using var context = new CarCenterDb(); + var query = context.Clients.Include(x => x.Director); + if (model.DirectorId.HasValue) + { + return query + .Where(x => model.DirectorId == x.DirectorId) + .Select(x => (ClientViewModel)x) + .ToList(); + } + if (model.CarsIds != null) + return query + .Include(x => x.Cars)! + .ThenInclude(x => x.Car) + .ThenInclude(x => x.Client) + .Where(x => x.Cars.Any(y => model.CarsIds.Contains(y.CarId))) + .Select(x => (ClientViewModel)x) + .ToList(); + return new(); + } + + public List GetFullList() + { + using var context = new CarCenterDb(); + return context.Clients.Include(x => x.Director) + .Select(x => (ClientViewModel)x) + .ToList(); + } + + public ClientViewModel? Insert(ClientBindingModel model) + { + var newClient = Client.Create(model); + if (newClient == null) + { + return null; + } + using var context = new CarCenterDb(); + context.Clients.Add(newClient); + context.SaveChanges(); + return newClient; + } + + public ClientViewModel? Update(ClientBindingModel model) + { + using var context = new CarCenterDb(); + var Client = context.Clients.FirstOrDefault(x => x.Id == model.Id); + if (Client == null) + { + return null; + } + Client.Update(model); + context.SaveChanges(); + return Client; + } + } +} diff --git a/CarCenterDatabaseImplement/Implements/RequirementStorage.cs b/CarCenterDatabaseImplement/Implements/RequirementStorage.cs new file mode 100644 index 0000000..c920673 --- /dev/null +++ b/CarCenterDatabaseImplement/Implements/RequirementStorage.cs @@ -0,0 +1,95 @@ +using CarCenterContracts.BindingModels; +using CarCenterContracts.SearchModels; +using CarCenterContracts.StoragesContracts; +using CarCenterContracts.ViewModels; +using CarCenterDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace CarCenterDatabaseImplement.Implements +{ + public class RequirementStorage : IRequirementStorage + { + private void CheckSearchModel(RequirementSearchModel model) + { + if (model == null) + throw new ArgumentNullException("Передаваемая модель для поиска равна нулю", nameof(model)); + if (!model.Id.HasValue && !model.DirectorId.HasValue) + throw new ArgumentException("Все передаваемые поля поисковой модели оказались пусты или равны null"); + } + public RequirementViewModel? Delete(RequirementBindingModel model) + { + using var context = new CarCenterDb(); + var element = context.Requirements.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + context.Requirements.Remove(element); + context.SaveChanges(); + return element; + } + return null; + } + + public RequirementViewModel? GetElement(RequirementSearchModel model) + { + CheckSearchModel(model); + using var context = new CarCenterDb(); + if (!model.Id.HasValue) + { + return null; + } + return context.Requirements + .Include(requirement => requirement.Director) + .Include(requirement => requirement.Cars).ThenInclude(requirementByCar => requirementByCar.Car) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id); + } + + public List GetFilteredList(RequirementSearchModel model) + { + CheckSearchModel(model); + using var context = new CarCenterDb(); + return context.Requirements + .Include(requirement => requirement.Director) + .Include(requirement => requirement.Cars).ThenInclude(requirementByCar => requirementByCar.Car) + .Where(x => x.DirectorId == model.DirectorId) + .Select(x => (RequirementViewModel)x) + .ToList(); + } + + public List GetFullList() + { + using var context = new CarCenterDb(); + return context.Requirements + .Include(requirement => requirement.Director) + .Include(requirement => requirement.Cars).ThenInclude(requirementByCar => requirementByCar.Car) + .Select(x => (RequirementViewModel)x) + .ToList(); + } + + public RequirementViewModel? Insert(RequirementBindingModel model) + { + var newRequirement = Requirement.Create(model); + if (newRequirement == null) + { + return null; + } + using var context = new CarCenterDb(); + context.Requirements.Add(newRequirement); + context.SaveChanges(); + return newRequirement; + } + public RequirementViewModel? Update(RequirementBindingModel model) + { + using var context = new CarCenterDb(); + var requirement = context.Requirements.FirstOrDefault(x => x.Id == model.Id); + if (requirement == null) + { + return null; + } + requirement.Update(model); + context.SaveChanges(); + requirement.UpdateCars(context, model); + context.SaveChanges(); + return requirement; + } + } +}