PIbd-22_Gerimovich.I.M._Fur.../FurnitureAssembly/FurnitureAssemblyFileImplement/Implements/FurnitureStorage.cs

98 lines
2.9 KiB
C#
Raw Normal View History

2024-03-27 00:02:22 +04:00
using FurnitureAssemblyContracts.BindingModels;
using FurnitureAssemblyContracts.SearchModels;
using FurnitureAssemblyContracts.StoragesContracts;
using FurnitureAssemblyContracts.ViewModels;
using FurnitureAssemblyFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FurnitureAssemblyFileImplement.Implements
{
2024-05-22 10:14:48 +04:00
// Реализация интерфейса хранилища изделий
2024-03-27 00:02:22 +04:00
public class FurnitureStorage : IFurnitureStorage
{
private readonly DataFileSingleton source;
public FurnitureStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<FurnitureViewModel> GetFullList()
{
return source.Furnitures.Select(x => x.GetViewModel).ToList();
}
public List<FurnitureViewModel> GetFilteredList(FurnitureSearchModel model)
{
2024-05-22 10:14:48 +04:00
if(string.IsNullOrEmpty(model.FurnitureName))
2024-03-27 00:02:22 +04:00
{
return new();
}
return source.Furnitures.Where(x => x.FurnitureName.Contains(model.FurnitureName)).Select(x => x.GetViewModel).ToList();
}
public FurnitureViewModel? GetElement(FurnitureSearchModel model)
{
2024-05-22 10:14:48 +04:00
if(string.IsNullOrEmpty(model.FurnitureName) && !model.Id.HasValue)
2024-03-27 00:02:22 +04:00
{
return null;
}
return source.Furnitures.FirstOrDefault(x => (!string.IsNullOrEmpty(model.FurnitureName) && x.FurnitureName == model.FurnitureName)
|| (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public FurnitureViewModel? Insert(FurnitureBindingModel model)
{
model.Id = source.Furnitures.Count > 0 ? source.Furnitures.Max(x => x.Id) + 1 : 1;
var newFurniture = Furniture.Create(model);
2024-05-22 10:14:48 +04:00
if(newFurniture == null)
2024-03-27 00:02:22 +04:00
{
return null;
}
source.Furnitures.Add(newFurniture);
source.SaveFurnitures();
return newFurniture.GetViewModel;
}
public FurnitureViewModel? Update(FurnitureBindingModel model)
{
var furniture = source.Furnitures.FirstOrDefault(x => x.Id == model.Id);
2024-05-22 10:14:48 +04:00
if(furniture == null)
2024-03-27 00:02:22 +04:00
{
return null;
}
furniture.Update(model);
source.SaveFurnitures();
return furniture.GetViewModel;
}
public FurnitureViewModel? Delete(FurnitureBindingModel model)
{
var element = source.Furnitures.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
source.Furnitures.Remove(element);
source.SaveFurnitures();
return element.GetViewModel;
}
return null;
}
}
}