JewelStorage TODO

This commit is contained in:
VictoriaPresnyakova 2023-02-19 21:21:55 +04:00
parent 233c432ede
commit 0f91591408

View File

@ -2,111 +2,17 @@
using JewelryStoreContracts.SearchModels;
using JewelryStoreContracts.StoragesContracts;
using JewelryStoreContracts.ViewModels;
using JewelryStoreListImplement.Models;
using JewelryStoreFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JewelryStoreListImplement.Implements
namespace JewelryStoreFileImplement.Implements
{
public class JewelStorage : IJewelStorage // TODO реализовать интерфейс
public class JewelStorage : IJewelStorage // TODO Lab_2
{
private readonly DataListSingleton _source;
public JewelStorage()
{
_source = DataListSingleton.GetInstance();
}
public JewelViewModel? Delete(JewelBindingModel model)
{
for (int i = 0; i < _source.Jewels.Count; ++i)
{
if (_source.Jewels[i].Id == model.Id)
{
var element = _source.Jewels[i];
_source.Jewels.RemoveAt(i);
return element.GetViewModel;
}
}
return null;
}
public JewelViewModel? GetElement(JewelSearchModel model)
{
if (string.IsNullOrEmpty(model.JewelName) && !model.Id.HasValue)
{
return null;
}
foreach (var jewel in _source.Jewels)
{
if ((!string.IsNullOrEmpty(model.JewelName) &&
jewel.JewelName == model.JewelName) ||
(model.Id.HasValue && jewel.Id == model.Id))
{
return jewel.GetViewModel;
}
}
return null;
}
public List<JewelViewModel> GetFilteredList(JewelSearchModel model)
{
var result = new List<JewelViewModel>();
if (string.IsNullOrEmpty(model.JewelName))
{
return result;
}
foreach (var jewel in _source.Jewels)
{
if (jewel.JewelName.Contains(model.JewelName))
{
result.Add(jewel.GetViewModel);
}
}
return result;
}
public List<JewelViewModel> GetFullList()
{
var result = new List<JewelViewModel>();
foreach (var jewel in _source.Jewels)
{
result.Add(jewel.GetViewModel);
}
return result;
}
public JewelViewModel? Insert(JewelBindingModel model)
{
model.Id = 1;
foreach (var jewel in _source.Jewels)
{
if (model.Id <= jewel.Id)
{
model.Id = jewel.Id + 1;
}
}
var newJewel = Jewel.Create(model);
if (newJewel == null)
{
return null;
}
_source.Jewels.Add(newJewel);
return newJewel.GetViewModel;
}
public JewelViewModel? Update(JewelBindingModel model)
{
foreach (var jewel in _source.Jewels)
{
if (jewel.Id == model.Id)
{
jewel.Update(model);
return jewel.GetViewModel;
}
}
return null;
}
}
}