83 lines
2.7 KiB
C#
Raw Normal View History

2024-02-27 20:12:40 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TypographyContracts.BindingModels;
using TypographyContracts.SearchModels;
using TypographyContracts.StoragesContracts;
using TypographyContracts.ViewModels;
using TypographyFileImplement.Models;
namespace TypographyFileImplement.Implements
{
public class PrintedStorage : IPrintedStorage
{
private readonly DataFileSingleton source;
public PrintedStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<PrintedViewModel> GetFullList()
{
return source.Printeds.Select(x => x.GetViewModel).ToList();
}
public List<PrintedViewModel> GetFilteredList(PrintedSearchModel model)
{
if (string.IsNullOrEmpty(model.PrintedName))
{
return new();
}
return source.Printeds.Where(x => x.PrintedName.Contains(model.PrintedName)).Select(x => x.GetViewModel).ToList();
}
public PrintedViewModel? GetElement(PrintedSearchModel model)
{
if (string.IsNullOrEmpty(model.PrintedName) && !model.Id.HasValue)
{
return null;
}
return source.Printeds.FirstOrDefault(x => (!string.IsNullOrEmpty(model.PrintedName) &&
x.PrintedName == model.PrintedName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public PrintedViewModel? Insert(PrintedBindingModel model)
{
model.Id = source.Printeds.Count > 0 ? source.Printeds.Max(x => x.Id) + 1 : 1;
var newPrinted = Printed.Create(model);
if (newPrinted == null)
{
return null;
}
source.Printeds.Add(newPrinted);
source.SavePrinteds();
return newPrinted.GetViewModel;
}
public PrintedViewModel? Update(PrintedBindingModel model)
{
var printed = source.Printeds.FirstOrDefault(x => x.Id == model.Id);
if (printed == null)
{
return null;
}
printed.Update(model);
source.SavePrinteds();
return printed.GetViewModel;
}
public PrintedViewModel? Delete(PrintedBindingModel model)
{
var element = source.Printeds.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
source.Printeds.Remove(element);
source.SavePrinteds();
return element.GetViewModel;
}
return null;
}
}
}