PIbd-21_Balberova_D.N._Sush.../SushiBar/SushiBarFileImplement/Implements/IngredientStorage.cs

74 lines
2.7 KiB
C#
Raw Normal View History

2023-02-12 14:57:32 +04:00
using SushiBarContracts.BindingModels;
using SushiBarContracts.SearchModels;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushiBarFileImplement.Models;
namespace SushiBarFileImplement.Implements
{
public class IngredientStorage : IIngredientStorage
{
private readonly DataFileSingleton source;
public IngredientStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<IngredientViewModel> GetFullList()
{
return source.Ingredients.Select(x => x.GetViewModel).ToList();
}
public List<IngredientViewModel> GetFilteredList(IngredientSearchModel model)
{
if (string.IsNullOrEmpty(model.IngredientName))
{
return new();
}
return source.Ingredients.Where(x =>
x.IngredientName.Contains(model.IngredientName)).Select(x => x.GetViewModel).ToList();
}
public IngredientViewModel? GetElement(IngredientSearchModel model)
{
if (string.IsNullOrEmpty(model.IngredientName) && !model.Id.HasValue)
{
return null;
}
return source.Ingredients.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.IngredientName) && x.IngredientName ==
model.IngredientName) || (model.Id.HasValue && x.Id == model.Id)) ?.GetViewModel;
}
public IngredientViewModel? Insert(IngredientBindingModel model)
{
model.Id = source.Ingredients.Count > 0 ? source.Ingredients.Max(x => x.Id) + 1 : 1;
var newIngredient = Ingredient.Create(model);
if (newIngredient == null)
{
return null;
}
source.Ingredients.Add(newIngredient);
source.SaveIngredients();
return newIngredient.GetViewModel;
}
public IngredientViewModel? Update(IngredientBindingModel model)
{
var ingredient = source.Ingredients.FirstOrDefault(x => x.Id == model.Id);
if (ingredient == null)
{
return null;
}
ingredient.Update(model);
source.SaveIngredients();
return ingredient.GetViewModel;
}
public IngredientViewModel? Delete(IngredientBindingModel model)
{
var element = source.Ingredients.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
source.Ingredients.Remove(element);
source.SaveIngredients();
return element.GetViewModel;
}
return null;
}
}
}