platoff aeeee 2cefd91025 12
2024-05-24 13:05:47 +04:00

97 lines
2.7 KiB
C#

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
{
public class UsersStorage : IUsersStorage
{
private readonly DataFileSingleton source;
public UsersStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<FurnitureViewModel> GetFullList()
{
return source.Furnitures.Select(x => x.GetViewModel).ToList();
}
public List<FurnitureViewModel> GetFilteredList(UsersSearchModel model)
{
if (string.IsNullOrEmpty(model.UsersName))
{
return new();
}
return source.Furnitures.Where(x => x.UsersName.Contains(model.UsersName)).Select(x => x.GetViewModel).ToList();
}
public FurnitureViewModel? GetElement(UsersSearchModel model)
{
if (string.IsNullOrEmpty(model.UsersName) && !model.Id.HasValue)
{
return null;
}
return source.Furnitures.FirstOrDefault(x => (!string.IsNullOrEmpty(model.UsersName) && x.UsersName == model.UsersName)
|| (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public FurnitureViewModel? Insert(UsersBindingModel model)
{
model.Id = source.Furnitures.Count > 0 ? source.Furnitures.Max(x => x.Id) + 1 : 1;
var newFurniture = Users.Create(model);
if (newFurniture == null)
{
return null;
}
source.Furnitures.Add(newFurniture);
source.SaveFurnitures();
return newFurniture.GetViewModel;
}
public FurnitureViewModel? Update(UsersBindingModel model)
{
var furniture = source.Furnitures.FirstOrDefault(x => x.Id == model.Id);
if (furniture == null)
{
return null;
}
furniture.Update(model);
source.SaveFurnitures();
return furniture.GetViewModel;
}
public FurnitureViewModel? Delete(UsersBindingModel 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;
}
}
}