2024-04-28 20:26:49 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using SushiBarContracts.BindingModels;
|
|
|
|
|
using SushiBarContracts.SearchModels;
|
|
|
|
|
using SushiBarContracts.StoragesContracts;
|
|
|
|
|
using SushiBarContracts.ViewModels;
|
|
|
|
|
using SushiBarDatabaseImplement.Models;
|
|
|
|
|
|
|
|
|
|
namespace SushiBarDatabaseImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
public class ClientStorage : IClientStorage
|
|
|
|
|
{
|
|
|
|
|
public List<ClientViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new SushiBarDatabase();
|
|
|
|
|
return context.Clients
|
|
|
|
|
.Select(c => c.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new SushiBarDatabase();
|
|
|
|
|
return context.Clients
|
2024-04-28 20:26:49 +04:00
|
|
|
|
.Where(c =>
|
2024-05-05 13:46:10 +04:00
|
|
|
|
(!model.Id.HasValue || c.Id == model.Id) &&
|
|
|
|
|
(string.IsNullOrEmpty(model.ClientFIO) || c.ClientFIO.Contains(model.ClientFIO)) &&
|
|
|
|
|
(string.IsNullOrEmpty(model.Email) || c.Email.Contains(model.Email)) &&
|
|
|
|
|
(string.IsNullOrEmpty(model.Password) || c.Password.Contains(model.Password)))
|
|
|
|
|
.Select(c => c.GetViewModel)
|
2024-04-28 20:26:49 +04:00
|
|
|
|
.ToList();
|
2024-04-28 20:26:49 +04:00
|
|
|
|
}
|
|
|
|
|
public ClientViewModel? GetElement(ClientSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new SushiBarDatabase();
|
|
|
|
|
return context.Clients
|
2024-05-05 13:46:10 +04:00
|
|
|
|
.FirstOrDefault(c =>
|
|
|
|
|
(!model.Id.HasValue || c.Id == model.Id) &&
|
|
|
|
|
(string.IsNullOrEmpty(model.ClientFIO) || c.ClientFIO.Contains(model.ClientFIO)) &&
|
|
|
|
|
(string.IsNullOrEmpty(model.Email) || c.Email.Contains(model.Email)) &&
|
|
|
|
|
(string.IsNullOrEmpty(model.Password) || c.Password.Contains(model.Password)))?
|
2024-04-28 20:26:49 +04:00
|
|
|
|
.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
using var transaction = context.Database.BeginTransaction();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var client = context.Clients
|
|
|
|
|
.FirstOrDefault(c => c.Id == model.Id);
|
|
|
|
|
if (client == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
client.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
return client.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public ClientViewModel? Delete(ClientBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new SushiBarDatabase();
|
|
|
|
|
var element = context.Clients
|
|
|
|
|
.FirstOrDefault(c => c.Id == model.Id);
|
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
context.Clients.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|