80 lines
2.7 KiB
C#
80 lines
2.7 KiB
C#
using TransportCompanyContracts.BindingModels;
|
|
using TransportCompanyContracts.SearchModels;
|
|
using TransportCompanyContracts.StoragesContracts;
|
|
using TransportCompanyContracts.ViewModels;
|
|
using TransportCompanyDatabaseImplement.Models;
|
|
|
|
namespace TransportCompanyDatabaseImplement.Implements
|
|
{
|
|
public class DriverStorage : IDriverStorage
|
|
{
|
|
public List<DriverViewModel> GetFullList()
|
|
{
|
|
using var context = new TransportCompanyDatabase();
|
|
return context.Drivers.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public List<DriverViewModel> GetFilteredList(DriverSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.DriverFio))
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new TransportCompanyDatabase();
|
|
return context.Drivers.Where(x => x.DriverFio.Contains(model.DriverFio)).Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public DriverViewModel? GetElement(DriverSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.DriverFio) && !model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new TransportCompanyDatabase();
|
|
return context.Drivers.FirstOrDefault(x =>
|
|
(!string.IsNullOrEmpty(model.DriverFio) && x.DriverFio == model.DriverFio) ||
|
|
(model.Id.HasValue && x.Id == model.Id))
|
|
?.GetViewModel;
|
|
}
|
|
|
|
public DriverViewModel? Insert(DriverBindingModel model)
|
|
{
|
|
var newComponent = Driver.Create(model);
|
|
if (newComponent == null)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new TransportCompanyDatabase();
|
|
context.Drivers.Add(newComponent);
|
|
context.SaveChanges();
|
|
return newComponent.GetViewModel;
|
|
}
|
|
|
|
public DriverViewModel? Update(DriverBindingModel model)
|
|
{
|
|
using var context = new TransportCompanyDatabase();
|
|
var component = context.Drivers.FirstOrDefault(x => x.Id == model.Id);
|
|
if (component == null)
|
|
{
|
|
return null;
|
|
}
|
|
component.Update(model);
|
|
context.SaveChanges();
|
|
return component.GetViewModel;
|
|
}
|
|
|
|
public DriverViewModel? Delete(DriverBindingModel model)
|
|
{
|
|
using var context = new TransportCompanyDatabase();
|
|
var element = context.Drivers.FirstOrDefault(rec => rec.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
context.Drivers.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|