87 lines
2.6 KiB
C#
87 lines
2.6 KiB
C#
using FoodOrdersContracts.BindingModels;
|
|
using FoodOrdersContracts.SearchModels;
|
|
using FoodOrdersContracts.StoragesContracts;
|
|
using FoodOrdersContracts.ViewModels;
|
|
using FoodOrdersFileImplement.Models;
|
|
|
|
namespace FoodOrdersFileImplement.Implements
|
|
{
|
|
public class DishStorage : IDishStorage
|
|
{
|
|
private readonly DataFileSingleton source;
|
|
|
|
public DishStorage()
|
|
{
|
|
source = DataFileSingleton.GetInstance();
|
|
}
|
|
|
|
public List<DishViewModel> GetFullList()
|
|
{
|
|
return source.Dishs
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public List<DishViewModel> GetFilteredList(DishSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.DishName))
|
|
{
|
|
return new();
|
|
}
|
|
return source.Dishs
|
|
.Where(x => x.DishName.Contains(model.DishName))
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public DishViewModel? GetElement(DishSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.DishName) && !model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
return source.Dishs
|
|
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.DishName) && x.DishName == model.DishName) ||
|
|
(model.Id.HasValue && x.Id == model.Id))
|
|
?.GetViewModel;
|
|
}
|
|
|
|
public DishViewModel? Insert(DishBindingModel model)
|
|
{
|
|
model.Id = source.Dishs.Count > 0 ? source.Dishs.Max(x => x.Id) + 1 : 1;
|
|
var newDish = Dish.Create(model);
|
|
if (newDish == null)
|
|
{
|
|
return null;
|
|
}
|
|
source.Dishs.Add(newDish);
|
|
source.SaveDishs();
|
|
return newDish.GetViewModel;
|
|
}
|
|
|
|
public DishViewModel? Update(DishBindingModel model)
|
|
{
|
|
var Dish = source.Dishs.FirstOrDefault(x => x.Id == model.Id);
|
|
if (Dish == null)
|
|
{
|
|
return null;
|
|
}
|
|
Dish.Update(model);
|
|
source.SaveDishs();
|
|
return Dish.GetViewModel;
|
|
}
|
|
|
|
public DishViewModel? Delete(DishBindingModel model)
|
|
{
|
|
var element = source.Dishs.FirstOrDefault(x => x.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
source.Dishs.Remove(element);
|
|
source.SaveDishs();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|