я ее все, она пантера
This commit is contained in:
parent
bb4b669d7c
commit
c90a62cdda
109
CarCenterDatabaseImplement/Implements/ClientStorage.cs
Normal file
109
CarCenterDatabaseImplement/Implements/ClientStorage.cs
Normal file
@ -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<ClientViewModel> 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<ClientViewModel> 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;
|
||||
}
|
||||
}
|
||||
}
|
95
CarCenterDatabaseImplement/Implements/RequirementStorage.cs
Normal file
95
CarCenterDatabaseImplement/Implements/RequirementStorage.cs
Normal file
@ -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<RequirementViewModel> 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<RequirementViewModel> 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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user