PIbd-21_Raspaev_N.I._FoodOr.../FoodOrders/FoodOrdersFileImplement/Implements/ComponentStorage.cs

74 lines
2.8 KiB
C#
Raw Normal View History

2023-02-12 14:58:15 +04:00
using FoodOrdersContracts.BindingModels;
using FoodOrdersContracts.SearchModels;
using FoodOrdersContracts.StoragesContracts;
using FoodOrdersContracts.ViewModels;
using FoodOrdersFileImplement.Models;
using FoodOrdersFileImplement;
namespace FoodOrdersFileImplement.Implements
{
public class ComponentStorage : IComponentStorage
{
2023-02-12 15:28:46 +04:00
private readonly DataFileSingleton _source;
2023-02-12 14:58:15 +04:00
public ComponentStorage()
{
2023-02-12 15:28:46 +04:00
_source = DataFileSingleton.GetInstance();
2023-02-12 14:58:15 +04:00
}
public List<ComponentViewModel> GetFullList()
{
2023-02-12 15:28:46 +04:00
return _source.Components.Select(x => x.GetViewModel).ToList();
2023-02-12 14:58:15 +04:00
}
2023-03-12 10:35:52 +04:00
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel model)
2023-02-12 14:58:15 +04:00
{
if (string.IsNullOrEmpty(model.ComponentName))
{
return new();
}
2023-02-12 15:28:46 +04:00
return _source.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList();
2023-02-12 14:58:15 +04:00
}
2023-03-12 10:35:52 +04:00
//FirstOrDefault выбирается первый или ничего, то есть вернёт первое совпадение или null
2023-02-12 14:58:15 +04:00
public ComponentViewModel? GetElement(ComponentSearchModel model)
{
2023-02-12 15:28:46 +04:00
if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue)
2023-02-12 14:58:15 +04:00
{
return null;
}
2023-02-12 15:28:46 +04:00
return _source.Components.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
2023-02-12 14:58:15 +04:00
}
public ComponentViewModel? Insert(ComponentBindingModel model)
{
2023-03-12 10:35:52 +04:00
model.Id = _source.Components.Count > 0 ? _source.Components.Max(x => x.Id) + 1 : 1;
2023-02-12 14:58:15 +04:00
var newComponent = Component.Create(model);
if (newComponent == null)
{
return null;
}
2023-02-12 15:28:46 +04:00
_source.Components.Add(newComponent);
_source.SaveComponents();
2023-02-12 14:58:15 +04:00
return newComponent.GetViewModel;
}
public ComponentViewModel? Update(ComponentBindingModel model)
{
2023-03-12 10:35:52 +04:00
var component = _source.Components.FirstOrDefault(x => x.Id == model.Id);
2023-02-12 14:58:15 +04:00
if (component == null)
{
return null;
}
component.Update(model);
2023-02-12 15:28:46 +04:00
_source.SaveComponents();
2023-02-12 14:58:15 +04:00
return component.GetViewModel;
}
public ComponentViewModel? Delete(ComponentBindingModel model)
{
2023-03-12 10:35:52 +04:00
var element = _source.Components.FirstOrDefault(x => x.Id == model.Id);
2023-02-12 14:58:15 +04:00
if (element != null)
{
2023-02-12 15:28:46 +04:00
_source.Components.Remove(element);
_source.SaveComponents();
2023-02-12 14:58:15 +04:00
return element.GetViewModel;
}
return null;
}
}
}