99 lines
2.8 KiB
C#
99 lines
2.8 KiB
C#
|
using SecuritySystemContracts.BindingModels;
|
|||
|
using SecuritySystemContracts.SearchModels;
|
|||
|
using SecuritySystemContracts.StoragesContracts;
|
|||
|
using SecuritySystemContracts.ViewModels;
|
|||
|
using SecuritySystemFileImplement.Models;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.ComponentModel;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace SecuritySystemFileImplement.Implements
|
|||
|
{
|
|||
|
//реализация интерфейса хранилища заготовок
|
|||
|
public class SensorStorage : ISensorStorage
|
|||
|
{
|
|||
|
private readonly DataFileSingleton source;
|
|||
|
|
|||
|
public SensorStorage()
|
|||
|
{
|
|||
|
source = DataFileSingleton.GetInstance();
|
|||
|
}
|
|||
|
|
|||
|
public List<SensorViewModel> GetFullList()
|
|||
|
{
|
|||
|
return source.Sensors.Select(x => x.GetViewModel).ToList();
|
|||
|
}
|
|||
|
|
|||
|
public List<SensorViewModel> GetFilteredList(SensorSearchModel model)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(model.SensorName))
|
|||
|
{
|
|||
|
return new();
|
|||
|
}
|
|||
|
|
|||
|
return source.Sensors.Where(x => x.SensorName.Contains(model.SensorName)).Select(x => x.GetViewModel).ToList();
|
|||
|
}
|
|||
|
|
|||
|
public SensorViewModel? GetElement(SensorSearchModel model)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(model.SensorName) && !model.Id.HasValue)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
return source.Sensors.FirstOrDefault(x => (!string.IsNullOrEmpty(model.SensorName) && x.SensorName == model.SensorName)
|
|||
|
|| (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
public SensorViewModel? Insert(SensorBindingModel model)
|
|||
|
{
|
|||
|
model.Id = source.Sensors.Count > 0 ? source.Sensors.Max(x => x.Id) + 1 : 1;
|
|||
|
|
|||
|
var newSensor = Sensor.Create(model);
|
|||
|
|
|||
|
if (newSensor == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
source.Sensors.Add(newSensor);
|
|||
|
source.SaveSensors();
|
|||
|
|
|||
|
return newSensor.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
public SensorViewModel? Update(SensorBindingModel model)
|
|||
|
{
|
|||
|
var sensor = source.Sensors.FirstOrDefault(x => x.Id == model.Id);
|
|||
|
|
|||
|
if (sensor == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
sensor.Update(model);
|
|||
|
source.SaveSensors();
|
|||
|
|
|||
|
return sensor.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
public SensorViewModel? Delete(SensorBindingModel model)
|
|||
|
{
|
|||
|
var element = source.Sensors.FirstOrDefault(x => x.Id == model.Id);
|
|||
|
|
|||
|
if (element != null)
|
|||
|
{
|
|||
|
source.Sensors.Remove(element);
|
|||
|
source.SaveSensors();
|
|||
|
|
|||
|
return element.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|