ISEbd-22_Baygulov_A.A._Sush.../SushiBar/SushiBarFileImplement/SushiStorage.cs

86 lines
2.6 KiB
C#
Raw Normal View History

2024-03-14 21:54:43 +04:00
using SushiBarContracts.BindingModels;
using SushiBarContracts.SearchModels;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushiBarFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SushiBarFileImplement.Implements
{
2024-03-17 19:36:45 +04:00
public class SushiStorage : ISushiStorage
2024-03-14 21:54:43 +04:00
{
private readonly DataFileSingleton source;
2024-03-17 19:36:45 +04:00
public SushiStorage()
2024-03-14 21:54:43 +04:00
{
source = DataFileSingleton.GetInstance();
}
2024-03-17 19:36:45 +04:00
public List<SushiViewModel> GetFullList()
2024-03-14 21:54:43 +04:00
{
2024-03-17 19:36:45 +04:00
return source.Sushis.Select(x => x.GetViewModel).ToList();
2024-03-14 21:54:43 +04:00
}
2024-03-17 19:36:45 +04:00
public List<SushiViewModel> GetFilteredList(SushiSearchModel model)
2024-03-14 21:54:43 +04:00
{
2024-03-17 19:36:45 +04:00
if (string.IsNullOrEmpty(model.SushiName))
2024-03-14 21:54:43 +04:00
{
return new();
}
2024-03-17 19:36:45 +04:00
return source.Sushis.Where(x => x.SushiName.Contains(model.SushiName)).Select(x => x.GetViewModel).ToList();
2024-03-14 21:54:43 +04:00
}
2024-03-17 19:36:45 +04:00
public SushiViewModel? GetElement(SushiSearchModel model)
2024-03-14 21:54:43 +04:00
{
2024-03-17 19:36:45 +04:00
if (string.IsNullOrEmpty(model.SushiName) && !model.Id.HasValue)
2024-03-14 21:54:43 +04:00
{
return null;
}
2024-03-17 19:36:45 +04:00
return source.Sushis.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.SushiName) && x.SushiName == model.SushiName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
2024-03-14 21:54:43 +04:00
}
2024-03-17 19:36:45 +04:00
public SushiViewModel? Insert(SushiBindingModel model)
2024-03-14 21:54:43 +04:00
{
2024-03-17 19:36:45 +04:00
model.Id = source.Sushis.Count > 0 ? source.Sushis.Max(x => x.Id) + 1 : 1;
var newSushi = Sushi.Create(model);
if (newSushi == null)
2024-03-14 21:54:43 +04:00
{
return null;
}
2024-03-17 19:36:45 +04:00
source.Sushis.Add(newSushi);
source.SaveSushis();
return newSushi.GetViewModel;
2024-03-14 21:54:43 +04:00
}
2024-03-17 19:36:45 +04:00
public SushiViewModel? Update(SushiBindingModel model)
2024-03-14 21:54:43 +04:00
{
2024-03-17 19:36:45 +04:00
var Sushi = source.Sushis.FirstOrDefault(x => x.Id == model.Id);
if (Sushi == null)
2024-03-14 21:54:43 +04:00
{
2024-03-17 19:36:45 +04:00
return null;
2024-03-14 21:54:43 +04:00
}
2024-03-17 19:36:45 +04:00
Sushi.Update(model);
source.SaveSushis();
return Sushi.GetViewModel;
2024-03-14 21:54:43 +04:00
}
2024-03-17 19:36:45 +04:00
public SushiViewModel? Delete(SushiBindingModel model)
2024-03-14 21:54:43 +04:00
{
2024-03-17 19:36:45 +04:00
var Sushi = source.Sushis.FirstOrDefault(x => x.Id == model.Id);
if (Sushi != null)
2024-03-14 21:54:43 +04:00
{
2024-03-17 19:36:45 +04:00
source.Sushis.Remove(Sushi);
source.SaveSushis();
return Sushi.GetViewModel;
2024-03-14 21:54:43 +04:00
}
2024-03-17 19:36:45 +04:00
return null;
2024-03-14 21:54:43 +04:00
}
}
}