CourseWork_CompShop/ComputerShopProvider/ComputerShopDatabaseImplement/Implements/AssemblyStorage.cs

123 lines
4.1 KiB
C#
Raw Normal View History

2023-04-05 19:08:51 +04:00
using ComputerShopContracts.BindingModels;
using ComputerShopContracts.SearchModels;
using ComputerShopContracts.StorageContracts;
using ComputerShopContracts.ViewModels;
using ComputerShopDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComputerShopDatabaseImplement.Implements
{
2023-04-07 16:53:49 +04:00
public class AssemblyStorage : IAssemblyStorage
2023-04-05 19:08:51 +04:00
{
public List<AssemblyViewModel> GetFullList()
{
using var context = new ComputerShopDatabase();
return context.Assemblies
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<AssemblyViewModel> GetFilteredList(AssemblySearchModel model)
{
2023-05-17 12:19:00 +04:00
if (string.IsNullOrEmpty(model.AssemblyName) && model.ClientId == null)
2023-04-05 19:08:51 +04:00
{
return new();
}
using var context = new ComputerShopDatabase();
2023-05-17 12:19:00 +04:00
if (model.ClientId != null)
return context.Assemblies
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Where(x => x.ClientId == model.ClientId)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
else
{
return context.Assemblies
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Where(x => x.AssemblyName.Contains(model.AssemblyName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
2023-04-05 19:08:51 +04:00
}
public AssemblyViewModel? GetElement(AssemblySearchModel model)
{
if (string.IsNullOrEmpty(model.AssemblyName) &&
!model.Id.HasValue)
{
return null;
}
using var context = new ComputerShopDatabase();
return context.Assemblies
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.AssemblyName) &&
x.AssemblyName == model.AssemblyName) ||
(model.Id.HasValue && x.Id ==
model.Id))
?.GetViewModel;
}
public AssemblyViewModel? Insert(AssemblyBindingModel model)
{
using var context = new ComputerShopDatabase();
var newProduct = Assembly.Create(context, model);
if (newProduct == null)
{
return null;
}
context.Assemblies.Add(newProduct);
context.SaveChanges();
return newProduct.GetViewModel;
}
public AssemblyViewModel? Update(AssemblyBindingModel model)
{
using var context = new ComputerShopDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var product = context.Assemblies.FirstOrDefault(rec =>
rec.Id == model.Id);
if (product == null)
{
return null;
}
product.Update(model);
context.SaveChanges();
product.UpdateComponents(context, model);
transaction.Commit();
return product.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public AssemblyViewModel? Delete(AssemblyBindingModel model)
{
using var context = new ComputerShopDatabase();
var element = context.Assemblies
.Include(x => x.Components)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Assemblies.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}