Guarantor: Storages for entities were implemented.

This commit is contained in:
Yuee Shiness 2023-04-06 21:13:19 +04:00
parent e65286870e
commit 81673b0767
4 changed files with 334 additions and 0 deletions

View File

@ -0,0 +1,79 @@
using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.SearchModels;
using ComputerStoreContracts.StorageContracts;
using ComputerStoreContracts.ViewModels;
using ComputerStoreDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerStoreDatabaseImplement.Implements
{
public class ComponentStorage : IComponentStorage
{
public ComponentViewModel? GetElement(ComponentSearchModel model)
{
if(string.IsNullOrEmpty(model.Name) && !model.ID.HasValue)
{
return null;
}
using var context = new ComputerStoreDatabase();
return context.Components.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && model.Name == x.Name) || (model.ID.HasValue && model.ID ==x.ID))?.GetViewModel;
}
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel model)
{
if(string.IsNullOrEmpty(model.Name))
{
return new();
}
using var context = new ComputerStoreDatabase();
return context.Components.Where(x => x.Name.Equals(model.Name)).Select(x => x.GetViewModel).ToList();
}
public List<ComponentViewModel> GetFullList()
{
using var context = new ComputerStoreDatabase();
return context.Components.Select(x => x.GetViewModel).ToList();
}
public ComponentViewModel? Insert(ComponentBindingModel model)
{
using var context = new ComputerStoreDatabase();
var newComponent = Component.Create(model);
if(newComponent == null) { return null; }
context.Components.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public ComponentViewModel? Update(ComponentBindingModel model)
{
using var context = new ComputerStoreDatabase();
var specComponent = context.Components.FirstOrDefault(x => x.ID == model.ID);
if(specComponent == null) { return null; }
specComponent.Update(model);
context.SaveChanges();
return specComponent.GetViewModel;
}
public ComponentViewModel? Delete(ComponentBindingModel model)
{
using var context = new ComputerStoreDatabase();
var specComponent = context.Components.FirstOrDefault(x => x.ID==model.ID);
if(specComponent == null) { return null; }
context.Components.Remove(specComponent);
context.SaveChanges();
return specComponent.GetViewModel;
}
}
}

View File

@ -0,0 +1,79 @@
using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.SearchModels;
using ComputerStoreContracts.StorageContracts;
using ComputerStoreContracts.ViewModels;
using ComputerStoreDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerStoreDatabaseImplement.Implements
{
public class EmployeeStorage : IEmployeeStorage
{
public EmployeeViewModel? GetElement(EmployeeSearchModel model)
{
if(string.IsNullOrEmpty(model.Username) && !model.ID.HasValue)
{
return null;
}
using var context = new ComputerStoreDatabase();
return context.Employees.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Username) && model.Username == x.Username) || (model.ID.HasValue && model.ID == x.ID))?.GetViewModel;
}
public List<EmployeeViewModel> GetFilteredList(EmployeeSearchModel model)
{
if (string.IsNullOrEmpty(model.Username))
{
return new();
}
using var context = new ComputerStoreDatabase();
return context.Employees.Where(x => x.Username.Equals(model.Username)).Select(x => x.GetViewModel).ToList();
}
public List<EmployeeViewModel> GetFullList()
{
using var context = new ComputerStoreDatabase();
return context.Employees.Select(x => x.GetViewModel).ToList();
}
public EmployeeViewModel? Insert(EmployeeBindingModel model)
{
using var context = new ComputerStoreDatabase();
var newEmployee = Employee.Create(model);
if(newEmployee == null) { return null; }
context.Employees.Add(newEmployee);
context.SaveChanges();
return newEmployee.GetViewModel;
}
public EmployeeViewModel? Update(EmployeeBindingModel model)
{
using var context = new ComputerStoreDatabase();
var specEmployee = context.Employees.FirstOrDefault(x => x.ID == model.ID);
if (specEmployee == null) { return null; }
specEmployee.Update(model);
context.SaveChanges();
return specEmployee.GetViewModel;
}
public EmployeeViewModel? Delete(EmployeeBindingModel model)
{
using var context = new ComputerStoreDatabase();
var specEmployee = context.Employees.FirstOrDefault(x => x.ID == model.ID);
if (specEmployee == null) { return null; }
context.Employees.Remove(specEmployee);
context.SaveChanges();
return specEmployee.GetViewModel;
}
}
}

View File

