102 lines
3.1 KiB
C#
102 lines
3.1 KiB
C#
using DressAtelierContracts.BindingModels;
|
|
using DressAtelierContracts.SearchModels;
|
|
using DressAtelierContracts.StorageContracts;
|
|
using DressAtelierContracts.ViewModels;
|
|
using DressAtelierFileImplement.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DressAtelierFileImplement.Implements
|
|
{
|
|
public class AtelierStorage : IAtelierStorage
|
|
{
|
|
private readonly DataFileSingleton _source;
|
|
|
|
public AtelierStorage()
|
|
{
|
|
_source = DataFileSingleton.GetInstance();
|
|
}
|
|
|
|
public AtelierViewModel? Insert(AtelierBindingModel model)
|
|
{
|
|
model.ID = _source.Ateliers.Count() > 0 ? _source.Ateliers.Max(x => x.ID) + 1 : 1;
|
|
|
|
var newAtelier = Atelier.Create(model);
|
|
if(newAtelier == null)
|
|
{
|
|
return null;
|
|
}
|
|
_source.Ateliers.Add(newAtelier);
|
|
_source.SaveAteliers();
|
|
return newAtelier.GetViewModel;
|
|
}
|
|
|
|
public AtelierViewModel? Update(AtelierBindingModel model)
|
|
{
|
|
var atelier = _source.Ateliers.FirstOrDefault(x => x.ID == model.ID);
|
|
if(atelier == null)
|
|
{
|
|
return null;
|
|
}
|
|
atelier.Update(model);
|
|
_source.SaveAteliers();
|
|
return atelier.GetViewModel;
|
|
}
|
|
public AtelierViewModel? Delete(AtelierBindingModel model)
|
|
{
|
|
var atelier = _source.Ateliers.FirstOrDefault(x => x.ID == model.ID);
|
|
if (atelier == null)
|
|
{
|
|
return null;
|
|
}
|
|
_source.Ateliers.Remove(atelier);
|
|
_source.SaveAteliers();
|
|
return atelier.GetViewModel;
|
|
}
|
|
|
|
public AtelierViewModel? GetElement(AtelierSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.Name) && !model.ID.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
if(!string.IsNullOrEmpty(model.Name))
|
|
{
|
|
return _source.Ateliers.FirstOrDefault(x => x.Name.Equals(model.Name))?.GetViewModel;
|
|
}
|
|
|
|
if (model.ID.HasValue)
|
|
{
|
|
return _source.Ateliers.FirstOrDefault(x => x.ID == model.ID)?.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public List<AtelierViewModel> GetFilteredList(AtelierSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.Name) && !model.DressID.HasValue)
|
|
{
|
|
return new();
|
|
}
|
|
|
|
if(!string.IsNullOrEmpty(model.Name))
|
|
{
|
|
return _source.Ateliers.Where(x => x.Name.Equals(model.Name)).Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
return _source.Ateliers.Where(x => x.DressesList.ContainsKey((int)model.DressID)).Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public List<AtelierViewModel> GetFullList()
|
|
{
|
|
return _source.Ateliers.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
|
|
}
|
|
}
|