90 lines
3.0 KiB
C#
90 lines
3.0 KiB
C#
using ElectronicsShopContracts.BindingModels;
|
|
using ElectronicsShopContracts.SearchModels;
|
|
using ElectronicsShopContracts.StorageContracts;
|
|
using ElectronicsShopContracts.ViewModels;
|
|
using ElectronicsShopDataBaseImplement.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Query.Internal;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace ElectronicsShopDataBaseImplement.Implements
|
|
{
|
|
public class ClientStorage : IClientStorage
|
|
{
|
|
public ClientViewModel? Insert(ClientBindingModel model) {
|
|
var newComponent = Client.Create(model);
|
|
if (newComponent == null) {
|
|
return null;
|
|
}
|
|
using var context = new Database();
|
|
context.Clients.Add(newComponent);
|
|
context.SaveChanges();
|
|
return newComponent.GetViewModel;
|
|
}
|
|
|
|
public ClientViewModel? Update(ClientBindingModel model) {
|
|
using var context = new Database();
|
|
var component = context.Clients.FirstOrDefault(x => x.ID == model.ID);
|
|
if (component == null) {
|
|
return null;
|
|
}
|
|
component.Update(model);
|
|
context.SaveChanges();
|
|
return component.GetViewModel;
|
|
}
|
|
|
|
public ClientViewModel? Delete(ClientBindingModel model)
|
|
{
|
|
using var context = new Database();
|
|
var element = context.Clients.FirstOrDefault(rec => rec.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 Database();
|
|
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.Passwrod)) {
|
|
return context.Clients
|
|
.Include (x => x.Orders)
|
|
.FirstOrDefault(x => (x.Email == model.Email && x.Password == model.Passwrod))
|
|
?.GetViewModel;
|
|
}
|
|
return new();
|
|
}
|
|
|
|
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.Email))
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new Database();
|
|
return context.Clients.Where(x => !string.IsNullOrEmpty(model.ClientFIO) && x.ClientFIO
|
|
.Contains(model.ClientFIO)).Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public List<ClientViewModel> GetFullList()
|
|
{
|
|
using var context = new Database();
|
|
return context.Clients.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
}
|
|
}
|