From 3e9619c7c4a9b6305865b39f13660ef2cd62918e Mon Sep 17 00:00:00 2001 From: Zakharov_Rostislav Date: Sun, 5 May 2024 22:58:29 +0400 Subject: [PATCH] add storages --- .../Entities/Car.cs | 2 +- .../Entities/Client.cs | 2 +- .../Entities/Employee.cs | 2 +- .../Entities/Model.cs | 2 +- .../Entities/Sale.cs | 6 +- .../Entities/Service.cs | 2 +- .../Storages/CarStorage.cs | 88 +++++++++++ .../Storages/ClientStorage.cs | 88 +++++++++++ .../Storages/EmployeeStorage.cs | 98 ++++++++++++ .../Storages/IServiceStorage.cs | 88 +++++++++++ .../Storages/MakeStorage.cs | 88 +++++++++++ .../Storages/ModelStorage.cs | 88 +++++++++++ .../Storages/SaleStorage.cs | 145 ++++++++++++++++++ 13 files changed, 691 insertions(+), 8 deletions(-) create mode 100644 CarShowroom/CarShowroomDatabaseStorage/Storages/CarStorage.cs create mode 100644 CarShowroom/CarShowroomDatabaseStorage/Storages/ClientStorage.cs create mode 100644 CarShowroom/CarShowroomDatabaseStorage/Storages/EmployeeStorage.cs create mode 100644 CarShowroom/CarShowroomDatabaseStorage/Storages/IServiceStorage.cs create mode 100644 CarShowroom/CarShowroomDatabaseStorage/Storages/MakeStorage.cs create mode 100644 CarShowroom/CarShowroomDatabaseStorage/Storages/ModelStorage.cs create mode 100644 CarShowroom/CarShowroomDatabaseStorage/Storages/SaleStorage.cs diff --git a/CarShowroom/CarShowroomDatabaseStorage/Entities/Car.cs b/CarShowroom/CarShowroomDatabaseStorage/Entities/Car.cs index 46bd081..cd64a1c 100644 --- a/CarShowroom/CarShowroomDatabaseStorage/Entities/Car.cs +++ b/CarShowroom/CarShowroomDatabaseStorage/Entities/Car.cs @@ -54,7 +54,7 @@ namespace CarShowroomDatabaseStorage.Entities ModelId = car.ModelId; } - public CarView GetCarView() + public CarView GetView() { CarView car = new CarView(this); car.ModelPrice = Model?.Price ?? 0; diff --git a/CarShowroom/CarShowroomDatabaseStorage/Entities/Client.cs b/CarShowroom/CarShowroomDatabaseStorage/Entities/Client.cs index 75536d0..e00b494 100644 --- a/CarShowroom/CarShowroomDatabaseStorage/Entities/Client.cs +++ b/CarShowroom/CarShowroomDatabaseStorage/Entities/Client.cs @@ -52,7 +52,7 @@ namespace CarShowroomDatabaseStorage.Entities PhoneNumber = client.PhoneNumber; } - public ClientView GetClientView() + public ClientView GetView() { return new ClientView(this); } diff --git a/CarShowroom/CarShowroomDatabaseStorage/Entities/Employee.cs b/CarShowroom/CarShowroomDatabaseStorage/Entities/Employee.cs index 0ad9736..5c60497 100644 --- a/CarShowroom/CarShowroomDatabaseStorage/Entities/Employee.cs +++ b/CarShowroom/CarShowroomDatabaseStorage/Entities/Employee.cs @@ -57,7 +57,7 @@ namespace CarShowroomDatabaseStorage.Entities Password = employee.Password; } - public EmployeeView GetEmployeeView() + public EmployeeView GetView() { return new EmployeeView(this); } diff --git a/CarShowroom/CarShowroomDatabaseStorage/Entities/Model.cs b/CarShowroom/CarShowroomDatabaseStorage/Entities/Model.cs index 2c3834a..c3d1b64 100644 --- a/CarShowroom/CarShowroomDatabaseStorage/Entities/Model.cs +++ b/CarShowroom/CarShowroomDatabaseStorage/Entities/Model.cs @@ -56,7 +56,7 @@ namespace CarShowroomDatabaseStorage.Entities MakeId = model.MakeId; } - public ModelView GetModelView() + public ModelView GetView() { ModelView model = new ModelView(this); model.MakeName = Make?.Name ?? string.Empty; diff --git a/CarShowroom/CarShowroomDatabaseStorage/Entities/Sale.cs b/CarShowroom/CarShowroomDatabaseStorage/Entities/Sale.cs index 34ea46a..a1ae884 100644 --- a/CarShowroom/CarShowroomDatabaseStorage/Entities/Sale.cs +++ b/CarShowroom/CarShowroomDatabaseStorage/Entities/Sale.cs @@ -78,13 +78,13 @@ namespace CarShowroomDatabaseStorage.Entities EmployeeId = sale.EmployeeId; } - public SaleView GetSaleView() + public SaleView GetView() { SaleView sale = new SaleView(this); sale.ClientName = Client?.Name ?? string.Empty; sale.EmployeeName = Employee?.Name ?? string.Empty; - sale.Services = SaleServices.Select(s => s.Service.GetServiceView()).ToList(); - sale.Cars = SaleCars.Select(c => c.Car.GetCarView()).ToList(); + sale.Services = SaleServices.Select(s => s.Service.GetView()).ToList(); + sale.Cars = SaleCars.Select(c => c.Car.GetView()).ToList(); return sale; } } diff --git a/CarShowroom/CarShowroomDatabaseStorage/Entities/Service.cs b/CarShowroom/CarShowroomDatabaseStorage/Entities/Service.cs index b4aee0c..bdb5ac8 100644 --- a/CarShowroom/CarShowroomDatabaseStorage/Entities/Service.cs +++ b/CarShowroom/CarShowroomDatabaseStorage/Entities/Service.cs @@ -51,7 +51,7 @@ namespace CarShowroomDatabaseStorage.Entities Cost = service.Cost; } - public ServiceView GetServiceView() + public ServiceView GetView() { return new ServiceView(this); } diff --git a/CarShowroom/CarShowroomDatabaseStorage/Storages/CarStorage.cs b/CarShowroom/CarShowroomDatabaseStorage/Storages/CarStorage.cs new file mode 100644 index 0000000..77a256e --- /dev/null +++ b/CarShowroom/CarShowroomDatabaseStorage/Storages/CarStorage.cs @@ -0,0 +1,88 @@ +using CarShowroomContracts.StorageContracts; +using CarShowroomDatabaseStorage.Entities; +using CarShowroomDataModels.Dtos; +using CarShowroomDataModels.SearchModel; +using CarShowroomDataModels.Views; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarShowroomDatabaseStorage.Storages +{ + public class CarStorage : ICarStorage + { + public List GetFullList() + { + using var context = new CarShowroomDatabase(); + return context.Cars + .Select(x => x.GetView()) + .ToList(); + } + + public List GetFilteredList(CarSearch model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new CarShowroomDatabase(); + return context.Cars + .Where(x => !model.Id.HasValue || x.Id == model.Id) + .Select(x => x.GetView()) + .ToList(); + } + + public CarView? GetElement(CarSearch model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new CarShowroomDatabase(); + return context.Cars + .FirstOrDefault(x => !model.Id.HasValue || x.Id == model.Id) + ?.GetView(); + } + + public CarView? Insert(CarDto model) + { + var newCar = Car.Create(model); + if (newCar == null) + { + return null; + } + using var context = new CarShowroomDatabase(); + context.Cars.Add(newCar); + context.SaveChanges(); + return newCar.GetView(); + } + + public CarView? Update(CarDto model) + { + using var context = new CarShowroomDatabase(); + var car = context.Cars.FirstOrDefault(x => x.Id == model.Id); + if (car == null) + { + return null; + } + car.Update(model); + context.SaveChanges(); + return car.GetView(); + } + + public CarView? Delete(CarDto model) + { + using var context = new CarShowroomDatabase(); + var element = context.Cars.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Cars.Remove(element); + context.SaveChanges(); + return element.GetView(); + } + return null; + } + } +} diff --git a/CarShowroom/CarShowroomDatabaseStorage/Storages/ClientStorage.cs b/CarShowroom/CarShowroomDatabaseStorage/Storages/ClientStorage.cs new file mode 100644 index 0000000..8550da9 --- /dev/null +++ b/CarShowroom/CarShowroomDatabaseStorage/Storages/ClientStorage.cs @@ -0,0 +1,88 @@ +using CarShowroomContracts.StorageContracts; +using CarShowroomDatabaseStorage.Entities; +using CarShowroomDataModels.Dtos; +using CarShowroomDataModels.SearchModel; +using CarShowroomDataModels.Views; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarShowroomDatabaseStorage.Storages +{ + public class ClientStorage : IClientStorage + { + public List GetFullList() + { + using var context = new CarShowroomDatabase(); + return context.Clients + .Select(x => x.GetView()) + .ToList(); + } + + public List GetFilteredList(ClientSearch model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new CarShowroomDatabase(); + return context.Clients + .Where(x => !model.Id.HasValue || x.Id == model.Id) + .Select(x => x.GetView()) + .ToList(); + } + + public ClientView? GetElement(ClientSearch model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new CarShowroomDatabase(); + return context.Clients + .FirstOrDefault(x => !model.Id.HasValue || x.Id == model.Id) + ?.GetView(); + } + + public ClientView? Insert(ClientDto model) + { + var newClient = Client.Create(model); + if (newClient == null) + { + return null; + } + using var context = new CarShowroomDatabase(); + context.Clients.Add(newClient); + context.SaveChanges(); + return newClient.GetView(); + } + + public ClientView? Update(ClientDto model) + { + using var context = new CarShowroomDatabase(); + var client = context.Clients.FirstOrDefault(x => x.Id == model.Id); + if (client == null) + { + return null; + } + client.Update(model); + context.SaveChanges(); + return client.GetView(); + } + + public ClientView? Delete(ClientDto model) + { + using var context = new CarShowroomDatabase(); + var element = context.Clients.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Clients.Remove(element); + context.SaveChanges(); + return element.GetView(); + } + return null; + } + } +} diff --git a/CarShowroom/CarShowroomDatabaseStorage/Storages/EmployeeStorage.cs b/CarShowroom/CarShowroomDatabaseStorage/Storages/EmployeeStorage.cs new file mode 100644 index 0000000..ed2b16e --- /dev/null +++ b/CarShowroom/CarShowroomDatabaseStorage/Storages/EmployeeStorage.cs @@ -0,0 +1,98 @@ +using CarShowroomContracts.StorageContracts; +using CarShowroomDatabaseStorage.Entities; +using CarShowroomDataModels.Dtos; +using CarShowroomDataModels.SearchModel; +using CarShowroomDataModels.Views; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarShowroomDatabaseStorage.Storages +{ + public class EmployeeStorage : IEmployeeStorage + { + public List GetFullList() + { + using var context = new CarShowroomDatabase(); + return context.Employees + .Select(x => x.GetView()) + .ToList(); + } + + public List GetFilteredList(EmployeeSearch model) + { + if (!model.Id.HasValue && + string.IsNullOrEmpty(model.Password) && + string.IsNullOrEmpty(model.Email)) + { + return new(); + } + using var context = new CarShowroomDatabase(); + return context.Employees + .Where(x => + !model.Id.HasValue || x.Id == model.Id && + (string.IsNullOrEmpty(model.Password) || x.Password == model.Password) && + (string.IsNullOrEmpty(model.Email) || x.Password == model.Email)) + .Select(x => x.GetView()) + .ToList(); + } + + public EmployeeView? GetElement(EmployeeSearch model) + { + if (!model.Id.HasValue && + string.IsNullOrEmpty(model.Password) && + string.IsNullOrEmpty(model.Email)) + { + return null; + } + using var context = new CarShowroomDatabase(); + return context.Employees + .FirstOrDefault(x => + !model.Id.HasValue || x.Id == model.Id && + (string.IsNullOrEmpty(model.Password) || x.Password == model.Password) && + (string.IsNullOrEmpty(model.Email) || x.Password == model.Email)) + ?.GetView(); + } + + public EmployeeView? Insert(EmployeeDto model) + { + var newEmployee = Employee.Create(model); + if (newEmployee == null) + { + return null; + } + using var context = new CarShowroomDatabase(); + context.Employees.Add(newEmployee); + context.SaveChanges(); + return newEmployee.GetView(); + } + + public EmployeeView? Update(EmployeeDto model) + { + using var context = new CarShowroomDatabase(); + var employee = context.Employees.FirstOrDefault(x => x.Id == model.Id); + if (employee == null) + { + return null; + } + employee.Update(model); + context.SaveChanges(); + return employee.GetView(); + } + + public EmployeeView? Delete(EmployeeDto model) + { + using var context = new CarShowroomDatabase(); + var element = context.Employees.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Employees.Remove(element); + context.SaveChanges(); + return element.GetView(); + } + return null; + } + } +} diff --git a/CarShowroom/CarShowroomDatabaseStorage/Storages/IServiceStorage.cs b/CarShowroom/CarShowroomDatabaseStorage/Storages/IServiceStorage.cs new file mode 100644 index 0000000..4dbd1fb --- /dev/null +++ b/CarShowroom/CarShowroomDatabaseStorage/Storages/IServiceStorage.cs @@ -0,0 +1,88 @@ +using CarShowroomContracts.StorageContracts; +using CarShowroomDatabaseStorage.Entities; +using CarShowroomDataModels.Dtos; +using CarShowroomDataModels.SearchModel; +using CarShowroomDataModels.Views; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarShowroomDatabaseStorage.Storages +{ + public class ServiceStorage : IServiceStorage + { + public List GetFullList() + { + using var context = new CarShowroomDatabase(); + return context.Services + .Select(x => x.GetView()) + .ToList(); + } + + public List GetFilteredList(ServiceSearch model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new CarShowroomDatabase(); + return context.Services + .Where(x => !model.Id.HasValue || x.Id == model.Id) + .Select(x => x.GetView()) + .ToList(); + } + + public ServiceView? GetElement(ServiceSearch model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new CarShowroomDatabase(); + return context.Services + .FirstOrDefault(x => !model.Id.HasValue || x.Id == model.Id) + ?.GetView(); + } + + public ServiceView? Insert(ServiceDto model) + { + var newService = Service.Create(model); + if (newService == null) + { + return null; + } + using var context = new CarShowroomDatabase(); + context.Services.Add(newService); + context.SaveChanges(); + return newService.GetView(); + } + + public ServiceView? Update(ServiceDto model) + { + using var context = new CarShowroomDatabase(); + var service = context.Services.FirstOrDefault(x => x.Id == model.Id); + if (service == null) + { + return null; + } + service.Update(model); + context.SaveChanges(); + return service.GetView(); + } + + public ServiceView? Delete(ServiceDto model) + { + using var context = new CarShowroomDatabase(); + var element = context.Services.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Services.Remove(element); + context.SaveChanges(); + return element.GetView(); + } + return null; + } + } +} diff --git a/CarShowroom/CarShowroomDatabaseStorage/Storages/MakeStorage.cs b/CarShowroom/CarShowroomDatabaseStorage/Storages/MakeStorage.cs new file mode 100644 index 0000000..e0450f0 --- /dev/null +++ b/CarShowroom/CarShowroomDatabaseStorage/Storages/MakeStorage.cs @@ -0,0 +1,88 @@ +using CarShowroomContracts.StorageContracts; +using CarShowroomDatabaseStorage.Entities; +using CarShowroomDataModels.Dtos; +using CarShowroomDataModels.SearchModel; +using CarShowroomDataModels.Views; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarShowroomDatabaseStorage.Storages +{ + public class MakeStorage : IMakeStorage + { + public List GetFullList() + { + using var context = new CarShowroomDatabase(); + return context.Makes + .Select(x => x.GetView()) + .ToList(); + } + + public List GetFilteredList(MakeSearch model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new CarShowroomDatabase(); + return context.Makes + .Where(x => !model.Id.HasValue || x.Id == model.Id) + .Select(x => x.GetView()) + .ToList(); + } + + public MakeView? GetElement(MakeSearch model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new CarShowroomDatabase(); + return context.Makes + .FirstOrDefault(x => !model.Id.HasValue || x.Id == model.Id) + ?.GetView(); + } + + public MakeView? Insert(MakeDto model) + { + var newMake = Make.Create(model); + if (newMake == null) + { + return null; + } + using var context = new CarShowroomDatabase(); + context.Makes.Add(newMake); + context.SaveChanges(); + return newMake.GetView(); + } + + public MakeView? Update(MakeDto model) + { + using var context = new CarShowroomDatabase(); + var make = context.Makes.FirstOrDefault(x => x.Id == model.Id); + if (make == null) + { + return null; + } + make.Update(model); + context.SaveChanges(); + return make.GetView(); + } + + public MakeView? Delete(MakeDto model) + { + using var context = new CarShowroomDatabase(); + var element = context.Makes.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Makes.Remove(element); + context.SaveChanges(); + return element.GetView(); + } + return null; + } + } +} diff --git a/CarShowroom/CarShowroomDatabaseStorage/Storages/ModelStorage.cs b/CarShowroom/CarShowroomDatabaseStorage/Storages/ModelStorage.cs new file mode 100644 index 0000000..bbccf4c --- /dev/null +++ b/CarShowroom/CarShowroomDatabaseStorage/Storages/ModelStorage.cs @@ -0,0 +1,88 @@ +using CarShowroomContracts.StorageContracts; +using CarShowroomDatabaseStorage.Entities; +using CarShowroomDataModels.Dtos; +using CarShowroomDataModels.SearchModel; +using CarShowroomDataModels.Views; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarShowroomDatabaseStorage.Storages +{ + public class ModelStorage : IModelStorage + { + public List GetFullList() + { + using var context = new CarShowroomDatabase(); + return context.Models + .Select(x => x.GetView()) + .ToList(); + } + + public List GetFilteredList(ModelSearch model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new CarShowroomDatabase(); + return context.Models + .Where(x => !model.Id.HasValue || x.Id == model.Id) + .Select(x => x.GetView()) + .ToList(); + } + + public ModelView? GetElement(ModelSearch model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new CarShowroomDatabase(); + return context.Models + .FirstOrDefault(x => !model.Id.HasValue || x.Id == model.Id) + ?.GetView(); + } + + public ModelView? Insert(ModelDto model) + { + var newModel = Model.Create(model); + if (newModel == null) + { + return null; + } + using var context = new CarShowroomDatabase(); + context.Models.Add(newModel); + context.SaveChanges(); + return newModel.GetView(); + } + + public ModelView? Update(ModelDto model) + { + using var context = new CarShowroomDatabase(); + var model = context.Models.FirstOrDefault(x => x.Id == model.Id); + if (model == null) + { + return null; + } + model.Update(model); + context.SaveChanges(); + return model.GetView(); + } + + public ModelView? Delete(ModelDto model) + { + using var context = new CarShowroomDatabase(); + var element = context.Models.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Models.Remove(element); + context.SaveChanges(); + return element.GetView(); + } + return null; + } + } +} diff --git a/CarShowroom/CarShowroomDatabaseStorage/Storages/SaleStorage.cs b/CarShowroom/CarShowroomDatabaseStorage/Storages/SaleStorage.cs new file mode 100644 index 0000000..0dc31d6 --- /dev/null +++ b/CarShowroom/CarShowroomDatabaseStorage/Storages/SaleStorage.cs @@ -0,0 +1,145 @@ +using CarShowroomContracts.StorageContracts; +using CarShowroomDatabaseStorage.Entities; +using CarShowroomDataModels.Dtos; +using CarShowroomDataModels.SearchModel; +using CarShowroomDataModels.Views; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarShowroomDatabaseStorage.Storages +{ + public class SaleStorage : ISaleStorage + { + public List GetFullList() + { + using var context = new CarShowroomDatabase(); + return context.Sales + .Select(x => x.GetView()) + .ToList(); + } + + public List GetFilteredList(SaleSearch model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new CarShowroomDatabase(); + return context.Sales + .Where(x => !model.Id.HasValue || x.Id == model.Id) + .Select(x => x.GetView()) + .ToList(); + } + + public SaleView? GetElement(SaleSearch model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new CarShowroomDatabase(); + return context.Sales + .FirstOrDefault(x => !model.Id.HasValue || x.Id == model.Id) + ?.GetView(); + } + + public SaleView? Insert(SaleDto model) + { + var newSale = Sale.Create(model); + if (newSale == null) + { + return null; + } + using var context = new CarShowroomDatabase(); + context.Sales.Add(newSale); + context.SaveChanges(); + return newSale.GetView(); + } + + public SaleView? Update(SaleDto model) + { + using var context = new CarShowroomDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var sale = context.Sales + .FirstOrDefault(rec => rec.Id == model.Id); + if (sale == null) + return null; + sale.Update(model); + context.SaveChanges(); + UpdateCarsAndServices(context, model, sale); + transaction.Commit(); + return sale.GetView(); + } + catch + { + transaction.Rollback(); + throw; + } + } + + public SaleView? Delete(SaleDto model) + { + using var context = new CarShowroomDatabase(); + var element = context.Sales + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Sales.Remove(element); + context.SaveChanges(); + return element.GetView(); + } + return null; + } + + private void UpdateCarsAndServices(CarShowroomDatabase context, + SaleDto model, Sale entity) + { + var sale = context.Sales.First(x => x.Id == entity.Id); + var saleCars = context.SaleCars + .Where(rec => rec.SaleId == model.Id) + .ToList(); + if (saleCars != null && sale.SaleCars.Count > 0) + { + context.SaleCars + .RemoveRange(saleCars + .Where(rec => !model.CarIds + .Contains(rec.CarId))); + context.SaveChanges(); + } + foreach (var sc in model.CarIds) + { + context.SaleCars.Add(new SaleCar + { + Sale = sale, + Car = context.Cars.First(x => x.Id == sc) + }); + context.SaveChanges(); + } + var saleServices = context.SaleServices + .Where(rec => rec.SaleId == model.Id) + .ToList(); + if (saleServices != null && sale.SaleServices.Count > 0) + { + context.SaleServices + .RemoveRange(saleServices + .Where(rec => !model.ServiceIds + .Contains(rec.ServiceId))); + context.SaveChanges(); + } + foreach (var sc in model.ServiceIds) + { + context.SaleServices.Add(new SaleService + { + Sale = sale, + Service = context.Services.First(x => x.Id == sc) + }); + context.SaveChanges(); + } + } + } +}