add storages

This commit is contained in:
Zakharov_Rostislav 2024-05-05 22:58:29 +04:00
parent e9774d6b48
commit 3e9619c7c4
13 changed files with 691 additions and 8 deletions

View File

@ -54,7 +54,7 @@ namespace CarShowroomDatabaseStorage.Entities
ModelId = car.ModelId; ModelId = car.ModelId;
} }
public CarView GetCarView() public CarView GetView()
{ {
CarView car = new CarView(this); CarView car = new CarView(this);
car.ModelPrice = Model?.Price ?? 0; car.ModelPrice = Model?.Price ?? 0;

View File

@ -52,7 +52,7 @@ namespace CarShowroomDatabaseStorage.Entities
PhoneNumber = client.PhoneNumber; PhoneNumber = client.PhoneNumber;
} }
public ClientView GetClientView() public ClientView GetView()
{ {
return new ClientView(this); return new ClientView(this);
} }

View File

@ -57,7 +57,7 @@ namespace CarShowroomDatabaseStorage.Entities
Password = employee.Password; Password = employee.Password;
} }
public EmployeeView GetEmployeeView() public EmployeeView GetView()
{ {
return new EmployeeView(this); return new EmployeeView(this);
} }

View File

@ -56,7 +56,7 @@ namespace CarShowroomDatabaseStorage.Entities
MakeId = model.MakeId; MakeId = model.MakeId;
} }
public ModelView GetModelView() public ModelView GetView()
{ {
ModelView model = new ModelView(this); ModelView model = new ModelView(this);
model.MakeName = Make?.Name ?? string.Empty; model.MakeName = Make?.Name ?? string.Empty;

View File

@ -78,13 +78,13 @@ namespace CarShowroomDatabaseStorage.Entities
EmployeeId = sale.EmployeeId; EmployeeId = sale.EmployeeId;
} }
public SaleView GetSaleView() public SaleView GetView()
{ {
SaleView sale = new SaleView(this); SaleView sale = new SaleView(this);
sale.ClientName = Client?.Name ?? string.Empty; sale.ClientName = Client?.Name ?? string.Empty;
sale.EmployeeName = Employee?.Name ?? string.Empty; sale.EmployeeName = Employee?.Name ?? string.Empty;
sale.Services = SaleServices.Select(s => s.Service.GetServiceView()).ToList(); sale.Services = SaleServices.Select(s => s.Service.GetView()).ToList();
sale.Cars = SaleCars.Select(c => c.Car.GetCarView()).ToList(); sale.Cars = SaleCars.Select(c => c.Car.GetView()).ToList();
return sale; return sale;
} }
} }

View File

@ -51,7 +51,7 @@ namespace CarShowroomDatabaseStorage.Entities
Cost = service.Cost; Cost = service.Cost;
} }
public ServiceView GetServiceView() public ServiceView GetView()
{ {
return new ServiceView(this); return new ServiceView(this);
} }

View File

@ -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<CarView> GetFullList()
{
using var context = new CarShowroomDatabase();
return context.Cars
.Select(x => x.GetView())
.ToList();
}
public List<CarView> 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;
}
}
}

View File

@ -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<ClientView> GetFullList()
{
using var context = new CarShowroomDatabase();
return context.Clients
.Select(x => x.GetView())
.ToList();
}
public List<ClientView> 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;
}
}
}

View File

@ -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<EmployeeView> GetFullList()
{
using var context = new CarShowroomDatabase();
return context.Employees
.Select(x => x.GetView())
.ToList();
}
public List<EmployeeView> 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;
}
}
}

View File

@ -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<ServiceView> GetFullList()
{
using var context = new CarShowroomDatabase();
return context.Services
.Select(x => x.GetView())
.ToList();
}
public List<ServiceView> 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;
}
}
}

View File

@ -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<MakeView> GetFullList()
{
using var context = new CarShowroomDatabase();
return context.Makes
.Select(x => x.GetView())
.ToList();
}
public List<MakeView> 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;
}
}
}

View File

@ -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<ModelView> GetFullList()
{
using var context = new CarShowroomDatabase();
return context.Models
.Select(x => x.GetView())
.ToList();
}
public List<ModelView> 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;
}
}
}

View File

@ -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<SaleView> GetFullList()
{
using var context = new CarShowroomDatabase();
return context.Sales
.Select(x => x.GetView())
.ToList();
}
public List<SaleView> 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();
}
}
}
}