PIbd-22_Smirnov_A.A._Securi.../SecuritySystem/SecuritySystemDatabaseImplement/Implements/ClientStorage.cs

137 lines
3.1 KiB
C#
Raw Normal View History

2024-05-08 08:03:51 +04:00
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
{
2024-05-08 09:27:00 +04:00
public class ClientStorage : IClientStorage
{
public ClientViewModel? Delete(ClientBindingModel model)
{
2024-05-08 08:03:51 +04:00
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;
}
2024-05-08 09:27:00 +04:00
public ClientViewModel? GetElement(ClientSearchModel model)
{
2024-05-08 08:03:51 +04:00
using var context = new SecuritySystemDatabase();
if (model.Id.HasValue)
{
return context.Clients
.Include(x => x.Orders)
2024-05-08 09:27:00 +04:00
.FirstOrDefault(x => x.Id == model.Id)
2024-05-08 08:03:51 +04:00
?.GetViewModel;
}
2024-05-08 09:27:00 +04:00
if (!string.IsNullOrEmpty(model.Email) && !string.IsNullOrEmpty(model.Password))
2024-05-08 08:03:51 +04:00
{
return context.Clients
.Include(x => x.Orders)
.FirstOrDefault(x => (x.Email == model.Email && x.Password == model.Password))
?.GetViewModel;
}
2024-05-08 09:27:00 +04:00
if (!string.IsNullOrEmpty(model.Email))
return context.Clients
.FirstOrDefault(x => x.Email == model.Email)
?.GetViewModel;
return null;
2024-05-08 08:03:51 +04:00
}
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();
}
2024-05-08 09:27:00 +04:00
public List<ClientViewModel> GetFullList()
{
2024-05-08 08:03:51 +04:00
using var context = new SecuritySystemDatabase();
return context.Clients
.Include(x => x.Orders)
.Select(x => x.GetViewModel)
.ToList();
}
2024-05-08 09:27:00 +04:00
public ClientViewModel? Insert(ClientBindingModel model)
{
2024-05-08 08:03:51 +04:00
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;
}
2024-05-08 09:27:00 +04:00
public ClientViewModel? Update(ClientBindingModel model)
{
2024-05-08 08:03:51 +04:00
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;
}
2024-05-08 09:27:00 +04:00
}
2024-05-08 08:03:51 +04:00
}