108 lines
3.3 KiB
C#
108 lines
3.3 KiB
C#
|
using Microsoft.EntityFrameworkCore;
|
|||
|
using RestaurantContracts.BindingModels;
|
|||
|
using RestaurantContracts.SearchModels;
|
|||
|
using RestaurantContracts.StoragesContracts;
|
|||
|
using RestaurantContracts.ViewModels;
|
|||
|
using RestaurantDatabaseImplement.Models;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace RestaurantDatabaseImplement.Implements
|
|||
|
{
|
|||
|
public class ClientStorage : IClientStorage
|
|||
|
{
|
|||
|
public ClientViewModel? Delete(ClientBindingModel model)
|
|||
|
{
|
|||
|
using var context = new RestaurantDatabase();
|
|||
|
var element = context.Clients
|
|||
|
.FirstOrDefault(x => x.Id == model.Id);
|
|||
|
if (element == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
context.Clients.Remove(element);
|
|||
|
return element.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
public ClientViewModel? GetElement(ClientSearchModel model)
|
|||
|
{
|
|||
|
if (!model.Id.HasValue)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
using var context = new RestaurantDatabase();
|
|||
|
return context.Clients
|
|||
|
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
|
|||
|
{
|
|||
|
using var context = new RestaurantDatabase();
|
|||
|
if (model.Id.HasValue)
|
|||
|
{
|
|||
|
return context.Clients
|
|||
|
.Where(x => x.Id == model.Id)
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
else if (model.Number != null)
|
|||
|
{
|
|||
|
return context.Clients
|
|||
|
.Where(x => x.Number == model.Number)
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
else if (model.FirstName != null && model.LastName != null)
|
|||
|
{
|
|||
|
return context.Clients
|
|||
|
.Where(x => (x.FirstName == model.FirstName && x.LastName == model.LastName))
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
return new();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public List<ClientViewModel> GetFullList()
|
|||
|
{
|
|||
|
using var context = new RestaurantDatabase();
|
|||
|
return context.Clients
|
|||
|
.ToList()
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
|
|||
|
public ClientViewModel? Insert(ClientBindingModel model)
|
|||
|
{
|
|||
|
var newClient = Client.Create(model);
|
|||
|
if (newClient == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
using var context = new RestaurantDatabase();
|
|||
|
context.Clients.Add(newClient);
|
|||
|
context.SaveChanges();
|
|||
|
return newClient.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
public ClientViewModel? Update(ClientBindingModel model)
|
|||
|
{
|
|||
|
using var context = new RestaurantDatabase();
|
|||
|
var element = context.Clients
|
|||
|
.FirstOrDefault(x => x.Id == model.Id);
|
|||
|
if (element == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
element.Update(model);
|
|||
|
context.SaveChanges();
|
|||
|
return element.GetViewModel;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|