CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/ClientStorage.cs

96 lines
3.2 KiB
C#
Raw Normal View History

2024-04-29 01:55:20 +04:00
using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.SearchModels;
using ElectronicsShopContracts.StorageContracts;
using ElectronicsShopContracts.ViewModels;
using ElectronicsShopDataBaseImplement.Models;
2024-05-26 17:47:24 +04:00
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query.Internal;
2024-04-29 01:55:20 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ElectronicsShopDataBaseImplement.Implements
{
2024-05-25 13:06:38 +04:00
public class ClientStorage : IClientStorage
2024-04-29 01:55:20 +04:00
{
2024-05-26 17:47:24 +04:00
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)
2024-04-29 01:55:20 +04:00
{
using var context = new Database();
2024-05-25 13:06:38 +04:00
var element = context.Clients.FirstOrDefault(rec => rec.ID == model.ID);
2024-04-29 01:55:20 +04:00
if (element != null)
{
2024-05-25 13:06:38 +04:00
context.Clients.Remove(element);
2024-04-29 01:55:20 +04:00
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
2024-05-19 16:35:35 +04:00
public ClientViewModel? GetElement(ClientSearchModel model)
2024-04-29 01:55:20 +04:00
{
using var context = new Database();
2024-05-26 17:47:24 +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.Passwrod)) {
return context.Clients
.Include (x => x.Orders)
.FirstOrDefault(x => (x.Email == model.Email && x.Password == model.Passwrod))
?.GetViewModel;
}
return new();
2024-04-29 01:55:20 +04:00
}
2024-05-19 16:35:35 +04:00
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
2024-04-29 01:55:20 +04:00
{
2024-05-25 13:06:38 +04:00
if (string.IsNullOrEmpty(model.Email))
2024-04-29 01:55:20 +04:00
{
return new();
}
using var context = new Database();
2024-05-26 17:47:24 +04:00
return context.Clients
.Include(x => x.Orders)
.Where(x => x.Email
.Contains(model.Email))
.Select(x => x.GetViewModel).ToList();
2024-04-29 01:55:20 +04:00
}
2024-05-19 16:35:35 +04:00
public List<ClientViewModel> GetFullList()
2024-04-29 01:55:20 +04:00
{
using var context = new Database();
2024-05-26 17:47:24 +04:00
return context.Clients
.Include(x => x.Orders)
.Select(x => x.GetViewModel)
.ToList();
2024-04-29 01:55:20 +04:00
}
}
}