114 lines
4.3 KiB
C#
114 lines
4.3 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)
|
|
{
|
|
if (string.IsNullOrEmpty(model.Model) &&
|
|
string.IsNullOrEmpty(model.SerialNumber) &&
|
|
model.ProductionDate == null &&
|
|
model.WarrantyPeriod == 0 &&
|
|
model.Condition == null &&
|
|
model.KindId == 0 &&
|
|
model.KitId == 0)
|
|
{
|
|
return new();
|
|
}
|
|
|
|
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 == 0 || 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)
|
|
{
|
|
if (string.IsNullOrEmpty(model.Model) &&
|
|
string.IsNullOrEmpty(model.SerialNumber) &&
|
|
model.ProductionDate == null &&
|
|
model.WarrantyPeriod == 0 &&
|
|
model.Condition == null &&
|
|
model.KindId == 0 &&
|
|
model.KitId == 0)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
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 == 0 || 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)
|
|
{
|
|
context.Devices.Remove(device);
|
|
context.SaveChanges();
|
|
return device.GetViewModel;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
}
|