104 lines
3.0 KiB
C#
104 lines
3.0 KiB
C#
using SecuritySystemContracts.BindingModels;
|
|
using SecuritySystemContracts.SearchModels;
|
|
using SecuritySystemContracts.StoragesContracts;
|
|
using SecuritySystemContracts.ViewModels;
|
|
using SecuritySystemDatabaseImplement.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace SecuritySystemDatabaseImplement.Implements
|
|
{
|
|
public class SensorStorage : ISensorStorage
|
|
{
|
|
public List<SensorViewModel> GetFullList()
|
|
{
|
|
using var context = new SecuritySystemDatabase();
|
|
|
|
return context.Sensors
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public List<SensorViewModel> GetFilteredList(SensorSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.SensorName))
|
|
{
|
|
return new();
|
|
}
|
|
|
|
using var context = new SecuritySystemDatabase();
|
|
|
|
return context.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;
|
|
}
|
|
|
|
using var context = new SecuritySystemDatabase();
|
|
|
|
return context.Sensors
|
|
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.SensorName) && x.SensorName == model.SensorName) ||
|
|
(model.Id.HasValue && x.Id == model.Id))
|
|
?.GetViewModel;
|
|
}
|
|
|
|
public SensorViewModel? Insert(SensorBindingModel model)
|
|
{
|
|
var newSensor = Sensor.Create(model);
|
|
|
|
if (newSensor == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
using var context = new SecuritySystemDatabase();
|
|
context.Sensors.Add(newSensor);
|
|
context.SaveChanges();
|
|
|
|
return newSensor.GetViewModel;
|
|
}
|
|
|
|
public SensorViewModel? Update(SensorBindingModel model)
|
|
{
|
|
using var context = new SecuritySystemDatabase();
|
|
var sensor = context.Sensors.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
if (sensor == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
sensor.Update(model);
|
|
context.SaveChanges();
|
|
|
|
return sensor.GetViewModel;
|
|
}
|
|
|
|
public SensorViewModel? Delete(SensorBindingModel model)
|
|
{
|
|
using var context = new SecuritySystemDatabase();
|
|
var element = context.Sensors.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
if (element != null)
|
|
{
|
|
context.Sensors.Remove(element);
|
|
context.SaveChanges();
|
|
|
|
return element.GetViewModel;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
}
|