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

100 lines
2.7 KiB
C#
Raw Normal View History

2024-05-08 11:24:46 +04:00
using FurnitureAssemblyContracts.BindingModels;
using FurnitureAssemblyContracts.SearchModels;
using FurnitureAssemblyContracts.StoragesContracts;
using FurnitureAssemblyContracts.ViewModels;
2024-05-13 23:32:57 +04:00
using FurnitureAssemblyFileImplement.Models;
2024-05-08 11:24:46 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FurnitureAssemblyFileImplement.Implements
{
public class ClientStorage : IClientStorage
{
private readonly DataFileSingleton source;
2024-05-08 11:24:46 +04:00
public ClientStorage()
{
source = DataFileSingleton.GetInstance();
}
public ClientViewModel? GetElement(ClientSearchModel model)
{
if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue)
{
return null;
}
return source.Clients
2024-05-22 10:14:48 +04:00
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Email) && x.Email == model.Email)
|| (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
2024-05-08 11:24:46 +04:00
}
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
{
if (string.IsNullOrEmpty(model.Email))
2024-05-08 11:24:46 +04:00
{
return new();
}
2024-05-08 11:24:46 +04:00
return source.Clients
.Where(x => x.Email.Contains(model.Email))
.Select(x => x.GetViewModel)
.ToList();
2024-05-08 11:24:46 +04:00
}
public List<ClientViewModel> GetFullList()
{
return source.Clients.Select(x => x.GetViewModel).ToList();
2024-05-08 11:24:46 +04:00
}
public ClientViewModel? Insert(ClientBindingModel model)
{
model.Id = source.Clients.Count > 0 ? source.Clients.Max(x => x.Id) + 1 : 1;
2024-05-08 11:24:46 +04:00
var newClient = Client.Create(model);
2024-05-08 11:24:46 +04:00
if (newClient == null)
{
return null;
}
2024-05-08 11:24:46 +04:00
source.Clients.Add(newClient);
source.SaveClients();
2024-05-08 11:24:46 +04:00
return newClient.GetViewModel;
}
public ClientViewModel? Update(ClientBindingModel model)
{
var client = source.Clients.FirstOrDefault(x => x.Id == model.Id);
2024-05-08 11:24:46 +04:00
if (client == null)
{
return null;
}
2024-05-08 11:24:46 +04:00
client.Update(model);
source.SaveClients();
return client?.GetViewModel;
}
public ClientViewModel? Delete(ClientBindingModel model)
{
var client = source.Clients.FirstOrDefault(x => x.Id == model.Id);
if (client != null)
{
source.Clients.Remove(client);
source.SaveClients();
return client?.GetViewModel;
}
return null;
2024-05-08 11:24:46 +04:00
}
}
}