ISEbd-22_Alimova_M.S._Confe.../Confectionery/ConfectioneryFileImplement/ImplementerStorage.cs

94 lines
2.5 KiB
C#
Raw Normal View History

using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.SearchModels;
using ConfectioneryContracts.StoragesContracts;
using ConfectioneryContracts.ViewModels;
using ConfectioneryFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConfectioneryFileImplement.Implements
{
public class ImplementerStorage : IImplementerStorage
{
2024-06-03 23:32:39 +04:00
private readonly DataFileSingleton source;
public ImplementerStorage()
{
2024-06-03 23:32:39 +04:00
source = DataFileSingleton.GetInstance();
}
public List<ImplementerViewModel> GetFullList()
{
2024-06-03 23:32:39 +04:00
return source.Implementers
.Select(x => x.GetViewModel)
.ToList();
}
public List<ImplementerViewModel> GetFilteredList(ImplementerSearchModel model)
{
2024-06-03 23:32:39 +04:00
if (string.IsNullOrEmpty(model.ImplementerFIO))
{
return new();
}
2024-06-03 23:32:39 +04:00
return source.Implementers
.Where(x => x.ImplementerFIO.Contains(model.ImplementerFIO))
.Select(x => x.GetViewModel)
.ToList();
}
public ImplementerViewModel? GetElement(ImplementerSearchModel model)
{
2024-06-03 23:32:39 +04:00
if (string.IsNullOrEmpty(model.ImplementerFIO) && string.IsNullOrEmpty(model.Password) && !model.Id.HasValue)
{
2024-06-03 23:32:39 +04:00
return null;
}
2024-06-03 23:32:39 +04:00
return source.Implementers
.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.ImplementerFIO) && x.ImplementerFIO == model.ImplementerFIO
&& (string.IsNullOrEmpty(model.Password) || x.Password == model.Password))
|| (model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public ImplementerViewModel? Insert(ImplementerBindingModel model)
{
2024-06-03 23:32:39 +04:00
model.Id = source.Implementers.Count > 0 ? source.Implementers.Max(x => x.Id) + 1 : 1;
var newImplementer = Implementer.Create(model);
if (newImplementer == null)
{
2024-06-03 23:32:39 +04:00
return null;
}
2024-06-03 23:32:39 +04:00
source.Implementers.Add(newImplementer);
source.SaveImplementers();
return newImplementer.GetViewModel;
}
public ImplementerViewModel? Update(ImplementerBindingModel model)
{
2024-06-03 23:32:39 +04:00
var implementer = source.Implementers.FirstOrDefault(x => x.Id == model.Id);
if (implementer == null)
{
2024-06-03 23:32:39 +04:00
return null;
}
2024-06-03 23:32:39 +04:00
implementer.Update(model);
source.SaveImplementers();
return implementer.GetViewModel;
}
public ImplementerViewModel? Delete(ImplementerBindingModel model)
{
2024-06-03 23:32:39 +04:00
var element = source.Implementers.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
2024-06-03 23:32:39 +04:00
source.Implementers.Remove(element);
source.SaveImplementers();
return element.GetViewModel;
}
2024-06-03 23:32:39 +04:00
return null;
}
}
}