86 lines
3.1 KiB
C#
86 lines
3.1 KiB
C#
|
using FoodOrdersContracts.BindingModels;
|
|||
|
using FoodOrdersContracts.SearchModels;
|
|||
|
using FoodOrdersContracts.StoragesContracts;
|
|||
|
using FoodOrdersContracts.ViewModels;
|
|||
|
using FoodOrdersDatabaseImplement.Models;
|
|||
|
|
|||
|
namespace FoodOrdersDatabaseImplement.Implements
|
|||
|
{
|
|||
|
public class ImplementerStorage : IImplementerStorage
|
|||
|
{
|
|||
|
public List<ImplementerViewModel> GetFullList()
|
|||
|
{
|
|||
|
using var context = new FoodOrdersDatabase();
|
|||
|
return context.Implementers
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
|
|||
|
public List<ImplementerViewModel> GetFilteredList(ImplementerSearchModel model)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(model.ImplementerFIO))
|
|||
|
{
|
|||
|
return new();
|
|||
|
}
|
|||
|
using var context = new FoodOrdersDatabase();
|
|||
|
return context.Implementers
|
|||
|
.Where(x => x.ImplementerFIO.Contains(model.ImplementerFIO))
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
|
|||
|
public ImplementerViewModel? GetElement(ImplementerSearchModel model)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(model.ImplementerFIO) && string.IsNullOrEmpty(model.Password) && !model.Id.HasValue)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
using var context = new FoodOrdersDatabase();
|
|||
|
return context.Implementers
|
|||
|
.FirstOrDefault(x =>
|
|||
|
(!string.IsNullOrEmpty(model.ImplementerFIO) && x.ImplementerFIO == model.ImplementerFIO
|
|||
|
&& (string.IsNullOrEmpty(model.Password) || x.Password == model.Password))
|
|||
|
|| (model.Id.HasValue && x.Id == model.Id))
|
|||
|
?.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
public ImplementerViewModel? Insert(ImplementerBindingModel model)
|
|||
|
{
|
|||
|
var newImplementer = Implementer.Create(model);
|
|||
|
if (newImplementer == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
using var context = new FoodOrdersDatabase();
|
|||
|
context.Implementers.Add(newImplementer);
|
|||
|
context.SaveChanges();
|
|||
|
return newImplementer.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
public ImplementerViewModel? Update(ImplementerBindingModel model)
|
|||
|
{
|
|||
|
using var context = new FoodOrdersDatabase();
|
|||
|
var implementer = context.Implementers.FirstOrDefault(x => x.Id == model.Id);
|
|||
|
if (implementer == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
implementer.Update(model);
|
|||
|
context.SaveChanges();
|
|||
|
return implementer.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
public ImplementerViewModel? Delete(ImplementerBindingModel model)
|
|||
|
{
|
|||
|
using var context = new FoodOrdersDatabase();
|
|||
|
var element = context.Implementers.FirstOrDefault(x => x.Id == model.Id);
|
|||
|
if (element != null)
|
|||
|
{
|
|||
|
context.Implementers.Remove(element);
|
|||
|
context.SaveChanges();
|
|||
|
return element.GetViewModel;
|
|||
|
}
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|