101 lines
2.9 KiB
C#
Raw Normal View History

2024-05-20 05:17:39 +04:00
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();
}
2024-05-20 05:17:39 +04:00
public List<KindViewModel> GetFilteredList(KindSearchModel model)
{
if (string.IsNullOrEmpty(model.Title) &&
model.Frequency == 0)
2024-05-20 05:17:39 +04:00
{
return new();
}
2024-05-20 05:17:39 +04:00
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))
2024-05-20 05:17:39 +04:00
.Select(x => x.GetViewModel).ToList();
}
2024-05-20 05:17:39 +04:00
public KindViewModel? GetElement(KindSearchModel model)
{
if (string.IsNullOrEmpty(model.Title) &&
model.Frequency == 0)
2024-05-20 05:17:39 +04:00
{
return null;
}
2024-05-20 05:17:39 +04:00
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))
2024-05-20 05:17:39 +04:00
?.GetViewModel;
}
2024-05-20 05:17:39 +04:00
public KindViewModel? Insert(KindBindingModel model)
{
var newKind = Kind.Create(model);
if (newKind == null)
{
return null;
}
2024-05-20 05:17:39 +04:00
using var context = new DeviceDatabase();
context.Kinds.Add(newKind);
context.SaveChanges();
return newKind.GetViewModel;
}
2024-05-20 05:17:39 +04:00
public KindViewModel? Update(KindBindingModel model)
{
using var context = new DeviceDatabase();
var kind = context.Kinds
.FirstOrDefault(x => x.Id == model.Id);
2024-05-20 05:17:39 +04:00
if (kind == null)
{
return null;
}
2024-05-20 05:17:39 +04:00
kind.Update(model);
context.SaveChanges();
return kind.GetViewModel;
}
2024-05-20 05:17:39 +04:00
public KindViewModel? Delete(KindBindingModel model)
{
using var context = new DeviceDatabase();
var kind = context.Kinds
.FirstOrDefault(x => x.Id == model.Id);
if (kind != null)
2024-05-20 05:17:39 +04:00
{
context.Kinds.Remove(kind);
2024-05-20 05:17:39 +04:00
context.SaveChanges();
return kind.GetViewModel;
2024-05-20 05:17:39 +04:00
}
2024-05-20 05:17:39 +04:00
return null;
}
}
}