93 lines
2.8 KiB
C#
93 lines
2.8 KiB
C#
using DinerContracts.BindingModels;
|
|
using DinerContracts.SearchModels;
|
|
using DinerContracts.StoragesContracts;
|
|
using DinerContracts.ViewModels;
|
|
using DinerDataBaseImplement.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DinerDataBaseImplement.Implements {
|
|
public class ClientStorage : IClientStorage {
|
|
|
|
public ClientViewModel? Insert(ClientBindingModel model) {
|
|
var newClient = Client.Create(model);
|
|
if (newClient == null) {
|
|
return null;
|
|
}
|
|
using var context = new DinerDatabaseBy7Work();
|
|
context.Clients.Add(newClient);
|
|
context.SaveChanges();
|
|
|
|
return context.Clients
|
|
.Include(x => x.Orders)
|
|
.FirstOrDefault(x => x.ID == newClient.ID)
|
|
?.GetViewModel;
|
|
}
|
|
|
|
public ClientViewModel? Update(ClientBindingModel model) {
|
|
using var context = new DinerDatabaseBy7Work();
|
|
var client = context.Clients.FirstOrDefault(x => x.ID == model.ID);
|
|
if (client == null) {
|
|
return null;
|
|
}
|
|
client.Update(model);
|
|
context.SaveChanges();
|
|
return client.GetViewModel;
|
|
}
|
|
|
|
public ClientViewModel? Delete(ClientBindingModel model) {
|
|
using var context = new DinerDatabaseBy7Work();
|
|
var element = context.Clients.FirstOrDefault(x => x.ID == model.ID);
|
|
if (element != null) {
|
|
context.Clients.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public ClientViewModel? GetElement(ClientSearchModel model) {
|
|
using var context = new DinerDatabaseBy7Work();
|
|
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();
|
|
}
|
|
|
|
public List<ClientViewModel> GetFilteredList(ClientSearchModel model) {
|
|
if (string.IsNullOrEmpty(model.ClientFIO)) {
|
|
return new();
|
|
}
|
|
using var context = new DinerDatabaseBy7Work();
|
|
|
|
return context.Clients
|
|
.Include(x => x.Orders)
|
|
.Where(x => x.ClientFIO.Contains(model.ClientFIO))
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public List<ClientViewModel> GetFullList() {
|
|
using var context = new DinerDatabaseBy7Work();
|
|
|
|
return context.Clients
|
|
.Include(x => x.Orders)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
}
|
|
}
|