2024-05-01 12:51:35 +04:00
|
|
|
|
using DinerContracts.BindingModels;
|
|
|
|
|
using DinerContracts.SearchModels;
|
|
|
|
|
using DinerContracts.StoragesContracts;
|
|
|
|
|
using DinerContracts.ViewModels;
|
2024-05-01 21:05:07 +04:00
|
|
|
|
using DinerDataBaseImplement.Models;
|
2024-05-05 18:00:10 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-05-01 12:51:35 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace DinerDataBaseImplement.Implements {
|
|
|
|
|
public class ClientStorage : IClientStorage {
|
2024-05-01 21:05:07 +04:00
|
|
|
|
|
|
|
|
|
public ClientViewModel? Insert(ClientBindingModel model) {
|
2024-05-11 22:14:52 +04:00
|
|
|
|
var newClient = Client.Create(model);
|
|
|
|
|
if (newClient == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-05-13 16:49:39 +04:00
|
|
|
|
using var context = new DinerDatabaseBy6Work();
|
2024-05-11 22:14:52 +04:00
|
|
|
|
context.Clients.Add(newClient);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
return context.Clients
|
|
|
|
|
.Include(x => x.Orders)
|
|
|
|
|
.FirstOrDefault(x => x.ID == newClient.ID)
|
|
|
|
|
?.GetViewModel;
|
|
|
|
|
}
|
2024-05-01 12:51:35 +04:00
|
|
|
|
|
2024-05-01 21:05:07 +04:00
|
|
|
|
public ClientViewModel? Update(ClientBindingModel model) {
|
2024-05-13 16:49:39 +04:00
|
|
|
|
using var context = new DinerDatabaseBy6Work();
|
2024-05-01 21:05:07 +04:00
|
|
|
|
var client = context.Clients.FirstOrDefault(x => x.ID == model.ID);
|
|
|
|
|
if (client == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
client.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return client.GetViewModel;
|
2024-05-01 12:51:35 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-01 21:05:07 +04:00
|
|
|
|
public ClientViewModel? Delete(ClientBindingModel model) {
|
2024-05-13 16:49:39 +04:00
|
|
|
|
using var context = new DinerDatabaseBy6Work();
|
2024-05-01 21:05:07 +04:00
|
|
|
|
var element = context.Clients.FirstOrDefault(x => x.ID == model.ID);
|
|
|
|
|
if (element != null) {
|
|
|
|
|
context.Clients.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2024-05-01 12:51:35 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-01 21:05:07 +04:00
|
|
|
|
public ClientViewModel? GetElement(ClientSearchModel model) {
|
2024-05-13 16:49:39 +04:00
|
|
|
|
using var context = new DinerDatabaseBy6Work();
|
2024-05-05 18:00:10 +04:00
|
|
|
|
if (model.ID.HasValue) {
|
|
|
|
|
return context.Clients
|
|
|
|
|
.Include(x => x.Orders)
|
|
|
|
|
.FirstOrDefault(x => model.ID.HasValue && x.ID == model.ID)
|
|
|
|
|
?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
else if (!string.IsNullOrEmpty(model.Email) && !string.IsNullOrEmpty(model.Password)) {
|
|
|
|
|
return context.Clients
|
|
|
|
|
.Include(x => x.Orders)
|
|
|
|
|
.FirstOrDefault(x => (x.Email == model.Email && x.Password == model.Password))
|
|
|
|
|
?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
return new();
|
2024-05-01 12:51:35 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-01 21:05:07 +04:00
|
|
|
|
public List<ClientViewModel> GetFilteredList(ClientSearchModel model) {
|
2024-05-11 22:14:52 +04:00
|
|
|
|
if (string.IsNullOrEmpty(model.ClientFIO)) {
|
|
|
|
|
return new();
|
|
|
|
|
}
|
2024-05-13 16:49:39 +04:00
|
|
|
|
using var context = new DinerDatabaseBy6Work();
|
2024-05-11 22:14:52 +04:00
|
|
|
|
|
|
|
|
|
return context.Clients
|
|
|
|
|
.Include(x => x.Orders)
|
|
|
|
|
.Where(x => x.ClientFIO.Contains(model.ClientFIO))
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
2024-05-01 12:51:35 +04:00
|
|
|
|
|
2024-05-01 21:05:07 +04:00
|
|
|
|
public List<ClientViewModel> GetFullList() {
|
2024-05-13 16:49:39 +04:00
|
|
|
|
using var context = new DinerDatabaseBy6Work();
|
2024-05-11 22:14:52 +04:00
|
|
|
|
|
|
|
|
|
return context.Clients
|
|
|
|
|
.Include(x => x.Orders)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
2024-05-01 12:51:35 +04:00
|
|
|
|
}
|
|
|
|
|
}
|