97 lines
2.7 KiB
C#
Raw Normal View History

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