131 lines
3.0 KiB
C#
131 lines
3.0 KiB
C#
using SecuritySystemContracts.BindingModels;
|
|
using SecuritySystemContracts.SearchModels;
|
|
using SecuritySystemContracts.StoragesContracts;
|
|
using SecuritySystemContracts.ViewModels;
|
|
using SecuritySystemDatabaseImplement.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace SecuritySystemDatabaseImplement.Implements
|
|
{
|
|
public class ClientStorage : IClientStorage
|
|
{
|
|
public ClientViewModel? Delete(ClientBindingModel model)
|
|
{
|
|
using var context = new SecuritySystemDatabase();
|
|
|
|
var element = context.Clients
|
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
if (element != null)
|
|
{
|
|
// для отображения КОРРЕКТНОЙ ViewModel-и
|
|
var deletedElement = context.Clients
|
|
.Include(x => x.Orders)
|
|
.FirstOrDefault(x => x.Id == model.Id)
|
|
?.GetViewModel;
|
|
|
|
context.Clients.Remove(element);
|
|
context.SaveChanges();
|
|
|
|
return deletedElement;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public ClientViewModel? GetElement(ClientSearchModel model)
|
|
{
|
|
using var context = new SecuritySystemDatabase();
|
|
|
|
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 SecuritySystemDatabase();
|
|
|
|
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 SecuritySystemDatabase();
|
|
|
|
return context.Clients
|
|
.Include(x => x.Orders)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public ClientViewModel? Insert(ClientBindingModel model)
|
|
{
|
|
var newClient = Client.Create(model);
|
|
|
|
if (newClient == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
using var context = new SecuritySystemDatabase();
|
|
|
|
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 SecuritySystemDatabase();
|
|
var order = context.Clients
|
|
.Include(x => x.Orders)
|
|
.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
if (order == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
order.Update(model);
|
|
context.SaveChanges();
|
|
|
|
return context.Clients
|
|
.Include(x => x.Orders)
|
|
.FirstOrDefault(x => x.Id == model.Id)
|
|
?.GetViewModel;
|
|
}
|
|
}
|
|
}
|