сделала Implements

This commit is contained in:
Nastya_Kozlova 2024-04-29 00:20:55 +04:00
parent 4e7c734a22
commit f4b5935751
4 changed files with 589 additions and 0 deletions

View File

@ -0,0 +1,127 @@
using CarCenterContracts.BindingModels;
using CarCenterContracts.SearchModels;
using CarCenterContracts.StoragesContracts;
using CarCenterContracts.ViewModels;
using CarCenterDataBaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarCenterDataBaseImplement.Implements
{
public class AdministratorStorage : IAdministratorStorage
{
public List<AdministratorViewModel> GetFullList()
{
using var context = new CarCenterDataBase();
return context.Administrators
.Select(x => x.GetViewModel)
.ToList();
}
public List<AdministratorViewModel> GetFilteredList(AdministratorSearchModel model)
{
if (string.IsNullOrEmpty(model.AdministratorFIO))
{
return new();
}
using var context = new CarCenterDataBase();
return context.Administrators
.Include(x => x.Inspections)
.Include(x => x.Cars)
.Include(x => x.Equipments)
.Where(x => x.AdministratorLogin.Contains(model.AdministratorLogin) && x.AdministratorPassword == model.AdministratorPassword)
.Select(x => x.GetViewModel)
.ToList();
}
public AdministratorViewModel? GetElement(AdministratorSearchModel model)
{
using var context = new CarCenterDataBase();
if (model.Id.HasValue)
return context.Administrators
.Include(x => x.Inspections)
.Include(x => x.Cars)
.Include(x => x.Equipments)
.FirstOrDefault(x => x.Id == model.Id)?
.GetViewModel;
if (!string.IsNullOrEmpty(model.AdministratorEmail) && !string.IsNullOrEmpty(model.AdministratorPassword))
return context.Administrators
.Include(x => x.Inspections)
.Include(x => x.Cars)
.Include(x => x.Equipments)
.FirstOrDefault(x => x.AdministratorEmail.Equals(model.AdministratorEmail) && x.AdministratorPassword.Equals(model.AdministratorPassword))?
.GetViewModel;
if (!string.IsNullOrEmpty(model.AdministratorEmail))
return context.Administrators
.Include(x => x.Inspections)
.Include(x => x.Cars)
.Include(x => x.Equipments)
.FirstOrDefault(x => x.AdministratorEmail.Equals(model.AdministratorEmail))?
.GetViewModel;
return null;
}
public AdministratorViewModel? Insert(AdministratorBindingModel model)
{
var newAdministrator = Administrator.Create(model);
if (newAdministrator == null)
{
return null;
}
using var context = new CarCenterDataBase();
context.Administrators.Add(newAdministrator);
context.SaveChanges();
return newAdministrator.GetViewModel;
}
public AdministratorViewModel? Update(AdministratorBindingModel model)
{
using var context = new CarCenterDataBase();
var administrator = context.Administrators
.FirstOrDefault(x => x.Id == model.Id);
if (administrator == null)
{
return null;
}
administrator.Update(model);
context.SaveChanges();
return administrator.GetViewModel;
}
public AdministratorViewModel? Delete(AdministratorBindingModel model)
{
using var context = new CarCenterDataBase();
var element = context.Administrators.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Administrators.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,134 @@
using CarCenterContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using CarCenterDataBaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using CarCenterContracts.SearchModels;
using CarCenterContracts.BindingModels;
using CarCenterContracts.StoragesContracts;
namespace CarCenterDataBaseImplement.Implements
{
public class CarStorage : ICarStorage
{
public List<CarViewModel> GetFullList()
{
using var context = new CarCenterDataBase();
return context.Cars
.Include(x => x.EquipmentCars)
.ThenInclude(x => x.Equipment)
.Include(x => x.InspectionCar)
.ThenInclude(x => x.Inspection)
.Include(x => x.Administrator)
.Select(x => x.GetViewModel)
.ToList();
}
public CarViewModel? GetElement(CarSearchModel model)
{
if (string.IsNullOrEmpty(model.BrandCar) && !model.Id.HasValue)
{
return null;
}
using var context = new CarCenterDataBase();
return context.Cars
.Include(x => x.EquipmentCars)
.ThenInclude(x => x.Equipment)
.Include(x => x.InspectionCar)
.ThenInclude(x => x.Inspection)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.BrandCar) && x.BrandCar == model.BrandCar) || (model.Id.HasValue && x.Id == model.Id))?
.GetViewModel;
}
public List<CarViewModel> GetFilteredList(CarSearchModel model)
{
if (string.IsNullOrEmpty(model.BrandCar) && !model.AdministratorId.HasValue)
{
return new();
}
using var context = new CarCenterDataBase();
if (model.AdministratorId.HasValue)
{
return context.Cars
.Include(x => x.EquipmentCars)
.ThenInclude(x => x.Equipment)
.Include(x => x.InspectionCar)
.ThenInclude(x => x.Inspection)
.Include(x => x.Administrator)
.Where(x => x.AdministratorId == model.AdministratorId)
.Select(x => x.GetViewModel)
.ToList();
}
return context.Cars
.Include(x => x.EquipmentCars)
.ThenInclude(x => x.Equipment)
.Include(x => x.InspectionCar)
.ThenInclude(x => x.Inspection)
.Include(x => x.Administrator)
.Where(x => x.BrandCar.Contains(model.BrandCar))
.Select(x => x.GetViewModel)
.ToList();
}
public CarViewModel? Insert(CarBindingModel model)
{
using var context = new CarCenterDataBase();
var newCar = Car.Create(model);
if (newCar == null)
{
return null;
}
context.Cars.Add(newCar);
context.SaveChanges();
return newCar.GetViewModel;
}
public CarViewModel? Update(CarBindingModel model)
{
using var context = new CarCenterDataBase();
var car = context.Cars.FirstOrDefault(x => x.Id == model.Id);
if (car == null)
{
return null;
}
car.Update(model);
context.SaveChanges();
return car.GetViewModel;
}
public CarViewModel? Delete(CarBindingModel model)
{
using var context = new CarCenterDataBase();
var element = context.Cars.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Cars.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,167 @@
using CarCenterContracts.StoragesContracts;
using CarCenterContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using CarCenterContracts.SearchModels;
using CarCenterContracts.BindingModels;
using CarCenterDataBaseImplement.Models;
namespace CarCenterDataBaseImplement.Implements
{
public class EquipmentStorage : IEquipmentStorage
{
public List<EquipmentViewModel> GetFullList()
{
using var context = new CarCenterDataBase();
return context.Equipments
.Include(x => x.Cars)
.ThenInclude(x => x.Car)
.ThenInclude(x => x.InspectionCar)
.ThenInclude(x => x.Inspection)
.Include(x => x.PreSaleWork)
.Include(x => x.Administrator)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public EquipmentViewModel? GetElement(EquipmentSearchModel model)
{
if (!model.Id.HasValue && string.IsNullOrEmpty(model.EquipmentName))
{
return null;
}
using var context = new CarCenterDataBase();
return context.Equipments
.Include(x => x.Cars)
.ThenInclude(x => x.Car)
.ThenInclude(x => x.InspectionCar)
.ThenInclude(x => x.Inspection)
.Include(x => x.PreSaleWork)
.Include(x => x.Administrator)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.EquipmentName) && x.EquipmentName == model.EquipmentName) || (model.Id.HasValue && x.Id == model.Id))?
.GetViewModel;
}
public List<EquipmentViewModel> GetFilteredList(EquipmentSearchModel model)
{
if (!model.Id.HasValue && !model.AdministratorId.HasValue)
{
return new();
}
using var context = new CarCenterDataBase();
if (model.AdministratorId.HasValue)
{
return context.Equipments
.Include(x => x.Cars)
.ThenInclude(x => x.Car)
.ThenInclude(x => x.InspectionCar)
.ThenInclude(x => x.Inspection)
.Include(x => x.PreSaleWork)
.Include(x => x.Administrator)
.Where(x => x.AdministratorId == model.AdministratorId)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
return context.Equipments
.Include(x => x.Cars)
.ThenInclude(x => x.Car)
.ThenInclude(x => x.InspectionCar)
.ThenInclude(x => x.Inspection)
.Include(x => x.PreSaleWork)
.Include(x => x.Administrator)
.Where(x => x.EquipmentName.Contains(model.EquipmentName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public EquipmentViewModel? Insert(EquipmentBindingModel model)
{
using var context = new CarCenterDataBase();
var newEquipment = Equipment.Create(context, model);
if (newEquipment == null)
{
return null;
}
context.Equipments.Add(newEquipment);
context.SaveChanges();
return context.Equipments
.Include(x => x.Cars)
.ThenInclude(x => x.Car)
.ThenInclude(x => x.InspectionCar)
.ThenInclude(x => x.Inspection)
.ThenInclude(x => x.Employee)
.Include(x => x.PreSaleWork)
.Include(x => x.Administrator)
.FirstOrDefault(x => x.Id == newEquipment.Id)
?.GetViewModel;
}
public EquipmentViewModel? Update(EquipmentBindingModel model)
{
using var context = new CarCenterDataBase();
using var transaction = context.Database.BeginTransaction();
try
{
var elem = context.Equipments
.Include(x => x.Cars)
.ThenInclude(x => x.Car)
.ThenInclude(x => x.InspectionCar)
.ThenInclude(x => x.Inspection)
.ThenInclude(x => x.Employee)
.FirstOrDefault(rec => rec.Id == model.Id);
if (elem == null)
{
return null;
}
elem.Update(model);
context.SaveChanges();
if (model.EquipmentCars != null)
elem.UpdateCars(context, model);
transaction.Commit();
return elem.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public EquipmentViewModel? Delete(EquipmentBindingModel model)
{
using var context = new CarCenterDataBase();
var element = context.Equipments
.Include(x => x.Cars)
.ThenInclude(x => x.Car)
.ThenInclude(x => x.InspectionCar)
.ThenInclude(x => x.Inspection)
.ThenInclude(x => x.Employee)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Equipments.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,161 @@
using CarCenterContracts.ViewModels;
using CarCenterDataBaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using CarCenterContracts.SearchModels;
using CarCenterContracts.BindingModels;
using CarCenterContracts.StoragesContracts;
namespace CarCenterDataBaseImplement.Implements
{
public class InspectionStorage : IInspectionStorage
{
public List<InspectionViewModel> GetFullList()
{
using var context = new CarCenterDataBase();
return context.Inspections
.Include(x => x.Cars)
.ThenInclude(x => x.Car)
.ThenInclude(x => x.EquipmentCars)
.ThenInclude(x => x.Equipment)
.Include(x => x.Employee)
.Include(x => x.Administrator)
.Select(x => x.GetViewModel)
.ToList();
}
public InspectionViewModel? GetElement(InspectionSearchModel model)
{
if (string.IsNullOrEmpty(model.InspectionName) && !model.Id.HasValue)
{
return null;
}
using var context = new CarCenterDataBase();
return context.Inspections
.Include(x => x.Cars)
.ThenInclude(x => x.Car)
.ThenInclude(x => x.EquipmentCars)
.ThenInclude(x => x.Equipment)
.Include(x => x.Employee)
.Include(x => x.Administrator)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.InspectionName) && x.InspectionName == model.InspectionName) || (model.Id.HasValue && x.Id == model.Id))?
.GetViewModel;
}
public List<InspectionViewModel> GetFilteredList(InspectionSearchModel model)
{
if (!model.AdministratorId.HasValue)
{
return new();
}
using var context = new CarCenterDataBase();
if (model.AdministratorId.HasValue)
{
return context.Inspections
.Include(x => x.Cars)
.ThenInclude(x => x.Car)
.ThenInclude(x => x.EquipmentCars)
.ThenInclude(x => x.Equipment)
.Include(x => x.Employee)
.Include(x => x.Administrator)
.Where(x => x.AdministratorId == model.AdministratorId)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
return context.Inspections
.Include(x => x.Cars)
.ThenInclude(x => x.Car)
.ThenInclude(x => x.EquipmentCars)
.ThenInclude(x => x.Equipment)
.Include(x => x.Employee)
.Include(x => x.Administrator)
.Where(x => x.InspectionName.Contains(model.InspectionName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public InspectionViewModel? Insert(InspectionBindingModel model)
{
using var context = new CarCenterDataBase();
var newInspection = Inspection.Create(context, model);
if (newInspection == null)
{
return null;
}
context.Inspections.Add(newInspection);
context.SaveChanges();
return context.Inspections
.Include(x => x.Cars)
.ThenInclude(x => x.Car)
.ThenInclude(x => x.EquipmentCars)
.ThenInclude(x => x.Equipment)
.Include(x => x.Employee)
.Include(x => x.Administrator)
.FirstOrDefault(x => x.Id == newInspection.Id)
?.GetViewModel;
}
public InspectionViewModel? Update(InspectionBindingModel model)
{
using var context = new CarCenterDataBase();
using var transaction = context.Database.BeginTransaction();
try
{
var elem = context.Inspections
.Include(x => x.Cars)
.ThenInclude(x => x.Car)
.ThenInclude(x => x.InspectionCar)
.ThenInclude(x => x.Inspection)
.ThenInclude(x => x.Employee)
.FirstOrDefault(rec => rec.Id == model.Id); if (elem == null)
{
return null;
}
elem.Update(model);
context.SaveChanges();
if (model.InspectionCars != null)
elem.UpdateCars(context, model);
transaction.Commit();
return elem.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public InspectionViewModel? Delete(InspectionBindingModel model)
{
using var context = new CarCenterDataBase();
var element = context.Inspections
.Include(x => x.Cars)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Inspections.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}