@ -0,0 +1,87 @@
using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.SearchModels;
using ComputerStoreContracts.StorageContracts;
using ComputerStoreContracts.ViewModels;
using ComputerStoreDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerStoreDatabaseImplement.Implements
{
public class PCStorage : IPCStorage
{
public PCViewModel? GetElement(PCSearchModel model)
{
if (string.IsNullOrEmpty(model.Name) && !model.ID.HasValue) { return null; }
using var context = new ComputerStoreDatabase();
return context.PCs.Include(x => x.Components).ThenInclude(x => x.Component).FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && model.Name.Equals(x.Name)) || (model.ID.HasValue && model.ID == x.ID))?.GetViewModel;
}
public List<PCViewModel> GetFilteredList(PCSearchModel model)
{
if (string.IsNullOrEmpty(model.Name))
{
return new();
}
using var context = new ComputerStoreDatabase();
return context.PCs.Include(x => x.Components).ThenInclude(x => x.Component).Where(x => x.ID == model.ID).ToList().Select(x => x.GetViewModel).ToList();
}
public List<PCViewModel> GetFullList()
{
using var context = new ComputerStoreDatabase();
return context.PCs.Include(x => x.Components).ThenInclude(x => x.Component).ToList().Select(x => x.GetViewModel).ToList();
}
public PCViewModel? Insert(PCBindingModel model)
{
using var context = new ComputerStoreDatabase();
var newPC = PC.Create(context, model);
if(newPC == null) { return null; }
context.PCs.Add(newPC);
context.SaveChanges();
return newPC.GetViewModel;
}
public PCViewModel? Update(PCBindingModel model)
{
using var context = new ComputerStoreDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var specPC = context.PCs.FirstOrDefault(x => x.ID == model.ID);
if(specPC == null) { return null; }
specPC.Update(model);
context.SaveChanges();
specPC.UpdateComponents(context, model);
transaction.Commit();
return specPC.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public PCViewModel? Delete(PCBindingModel model)
{
using var context = new ComputerStoreDatabase();
var specPC = context.PCs.Include(x => x.Components).FirstOrDefault(x => x.ID == model.ID);
if(specPC == null) { return null;}
context.PCs.Remove(specPC);
context.SaveChanges();
return specPC.GetViewModel;
}
}
}

View File

@ -0,0 +1,89 @@
using ComputerStoreContracts.BindingModels;
using ComputerStoreContracts.SearchModels;
using ComputerStoreContracts.StorageContracts;
using ComputerStoreContracts.ViewModels;
using ComputerStoreDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerStoreDatabaseImplement.Implements
{
public class ProductStorage : IProductStorage
{
public ProductViewModel? GetElement(ProductSearchModel model)
{
if(string.IsNullOrEmpty(model.Name) && !model.ID.HasValue) { return null; }
using var context = new ComputerStoreDatabase();
return context.Products.Include(x => x.Components).ThenInclude(x => x.Component).FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && model.Name.Equals(x.Name)) || (model.ID.HasValue && model.ID == x.ID))?.GetViewModel;
}
public List<ProductViewModel> GetFilteredList(ProductSearchModel model)
{
if(string.IsNullOrEmpty(model.Name))
{
return new();
}
using var context = new ComputerStoreDatabase();
return context.Products.Include(x => x.Components).ThenInclude(x => x.Component).Where(x => x.ID ==model.ID).Select(x => x.GetViewModel).ToList();
}
public List<ProductViewModel> GetFullList()
{
using var context = new ComputerStoreDatabase();
return context.Products.Include(x => x.Components).ThenInclude(x => x.Component).ToList().Select(x => x.GetViewModel).ToList();
}
public ProductViewModel? Insert(ProductBindingModel model)
{
using var context = new ComputerStoreDatabase();
var newProduct = Product.Create(context, model);
if(newProduct == null) { return null; }
context.Products.Add(newProduct);
context.SaveChanges();
return newProduct.GetViewModel;
}
public ProductViewModel? Update(ProductBindingModel model)
{
using var context = new ComputerStoreDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var specProduct = context.Products.FirstOrDefault(x => x.ID==model.ID);
if(specProduct == null) { return null; }
specProduct.Update(model);
context.SaveChanges();
specProduct.UpdateComponents(context, model);
transaction.Commit();
return specProduct.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public ProductViewModel? Delete(ProductBindingModel model)
{
using var context = new ComputerStoreDatabase();
var specProduct = context.Products.Include(x => x.Components).FirstOrDefault(x => x.ID == model.ID);
if(specProduct == null) { return null; }
context.Products.Remove(specProduct);
context.SaveChanges();
return specProduct.GetViewModel;
}
}
}