2024-04-08 00:44:32 +04:00
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
|
using SushiBarContracts.BindingModel;
|
2024-04-06 15:55:02 +04:00
|
|
|
|
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();
|
2024-04-08 11:32:05 +04:00
|
|
|
|
var element = context.Clients.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
context.Clients.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2024-04-06 15:55:02 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ClientViewModel? GetElement(ClientSearchModel model)
|
|
|
|
|
{
|
2024-04-08 00:44:32 +04:00
|
|
|
|
return GetFilteredList(model).FirstOrDefault();
|
2024-04-06 15:55:02 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new SushiBarDatabase();
|
2024-04-08 00:44:32 +04:00
|
|
|
|
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;
|
2024-04-06 15:55:02 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ClientViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new SushiBarDatabase();
|
|
|
|
|
return context.Clients.Select(x => x.GetViewModel).ToList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|