PIbd-21_Bakalskaya_E.D._Sus.../SushiBarDatabaseImplement/Implements/ClientStorage.cs
2024-04-08 10:32:05 +03:00

85 lines
2.8 KiB
C#

using Microsoft.IdentityModel.Tokens;
using SushiBarContracts.BindingModel;
using SushiBarContracts.SearchModel;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushiBarDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SushiBarDatabaseImplement.Implements
{
public class ClientStorage : IClientStorage
{
public ClientViewModel? Insert(ClientBindingModel model)
{
using var context = new SushiBarDatabase();
var newClient = Client.Create(model);
if (newClient == null) { return null; }
context.Clients.Add(newClient);
context.SaveChanges();
return newClient.GetViewModel;
}
public ClientViewModel? Update(ClientBindingModel model)
{
using var context = new SushiBarDatabase();
var client = context.Clients.FirstOrDefault(x => x.Id == model.Id);
if (client == null) { return null; };
client.Update(model);
context.SaveChanges();
return client.GetViewModel;
}
public ClientViewModel? Delete(ClientBindingModel model)
{
using var context = new SushiBarDatabase();
var element = context.Clients.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Clients.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public ClientViewModel? GetElement(ClientSearchModel model)
{
return GetFilteredList(model).FirstOrDefault();
}
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
{
using var context = new SushiBarDatabase();
var clients = context.Clients.Select(x => x.GetViewModel).ToList();
if (model.Id.HasValue)
{
clients = clients.Where(x => x.Id == model.Id.Value).ToList();
}
if (!model.Email.IsNullOrEmpty())
{
clients = clients.Where(x => x.Email == model.Email).ToList();
}
if (!model.Password.IsNullOrEmpty())
{
clients = clients.Where(x => x.Password == model.Password).ToList();
}
if (!model.ClientFIO.IsNullOrEmpty())
{
clients = clients.Where(x => x.ClientFIO == model.ClientFIO).ToList();
}
return clients;
}
public List<ClientViewModel> GetFullList()
{
using var context = new SushiBarDatabase();
return context.Clients.Select(x => x.GetViewModel).ToList();
}
}
}