2023-02-20 10:34:37 +04:00
|
|
|
|
using ConfectioneryContracts.BindingModels;
|
|
|
|
|
using ConfectioneryContracts.SearchModels;
|
|
|
|
|
using ConfectioneryContracts.StoragesContracts;
|
|
|
|
|
using ConfectioneryContracts.ViewModels;
|
|
|
|
|
using ConfectioneryFileImplement.Models;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace ConfectioneryFileImplement.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)
|
|
|
|
|
{
|
2023-02-20 10:46:32 +04:00
|
|
|
|
var ingredient = source.Ingredients.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (ingredient == null)
|
2023-02-20 10:34:37 +04:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-02-20 10:46:32 +04:00
|
|
|
|
ingredient.Update(model);
|
2023-02-20 10:34:37 +04:00
|
|
|
|
source.SaveIngredients();
|
2023-02-20 10:46:32 +04:00
|
|
|
|
return ingredient.GetViewModel;
|
2023-02-20 10:34:37 +04:00
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|