PIbd-23-Nasyrov-A.G.-Flower.../FlowerShopListImplement/FlowerStorage.cs
antoc0der 6a305bb8df '[
2024-02-12 16:52:44 +03:00

106 lines
3.2 KiB
C#

using FlowerShopContracts.BindingModels;
using FlowerShopContracts.SearchModels;
using FlowerShopContracts.StoragesContracts;
using FlowerShopContracts.ViewModels;
using FlowerShopListImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FlowerShopListImplement.Implements
{
public class FlowerStorage : IFlowerStorage
{
private readonly DataListSingleton _source;
public FlowerStorage()
{
_source = DataListSingleton.GetInstance();
}
public List<FlowerViewModel> GetFullList()
{
var result = new List<FlowerViewModel>();
foreach (var flower in _source.Flowers)
{
result.Add(flower.GetViewModel);
}
return result;
}
public List<FlowerViewModel> GetFilteredList(FlowerSearchModel model)
{
var result = new List<FlowerViewModel>();
if (string.IsNullOrEmpty(model.FlowerName))
{
return result;
}
foreach (var flower in _source.Flowers)
{
if (flower.FlowerName.Contains(model.FlowerName))
{
result.Add(flower.GetViewModel);
}
}
return result;
}
public FlowerViewModel? GetElement(FlowerSearchModel model)
{
if (string.IsNullOrEmpty(model.FlowerName) && !model.Id.HasValue)
{
return null;
}
foreach (var flower in _source.Flowers)
{
if ((!string.IsNullOrEmpty(model.FlowerName) && flower.FlowerName == model.FlowerName) || (model.Id.HasValue && flower.Id == model.Id))
{
return flower.GetViewModel;
}
}
return null;
}
public FlowerViewModel? Insert(FlowerBindingModel model)
{
model.Id = 1;
foreach (var flower in _source.Flowers)
{
if (model.Id <= flower.Id)
{
model.Id = flower.Id + 1;
}
}
var newFlower = Flower.Create(model);
if (newFlower == null)
{
return null;
}
_source.Flowers.Add(newFlower);
return newFlower.GetViewModel;
}
public FlowerViewModel? Update(FlowerBindingModel model)
{
foreach (var flower in _source.Flowers)
{
if (flower.Id == model.Id)
{
flower.Update(model);
return flower.GetViewModel;
}
}
return null;
}
public FlowerViewModel? Delete(FlowerBindingModel model)
{
for (int i = 0; i < _source.Flowers.Count; ++i)
{
if (_source.Flowers[i].Id == model.Id)
{
var element = _source.Flowers[i];
_source.Flowers.RemoveAt(i);
return element.GetViewModel;
}
}
return null;
}
}
}