2023-04-08 19:57:10 +04:00
|
|
|
|
using BankContracts.BindingModels;
|
|
|
|
|
using BankContracts.SearchModels;
|
|
|
|
|
using BankContracts.StoragesContracts;
|
|
|
|
|
using BankContracts.ViewModels;
|
|
|
|
|
using BankDataBaseImplement.Models;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace BankDataBaseImplement.Implemets
|
|
|
|
|
{
|
2023-04-09 12:14:52 +04:00
|
|
|
|
public class ClientStorage : IClientStorage
|
2023-04-08 19:57:10 +04:00
|
|
|
|
{
|
2023-04-08 22:43:54 +04:00
|
|
|
|
public ClientViewModel? Delete(ClientBindingModel model)
|
2023-04-08 19:57:10 +04:00
|
|
|
|
{
|
|
|
|
|
using var context = new BankDataBase();
|
|
|
|
|
|
2023-04-09 12:14:52 +04:00
|
|
|
|
var element = context.Clients.FirstOrDefault(rec => rec.Id == model.Id);
|
2023-04-08 19:57:10 +04:00
|
|
|
|
|
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
2023-04-09 12:14:52 +04:00
|
|
|
|
context.Clients.Remove(element);
|
2023-04-08 19:57:10 +04:00
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-08 22:43:54 +04:00
|
|
|
|
public ClientViewModel? GetElement(ClientSearchModel model)
|
2023-04-08 19:57:10 +04:00
|
|
|
|
{
|
|
|
|
|
using var context = new BankDataBase();
|
|
|
|
|
|
|
|
|
|
if (model.Id.HasValue)
|
2023-04-09 12:14:52 +04:00
|
|
|
|
return context.Clients
|
2023-04-08 19:57:10 +04:00
|
|
|
|
.Include(x => x.ConferenceBookings)
|
|
|
|
|
.Include(x => x.Dinners)
|
|
|
|
|
.Include(x => x.Rooms)
|
|
|
|
|
.FirstOrDefault(x => x.Id == model.Id)?
|
|
|
|
|
.GetViewModel;
|
|
|
|
|
|
2023-04-09 12:14:52 +04:00
|
|
|
|
if (!string.IsNullOrEmpty(model.ClientLogin) && !string.IsNullOrEmpty(model.ClientPassword))
|
|
|
|
|
return context.Clients
|
2023-04-08 19:57:10 +04:00
|
|
|
|
.Include(x => x.ConferenceBookings)
|
|
|
|
|
.Include(x => x.Dinners)
|
|
|
|
|
.Include(x => x.Rooms)
|
2023-04-09 12:14:52 +04:00
|
|
|
|
.FirstOrDefault(x => x.ClientLogin.Equals(model.ClientLogin) && x.ClientPassword.Equals(model.ClientPassword))?
|
2023-04-08 19:57:10 +04:00
|
|
|
|
.GetViewModel;
|
|
|
|
|
|
2023-04-09 12:14:52 +04:00
|
|
|
|
if (!string.IsNullOrEmpty(model.ClientLogin))
|
|
|
|
|
return context.Clients
|
2023-04-08 19:57:10 +04:00
|
|
|
|
.Include(x => x.ConferenceBookings)
|
|
|
|
|
.Include(x => x.Dinners)
|
|
|
|
|
.Include(x => x.Rooms)
|
2023-04-09 12:14:52 +04:00
|
|
|
|
.FirstOrDefault(x => x.ClientLogin.Equals(model.ClientLogin))?
|
2023-04-08 19:57:10 +04:00
|
|
|
|
.GetViewModel;
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-08 22:43:54 +04:00
|
|
|
|
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
|
2023-04-08 19:57:10 +04:00
|
|
|
|
{
|
2023-04-09 12:14:52 +04:00
|
|
|
|
if (string.IsNullOrEmpty(model.ClientFIO))
|
2023-04-08 19:57:10 +04:00
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using var context = new BankDataBase();
|
|
|
|
|
|
2023-04-09 12:14:52 +04:00
|
|
|
|
return context.Clients
|
2023-04-08 19:57:10 +04:00
|
|
|
|
.Include(x => x.ConferenceBookings)
|
|
|
|
|
.Include(x => x.Dinners)
|
|
|
|
|
.Include(x => x.Rooms)
|
2023-04-09 12:14:52 +04:00
|
|
|
|
.Where(x => x.ClientLogin.Contains(model.ClientLogin) && x.ClientPassword == model.ClientPassword)
|
2023-04-08 19:57:10 +04:00
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-08 22:43:54 +04:00
|
|
|
|
public List<ClientViewModel> GetFullList()
|
2023-04-08 19:57:10 +04:00
|
|
|
|
{
|
|
|
|
|
using var context = new BankDataBase();
|
|
|
|
|
|
2023-04-09 12:14:52 +04:00
|
|
|
|
return context.Clients
|
2023-04-08 19:57:10 +04:00
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-08 22:43:54 +04:00
|
|
|
|
public ClientViewModel? Insert(ClientBindingModel model)
|
2023-04-08 19:57:10 +04:00
|
|
|
|
{
|
2023-04-08 22:43:54 +04:00
|
|
|
|
var newHeadwaiter = Client.Create(model);
|
2023-04-08 19:57:10 +04:00
|
|
|
|
|
|
|
|
|
if (newHeadwaiter == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using var context = new BankDataBase();
|
|
|
|
|
|
2023-04-09 12:14:52 +04:00
|
|
|
|
context.Clients.Add(newHeadwaiter);
|
2023-04-08 19:57:10 +04:00
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
return newHeadwaiter.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-08 22:43:54 +04:00
|
|
|
|
public ClientViewModel? Update(ClientBindingModel model)
|
2023-04-08 19:57:10 +04:00
|
|
|
|
{
|
|
|
|
|
using var context = new BankDataBase();
|
|
|
|
|
|
2023-04-09 12:14:52 +04:00
|
|
|
|
var headwaiter = context.Clients
|
2023-04-08 19:57:10 +04:00
|
|
|
|
.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
|
|
|
|
|
if (headwaiter == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
headwaiter.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
return headwaiter.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|