PIbd-23_Ivanov_V.N._Pizzeria/Pizzeria/PizzeriaFileImplement/Implements/ImplementerStorage.cs

95 lines
3.1 KiB
C#
Raw Normal View History

2024-03-28 21:32:49 +04:00
using PizzeriaContracts.BindingModels;
using PizzeriaContracts.SearchModels;
using PizzeriaContracts.StoragesContracts;
using PizzeriaContracts.ViewModels;
2024-04-02 17:02:41 +04:00
using PizzeriaFileImplement.Models;
2024-03-28 21:32:49 +04:00
namespace PizzeriaFileImplement.Implements
{
public class ImplementerStorage : IImplementerStorage
{
2024-04-02 17:02:41 +04:00
private readonly DataFileSingleton _source;
public ImplementerStorage()
2024-03-28 21:32:49 +04:00
{
2024-04-02 17:02:41 +04:00
_source = DataFileSingleton.GetInstance();
2024-03-28 21:32:49 +04:00
}
2024-04-02 17:02:41 +04:00
public List<ImplementerViewModel> GetFullList()
2024-03-28 21:32:49 +04:00
{
2024-04-02 17:02:41 +04:00
return _source.Implementers.Select(x => x.GetViewModel).ToList();
2024-03-28 21:32:49 +04:00
}
public List<ImplementerViewModel> GetFilteredList(ImplementerSearchModel model)
{
2024-04-02 17:02:41 +04:00
if (model == null)
{
return new();
}
if (model.Id.HasValue)
{
var res = GetElement(model);
return res != null ? new() { res } : new();
}
if (model.ImplementerFIO != null)
{
return _source.Implementers
.Where(x => x.ImplementerFIO.Equals(model.ImplementerFIO))
.Select(x => x.GetViewModel)
.ToList();
}
return new();
2024-03-28 21:32:49 +04:00
}
2024-04-02 17:02:41 +04:00
public ImplementerViewModel? GetElement(ImplementerSearchModel model)
2024-03-28 21:32:49 +04:00
{
2024-04-02 17:02:41 +04:00
if (model.Id.HasValue)
{
return _source.Implementers.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
if (model.ImplementerFIO != null && model.Password != null)
{
return _source.Implementers.FirstOrDefault(x => x.ImplementerFIO.Equals(model.ImplementerFIO) && x.Password.Equals(model.Password))?.GetViewModel;
}
if (model.ImplementerFIO != null)
{
return _source.Implementers.FirstOrDefault(x => x.ImplementerFIO.Equals(model.ImplementerFIO))?.GetViewModel;
}
return null;
2024-03-28 21:32:49 +04:00
}
public ImplementerViewModel? Insert(ImplementerBindingModel model)
{
2024-04-02 17:02:41 +04:00
model.Id = _source.Implementers.Count > 0 ? _source.Implementers.Max(x => x.Id) + 1 : 1;
var res = Implementer.Create(model);
if (res != null)
{
_source.Implementers.Add(res);
_source.SaveImplementers();
}
return res?.GetViewModel;
2024-03-28 21:32:49 +04:00
}
public ImplementerViewModel? Update(ImplementerBindingModel model)
{
2024-04-02 17:02:41 +04:00
var res = _source.Implementers.FirstOrDefault(x => x.Id == model.Id);
if (res != null)
{
res.Update(model);
_source.SaveImplementers();
}
return res?.GetViewModel;
}
public ImplementerViewModel? Delete(ImplementerBindingModel model)
{
var res = _source.Implementers.FirstOrDefault(x => x.Id == model.Id);
if (res != null)
{
_source.Implementers.Remove(res);
_source.SaveImplementers();
}
return res?.GetViewModel;
2024-03-28 21:32:49 +04:00
}
}
}