111 lines
2.8 KiB
C#
111 lines
2.8 KiB
C#
using System.ComponentModel;
|
|
using System.Reflection;
|
|
using BeautySalonContracts.BindingModels;
|
|
using BeautySalonContracts.SearchModels;
|
|
using BeautySalonContracts.StorageContracts;
|
|
using BeautySalonContracts.ViewModels;
|
|
using BeautySalonDatabaseImplement.Models;
|
|
using BeautySalonListImplement;
|
|
|
|
namespace BeautySalonDatabaseImplement.Implements
|
|
{
|
|
public class CosmeticStorage : ICosmeticStorage
|
|
{
|
|
private readonly DataListSingleton _source;
|
|
public CosmeticStorage()
|
|
{
|
|
_source = DataListSingleton.GetInstance();
|
|
}
|
|
public List<CosmeticViewModel> GetFilteredList(CosmeticSearchModel model)
|
|
{
|
|
var result = new List<CosmeticViewModel>();
|
|
if (string.IsNullOrEmpty(model.Name))
|
|
{
|
|
return result;
|
|
}
|
|
foreach (var cosmetic in _source.Cosmetics)
|
|
{
|
|
if (cosmetic.Name.Contains(model.Name))
|
|
{
|
|
result.Add(cosmetic.GetViewModel);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public List<CosmeticViewModel> GetFullList()
|
|
{
|
|
var result = new List<CosmeticViewModel>();
|
|
|
|
foreach (var cosmetic in _source.Cosmetics)
|
|
{
|
|
result.Add(cosmetic.GetViewModel);
|
|
}
|
|
return result;
|
|
}
|
|
public CosmeticViewModel? GetElement(CosmeticSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
foreach(var cosmetic in _source.Cosmetics)
|
|
{
|
|
if ((!string.IsNullOrEmpty(model.Name) && cosmetic.Name == model.Name) || (cosmetic.Id == model.Id && model.Id.HasValue))
|
|
{
|
|
return cosmetic.GetViewModel;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public CosmeticViewModel? Insert(CosmeticBindingModel model)
|
|
{
|
|
model.Id = 1;
|
|
foreach (var cosmetic in _source.Cosmetics)
|
|
{
|
|
if (model.Id == cosmetic.Id)
|
|
{
|
|
model.Id = cosmetic.Id + 1;
|
|
}
|
|
}
|
|
var newCosmetic = Cosmetic.Create(model);
|
|
if (newCosmetic == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
_source.Cosmetics.Add(newCosmetic);
|
|
return newCosmetic.GetViewModel;
|
|
}
|
|
|
|
public CosmeticViewModel? Update(CosmeticBindingModel model)
|
|
{
|
|
foreach (var cosmetic in _source.Cosmetics)
|
|
{
|
|
if (cosmetic.Id == model.Id)
|
|
{
|
|
cosmetic.Update(model);
|
|
return cosmetic.GetViewModel;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public CosmeticViewModel? Delete(CosmeticBindingModel model)
|
|
{
|
|
for (int i = 0; i < _source.Cosmetics.Count; ++i)
|
|
{
|
|
if (_source.Cosmetics[i].Id == model.Id)
|
|
{
|
|
var deleted = _source.Cosmetics[i];
|
|
_source.Cosmetics.RemoveAt(i);
|
|
return deleted.GetViewModel;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
}
|
|
}
|
|
|