89 lines
2.9 KiB
C#
89 lines
2.9 KiB
C#
using DeviceContracts.BindingModels;
|
|
using DeviceContracts.SearchModels;
|
|
using DeviceContracts.StoragesContracts;
|
|
using DeviceContracts.ViewModels;
|
|
using DeviceDatabaseImplement.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DeviceDatabaseImplement.Implements
|
|
{
|
|
public class KitStorage : IKitStorage
|
|
{
|
|
public List<KitViewModel> GetFullList()
|
|
{
|
|
using var context = new DeviceDatabase();
|
|
return context.Kits.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
public List<KitViewModel> GetFilteredList(KitSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.Title) &&
|
|
model.CabinetId == 0)
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new DeviceDatabase();
|
|
return context.Kits
|
|
.Where(x => (string.IsNullOrEmpty(model.Title) ||
|
|
x.Title == model.Title) && (model.CabinetId == 0 ||
|
|
x.CabinetId == model.CabinetId))
|
|
.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
public KitViewModel? GetElement(KitSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.Title) &&
|
|
model.CabinetId == 0)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new DeviceDatabase();
|
|
return context.Kits
|
|
.FirstOrDefault(x => (string.IsNullOrEmpty(model.Title) ||
|
|
x.Title == model.Title) && (model.CabinetId == 0 ||
|
|
x.CabinetId == model.CabinetId))
|
|
?.GetViewModel;
|
|
}
|
|
public KitViewModel? Insert(KitBindingModel model)
|
|
{
|
|
var newKit = Kit.Create(model);
|
|
if (newKit == null)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new DeviceDatabase();
|
|
context.Kits.Add(newKit);
|
|
context.SaveChanges();
|
|
return newKit.GetViewModel;
|
|
}
|
|
public KitViewModel? Update(KitBindingModel model)
|
|
{
|
|
using var context = new DeviceDatabase();
|
|
var kit = context.Kits
|
|
.FirstOrDefault(x => x.Id == model.Id);
|
|
if (kit == null)
|
|
{
|
|
return null;
|
|
}
|
|
kit.Update(model);
|
|
context.SaveChanges();
|
|
return kit.GetViewModel;
|
|
}
|
|
public KitViewModel? Delete(KitBindingModel model)
|
|
{
|
|
using var context = new DeviceDatabase();
|
|
var element = context.Kits
|
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
context.Kits.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|