92 lines
3.6 KiB
C#
92 lines
3.6 KiB
C#
using DeviceContracts.BindingModels;
|
|
using DeviceContracts.SearchModels;
|
|
using DeviceContracts.StoragesContracts;
|
|
using DeviceContracts.ViewModels;
|
|
using DeviceDatabaseImplement.Models;
|
|
|
|
namespace DeviceDatabaseImplement.Implements
|
|
{
|
|
public class DeviceStorage : IDeviceStorage
|
|
{
|
|
public List<DeviceViewModel> GetFullList()
|
|
{
|
|
using var context = new DeviceDatabase();
|
|
return context.Devices.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
public List<DeviceViewModel> GetFilteredList(DeviceSearchModel model)
|
|
{
|
|
using var context = new DeviceDatabase();
|
|
return context.Devices
|
|
.Where(x => (string.IsNullOrEmpty(model.Model) ||
|
|
x.Model == model.Model) &&
|
|
(string.IsNullOrEmpty(model.SerialNumber) ||
|
|
x.SerialNumber == model.SerialNumber) &&
|
|
(model.ProductionDate == null ||
|
|
x.ProductionDate == model.ProductionDate) &&
|
|
(model.WarrantyPeriod == null ||
|
|
x.WarrantyPeriod == model.WarrantyPeriod) &&
|
|
(model.Condition == null || x.Condition == model.Condition)
|
|
&& (model.KindId == 0 || x.KindId == model.KindId) &&
|
|
(model.KitId == 0 || x.KitId == model.KitId))
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
public DeviceViewModel? GetElement(DeviceSearchModel model)
|
|
{
|
|
using var context = new DeviceDatabase();
|
|
return context.Devices
|
|
.FirstOrDefault(x => (string.IsNullOrEmpty(model.Model) ||
|
|
x.Model == model.Model) &&
|
|
(string.IsNullOrEmpty(model.SerialNumber) ||
|
|
x.SerialNumber == model.SerialNumber) &&
|
|
(model.ProductionDate == null ||
|
|
x.ProductionDate == model.ProductionDate) &&
|
|
(model.WarrantyPeriod == null ||
|
|
x.WarrantyPeriod == model.WarrantyPeriod) &&
|
|
(model.Condition == null ||
|
|
x.Condition == model.Condition) &&
|
|
(model.KindId == 0 || x.KindId == model.KindId) &&
|
|
(model.KitId == 0 || x.KitId == model.KitId))?
|
|
.GetViewModel;
|
|
}
|
|
public DeviceViewModel? Insert(DeviceBindingModel model)
|
|
{
|
|
var newDevice = Device.Create(model);
|
|
if (newDevice == null)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new DeviceDatabase();
|
|
context.Devices.Add(newDevice);
|
|
context.SaveChanges();
|
|
return newDevice.GetViewModel;
|
|
}
|
|
public DeviceViewModel? Update(DeviceBindingModel model)
|
|
{
|
|
using var context = new DeviceDatabase();
|
|
var device = context.Devices
|
|
.FirstOrDefault(x => x.Id == model.Id);
|
|
if (device == null)
|
|
{
|
|
return null;
|
|
}
|
|
device.Update(model);
|
|
context.SaveChanges();
|
|
return device.GetViewModel;
|
|
}
|
|
public DeviceViewModel? Delete(DeviceBindingModel model)
|
|
{
|
|
using var context = new DeviceDatabase();
|
|
var device = context.Devices
|
|
.FirstOrDefault(x => x.Id == model.Id);
|
|
if (device == null)
|
|
{
|
|
return null;
|
|
}
|
|
context.Devices.Remove(device);
|
|
context.SaveChanges();
|
|
return device.GetViewModel;
|
|
}
|
|
}
|
|
}
|