Jewel Storage_1

This commit is contained in:
VictoriaPresnyakova 2023-02-06 16:31:17 +04:00
parent bed4ff1872
commit db02f3c6d1

View File

@ -2,6 +2,7 @@
using JewelryStoreContracts.SearchModels; using JewelryStoreContracts.SearchModels;
using JewelryStoreContracts.StoragesContracts; using JewelryStoreContracts.StoragesContracts;
using JewelryStoreContracts.ViewModels; using JewelryStoreContracts.ViewModels;
using JewelryStoreListImplement.Models;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -12,34 +13,100 @@ namespace JewelryStoreListImplement.Implements
{ {
public class JewelStorage : IJewelStorage // TODO реализовать интерфейс public class JewelStorage : IJewelStorage // TODO реализовать интерфейс
{ {
private readonly DataListSingleton _source;
public JewelStorage()
{
_source = DataListSingleton.GetInstance();
}
public JewelViewModel? Delete(JewelBindingModel model) public JewelViewModel? Delete(JewelBindingModel model)
{ {
throw new NotImplementedException(); 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) public JewelViewModel? GetElement(JewelSearchModel model)
{ {
throw new NotImplementedException(); 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) public List<JewelViewModel> GetFilteredList(JewelSearchModel model)
{ {
throw new NotImplementedException(); 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() public List<JewelViewModel> GetFullList()
{ {
throw new NotImplementedException(); var result = new List<JewelViewModel>();
foreach (var jewel in _source.Jewels)
{
result.Add(jewel.GetViewModel);
}
return result;
} }
public JewelViewModel? Insert(JewelBindingModel model) public JewelViewModel? Insert(JewelBindingModel model)
{ {
throw new NotImplementedException(); 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) public JewelViewModel? Update(JewelBindingModel model)
{ {
throw new NotImplementedException(); foreach (var jewel in _source.Jewels)
{
if (jewel.Id == model.Id)
{
jewel.Update(model);
return jewel.GetViewModel;
}
}
return null;
} }
} }
} }