2024-05-22 02:01:59 +04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using PizzeriaContracts.BindingModels;
|
2024-05-07 23:00:38 +04:00
|
|
|
|
using PizzeriaContracts.SearchModels;
|
|
|
|
|
using PizzeriaContracts.StorageContracts;
|
|
|
|
|
using PizzeriaContracts.ViewModels;
|
|
|
|
|
using PizzeriaDatabaseImplement.Models;
|
|
|
|
|
|
|
|
|
|
namespace PizzeriaDatabaseImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
public class ClientStorage : IClientStorage
|
|
|
|
|
{
|
|
|
|
|
public List<ClientViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new PizzeriaDatabase();
|
|
|
|
|
return context.Clients.Select(x => x.GetViewModel).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
|
|
|
|
|
{
|
2024-05-22 02:01:59 +04:00
|
|
|
|
if (string.IsNullOrEmpty(model.ClientFIO))
|
2024-05-07 23:00:38 +04:00
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
using var context = new PizzeriaDatabase();
|
2024-05-22 02:01:59 +04:00
|
|
|
|
return context.Clients.Where(x => x.ClientFIO.Contains(model.ClientFIO)).Select(x => x.GetViewModel).ToList();
|
2024-05-07 23:00:38 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ClientViewModel? GetElement(ClientSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(model.Email) && string.IsNullOrEmpty(model.Password) && !model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new PizzeriaDatabase();
|
|
|
|
|
return context.Clients.FirstOrDefault(x =>
|
2024-05-22 02:01:59 +04:00
|
|
|
|
(!string.IsNullOrEmpty(model.Email) && x.Email == model.Email && !string.IsNullOrEmpty(model.Password) && x.Password == model.Password) ||
|
|
|
|
|
(model.Id.HasValue && x.Id == model.Id))
|
|
|
|
|
?.GetViewModel;
|
2024-05-07 23:00:38 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ClientViewModel? Insert(ClientBindingModel model)
|
|
|
|
|
{
|
2024-05-22 02:01:59 +04:00
|
|
|
|
var newComponent = Client.Create(model);
|
|
|
|
|
if (newComponent == null)
|
2024-05-07 23:00:38 +04:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new PizzeriaDatabase();
|
2024-05-22 02:01:59 +04:00
|
|
|
|
context.Clients.Add(newComponent);
|
2024-05-07 23:00:38 +04:00
|
|
|
|
context.SaveChanges();
|
2024-05-22 02:01:59 +04:00
|
|
|
|
return newComponent.GetViewModel;
|
2024-05-07 23:00:38 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ClientViewModel? Update(ClientBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new PizzeriaDatabase();
|
2024-05-22 02:01:59 +04:00
|
|
|
|
var component = context.Clients.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (component == null)
|
2024-05-07 23:00:38 +04:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-05-22 02:01:59 +04:00
|
|
|
|
component.Update(model);
|
2024-05-07 23:00:38 +04:00
|
|
|
|
context.SaveChanges();
|
2024-05-22 02:01:59 +04:00
|
|
|
|
return component.GetViewModel;
|
2024-05-07 23:00:38 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ClientViewModel? Delete(ClientBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new PizzeriaDatabase();
|
2024-05-22 02:01:59 +04:00
|
|
|
|
var element = context.Clients.FirstOrDefault(rec => rec.Id == model.Id);
|
2024-05-07 23:00:38 +04:00
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
context.Clients.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-22 02:01:59 +04:00
|
|
|
|
}
|