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 CargoStorage : ICargoStorage
|
|
{
|
|
public List<CargoViewModel> GetFullList()
|
|
{
|
|
using var context = new TransportCompanyDatabase();
|
|
return context.Cargos.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public List<CargoViewModel> GetFilteredList(CargoSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.CargoName))
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new TransportCompanyDatabase();
|
|
return context.Cargos.Where(x => x.CargoName.Contains(model.CargoName)).Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public CargoViewModel? GetElement(CargoSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.CargoName) && !model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new TransportCompanyDatabase();
|
|
return context.Cargos.FirstOrDefault(x =>
|
|
(!string.IsNullOrEmpty(model.CargoName) && x.CargoName == model.CargoName) ||
|
|
(model.Id.HasValue && x.Id == model.Id))
|
|
?.GetViewModel;
|
|
}
|
|
|
|
public CargoViewModel? Insert(CargoBindingModel model)
|
|
{
|
|
var newComponent = Cargo.Create(model);
|
|
if (newComponent == null)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new TransportCompanyDatabase();
|
|
context.Cargos.Add(newComponent);
|
|
context.SaveChanges();
|
|
return newComponent.GetViewModel;
|
|
}
|
|
|
|
public CargoViewModel? Update(CargoBindingModel model)
|
|
{
|
|
using var context = new TransportCompanyDatabase();
|
|
var component = context.Cargos.FirstOrDefault(x => x.Id == model.Id);
|
|
if (component == null)
|
|
{
|
|
return null;
|
|
}
|
|
component.Update(model);
|
|
context.SaveChanges();
|
|
return component.GetViewModel;
|
|
}
|
|
|
|
public CargoViewModel? Delete(CargoBindingModel model)
|
|
{
|
|
using var context = new TransportCompanyDatabase();
|
|
var element = context.Cargos.FirstOrDefault(rec => rec.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
context.Cargos.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|