101 lines
2.9 KiB
C#
101 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 KindStorage : IKindStorage
|
|
{
|
|
public List<KindViewModel> GetFullList()
|
|
{
|
|
using var context = new DeviceDatabase();
|
|
return context.Kinds.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public List<KindViewModel> GetFilteredList(KindSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.Title) &&
|
|
model.Frequency == 0)
|
|
{
|
|
return new();
|
|
}
|
|
|
|
using var context = new DeviceDatabase();
|
|
return context.Kinds
|
|
.Where(x => (string.IsNullOrEmpty(model.Title) ||
|
|
x.Title == model.Title) && (model.Frequency == 0 ||
|
|
x.Frequency == model.Frequency))
|
|
.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public KindViewModel? GetElement(KindSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.Title) &&
|
|
model.Frequency == 0)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
using var context = new DeviceDatabase();
|
|
return context.Kinds
|
|
.FirstOrDefault(x => (string.IsNullOrEmpty(model.Title) ||
|
|
x.Title == model.Title) && (model.Frequency == 0 ||
|
|
x.Frequency == model.Frequency))
|
|
?.GetViewModel;
|
|
}
|
|
|
|
public KindViewModel? Insert(KindBindingModel model)
|
|
{
|
|
var newKind = Kind.Create(model);
|
|
if (newKind == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
using var context = new DeviceDatabase();
|
|
context.Kinds.Add(newKind);
|
|
context.SaveChanges();
|
|
return newKind.GetViewModel;
|
|
}
|
|
|
|
public KindViewModel? Update(KindBindingModel model)
|
|
{
|
|
using var context = new DeviceDatabase();
|
|
var kind = context.Kinds
|
|
.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
if (kind == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
kind.Update(model);
|
|
context.SaveChanges();
|
|
return kind.GetViewModel;
|
|
}
|
|
|
|
public KindViewModel? Delete(KindBindingModel model)
|
|
{
|
|
using var context = new DeviceDatabase();
|
|
var kind = context.Kinds
|
|
.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
if (kind != null)
|
|
{
|
|
context.Kinds.Remove(kind);
|
|
context.SaveChanges();
|
|
return kind.GetViewModel;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
}
|