Comonent Storage TODO
This commit is contained in:
parent
ffd07d70da
commit
c1fdf0b803
@ -11,80 +11,8 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace JewelryStoreFileImplement.Implements
|
||||
{
|
||||
public class ComponentStorage : IComponentStorage
|
||||
public class ComponentStorage : IComponentStorage // TODO Lab_2
|
||||
{
|
||||
private readonly DataFileSingleton _source;
|
||||
public ComponentStorage()
|
||||
{
|
||||
_source = DataFileSingleton.GetInstance();
|
||||
}
|
||||
public List<ComponentViewModel> GetFullList()
|
||||
{
|
||||
return _source.Components.Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel
|
||||
model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.ComponentName))
|
||||
{
|
||||
return new();
|
||||
}
|
||||
return _source.Components
|
||||
.Where(x => x.ComponentName.Contains(model.ComponentName))
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
|
||||
}
|
||||
public ComponentViewModel? GetElement(ComponentSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return _source.Components
|
||||
.FirstOrDefault(x =>
|
||||
(!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) ||
|
||||
(model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
||||
}
|
||||
public ComponentViewModel? Insert(ComponentBindingModel model)
|
||||
{
|
||||
model.Id = _source.Components.Count > 0 ? _source.Components.Max(x =>
|
||||
x.Id) + 1 : 1;
|
||||
var newComponent = Component.Create(model);
|
||||
if (newComponent == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
_source.Components.Add(newComponent);
|
||||
_source.SaveComponents();
|
||||
return newComponent.GetViewModel;
|
||||
}
|
||||
|
||||
public ComponentViewModel? Update(ComponentBindingModel model)
|
||||
{
|
||||
var component = _source.Components.FirstOrDefault(x => x.Id ==
|
||||
model.Id);
|
||||
if (component == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
component.Update(model);
|
||||
_source.SaveComponents();
|
||||
return component.GetViewModel;
|
||||
|
||||
|
||||
}
|
||||
public ComponentViewModel? Delete(ComponentBindingModel model)
|
||||
{
|
||||
var element = _source.Components.FirstOrDefault(x => x.Id ==model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
_source.Components.Remove(element);
|
||||
_source.SaveComponents();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
112
JewelryStoreFileImplement/Implements/JewelStorage.cs
Normal file
112
JewelryStoreFileImplement/Implements/JewelStorage.cs
Normal file
@ -0,0 +1,112 @@
|
||||
using JewelryStoreContracts.BindingModels;
|
||||
using JewelryStoreContracts.SearchModels;
|
||||
using JewelryStoreContracts.StoragesContracts;
|
||||
using JewelryStoreContracts.ViewModels;
|
||||
using JewelryStoreListImplement.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace JewelryStoreListImplement.Implements
|
||||
{
|
||||
public class JewelStorage : IJewelStorage // TODO реализовать интерфейс
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user