From f66f89c461f5eaeb64b88e6afefe04068077332d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=BA=20=D0=98=D0=B3=D0=BE=D1=80=D1=8C?= Date: Thu, 6 Apr 2023 21:59:45 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BE=D1=81=D1=82=D0=B0=D0=B2=D1=88=D0=B8?= =?UTF-8?q?=D1=85=D1=81=D1=8F=20=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8?= =?UTF-8?q?=D1=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/CustomerStorage.cs | 79 +++++++++++++++++++ .../Implements/VehicleStorage.cs | 78 ++++++++++++++++++ .../Implements/WorkInRequestStorage.cs | 78 ++++++++++++++++++ .../Implements/WorkPaymentStorage.cs | 77 ++++++++++++++++++ 4 files changed, 312 insertions(+) create mode 100644 CarService/CarServiceDatabase/Implements/CustomerStorage.cs create mode 100644 CarService/CarServiceDatabase/Implements/VehicleStorage.cs create mode 100644 CarService/CarServiceDatabase/Implements/WorkInRequestStorage.cs create mode 100644 CarService/CarServiceDatabase/Implements/WorkPaymentStorage.cs diff --git a/CarService/CarServiceDatabase/Implements/CustomerStorage.cs b/CarService/CarServiceDatabase/Implements/CustomerStorage.cs new file mode 100644 index 0000000..47f5f3f --- /dev/null +++ b/CarService/CarServiceDatabase/Implements/CustomerStorage.cs @@ -0,0 +1,79 @@ +using CarServiceContracts.BindingModels; +using CarServiceContracts.SearchModels; +using CarServiceContracts.StorageContracts; +using CarServiceContracts.ViewModels; +using CarServiceDatabase.Models; + +namespace CarServiceDatabase.Implements +{ + public class CustomerStorage : ICustomerStorage + { + public List GetFullList() + { + using var context = new CarServiceDbContext(); + return context.Customers + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(CustomerSearchModel model) + { + using var context = new CarServiceDbContext(); + return context.Customers + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + public CustomerViewModel? GetElement(CustomerSearchModel model) + { + if (model == null) + { + return null; + } + using var context = new CarServiceDbContext(); + if (model.Id.HasValue)//ищем по Id + { + return context.Customers.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + //другие варианты поиска не реализуются (заглушка для роли клиента) + return null; + } + public CustomerViewModel? Insert(CustomerBindingModel model) + { + using var context = new CarServiceDbContext(); + var newCustomer = Customer.Create(model); + if (newCustomer != null) + { + context.Customers.Add(newCustomer); + context.SaveChanges(); + return newCustomer.GetViewModel; + } + return null; + } + public CustomerViewModel? Update(CustomerBindingModel model) + { + using var context = new CarServiceDbContext(); + var customer = context.Customers + .FirstOrDefault(x => x.Id == model.Id); + if (customer == null) + { + return null; + } + customer.Update(model); + context.SaveChanges(); + return customer.GetViewModel; + } + public CustomerViewModel? Delete(CustomerBindingModel model) + { + using var context = new CarServiceDbContext(); + var customer = context.Customers + .FirstOrDefault(x => x.Id == model.Id); + if (customer == null) + { + return null; + } + context.Customers.Remove(customer); + context.SaveChanges(); + return customer.GetViewModel; + } + } +} diff --git a/CarService/CarServiceDatabase/Implements/VehicleStorage.cs b/CarService/CarServiceDatabase/Implements/VehicleStorage.cs new file mode 100644 index 0000000..052ae49 --- /dev/null +++ b/CarService/CarServiceDatabase/Implements/VehicleStorage.cs @@ -0,0 +1,78 @@ +using CarServiceContracts.BindingModels; +using CarServiceContracts.SearchModels; +using CarServiceContracts.StorageContracts; +using CarServiceContracts.ViewModels; +using CarServiceDatabase.Models; + +namespace CarServiceDatabase.Implements +{ + public class VehicleStorage : IVehicleStorage + { + public List GetFullList() + { + using var context = new CarServiceDbContext(); + return context.Vehicles + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(VehicleSearchModel model) + { + using var context = new CarServiceDbContext(); + return context.Vehicles + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + public VehicleViewModel? GetElement(VehicleSearchModel model) + { + if (model == null) + { + return null; + } + using var context = new CarServiceDbContext(); + if (model.Id.HasValue)//ищем по Id + { + return context.Vehicles.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + return null; + } + public VehicleViewModel? Insert(VehicleBindingModel model) + { + using var context = new CarServiceDbContext(); + var newVehicle = Vehicle.Create(model); + if (newVehicle != null) + { + context.Vehicles.Add(newVehicle); + context.SaveChanges(); + return newVehicle.GetViewModel; + } + return null; + } + public VehicleViewModel? Update(VehicleBindingModel model) + { + using var context = new CarServiceDbContext(); + var vehicle = context.Vehicles + .FirstOrDefault(x => x.Id == model.Id); + if (vehicle == null) + { + return null; + } + vehicle.Update(model); + context.SaveChanges(); + return vehicle.GetViewModel; + } + public VehicleViewModel? Delete(VehicleBindingModel model) + { + using var context = new CarServiceDbContext(); + var vehicle = context.Vehicles + .FirstOrDefault(x => x.Id == model.Id); + if (vehicle == null) + { + return null; + } + context.Vehicles.Remove(vehicle); + context.SaveChanges(); + return vehicle.GetViewModel; + } + } +} diff --git a/CarService/CarServiceDatabase/Implements/WorkInRequestStorage.cs b/CarService/CarServiceDatabase/Implements/WorkInRequestStorage.cs new file mode 100644 index 0000000..d066068 --- /dev/null +++ b/CarService/CarServiceDatabase/Implements/WorkInRequestStorage.cs @@ -0,0 +1,78 @@ +using CarServiceContracts.BindingModels; +using CarServiceContracts.SearchModels; +using CarServiceContracts.StorageContracts; +using CarServiceContracts.ViewModels; +using CarServiceDatabase.Models; +using Microsoft.EntityFrameworkCore; + +namespace CarServiceDatabase.Implements +{ + public class WorkInRequestStorage : IWorkInRequestStorage + { + public List GetFullList() + { + using var context = new CarServiceDbContext(); + return context.WorksInRequest + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(WorkInRequestSearchModel model) + { + using var context = new CarServiceDbContext(); + return context.WorksInRequest + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + public WorkInRequestViewModel? GetElement(WorkInRequestSearchModel model) + { + if (model == null) + { + return null; + } + using var context = new CarServiceDbContext(); + if (model.Id.HasValue) + { + return context.WorksInRequest + .FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + return null; + } + public WorkInRequestViewModel? Insert(WorkInRequestBindingModel model) + { + using var context = new CarServiceDbContext(); + var newWorkInRequest = WorkInRequest.Create(model); + if (newWorkInRequest != null) + { + context.WorksInRequest.Add(newWorkInRequest); + context.SaveChanges(); + return newWorkInRequest.GetViewModel; + } + return null; + } + public WorkInRequestViewModel? Update(WorkInRequestBindingModel model) + { + using var context = new CarServiceDbContext(); + var workInRequest = context.WorksInRequest.FirstOrDefault(x => x.Id == model.Id); + if (workInRequest == null) + { + return null; + } + workInRequest.Update(model); + context.SaveChanges(); + return workInRequest.GetViewModel; + } + public WorkInRequestViewModel? Delete(WorkInRequestBindingModel model) + { + using var context = new CarServiceDbContext(); + var workInRequest = context.WorksInRequest.FirstOrDefault(x => x.Id == model.Id); + if (workInRequest == null) + { + return null; + } + context.WorksInRequest.Remove(workInRequest); + context.SaveChanges(); + return workInRequest.GetViewModel; + } + } +} diff --git a/CarService/CarServiceDatabase/Implements/WorkPaymentStorage.cs b/CarService/CarServiceDatabase/Implements/WorkPaymentStorage.cs new file mode 100644 index 0000000..953b47a --- /dev/null +++ b/CarService/CarServiceDatabase/Implements/WorkPaymentStorage.cs @@ -0,0 +1,77 @@ +using CarServiceContracts.BindingModels; +using CarServiceContracts.SearchModels; +using CarServiceContracts.StorageContracts; +using CarServiceContracts.ViewModels; +using CarServiceDatabase.Models; + +namespace CarServiceDatabase.Implements +{ + public class WorkPaymentStorage : IWorkPaymentStorage + { + public List GetFullList() + { + using var context = new CarServiceDbContext(); + return context.WorkPayments + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(WorkPaymentSearchModel model) + { + using var context = new CarServiceDbContext(); + return context.WorkPayments + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + public WorkPaymentViewModel? GetElement(WorkPaymentSearchModel model) + { + if (model == null) + { + return null; + } + using var context = new CarServiceDbContext(); + if (model.Id.HasValue) + { + return context.WorkPayments + .FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + return null; + } + public WorkPaymentViewModel? Insert(WorkPaymentBindingModel model) + { + using var context = new CarServiceDbContext(); + var newWorkPayment = WorkPayment.Create(model); + if (newWorkPayment != null) + { + context.WorkPayments.Add(newWorkPayment); + context.SaveChanges(); + return newWorkPayment.GetViewModel; + } + return null; + } + public WorkPaymentViewModel? Update(WorkPaymentBindingModel model) + { + using var context = new CarServiceDbContext(); + var workPayment = context.WorkPayments.FirstOrDefault(x => x.Id == model.Id); + if (workPayment == null) + { + return null; + } + workPayment.Update(model); + context.SaveChanges(); + return workPayment.GetViewModel; + } + public WorkPaymentViewModel? Delete(WorkPaymentBindingModel model) + { + using var context = new CarServiceDbContext(); + var workPayment = context.WorkPayments.FirstOrDefault(x => x.Id == model.Id); + if (workPayment == null) + { + return null; + } + context.WorkPayments.Remove(workPayment); + context.SaveChanges(); + return workPayment.GetViewModel; + } + } +}