build storage

This commit is contained in:
bekodeg 2024-05-01 11:35:35 +04:00
parent 1ae74a6fdb
commit 09dcc021c8
3 changed files with 113 additions and 8 deletions

View File

@ -0,0 +1,107 @@
using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.SearchModels;
using ComputerHardwareStoreContracts.StorageContracts;
using ComputerHardwareStoreContracts.ViewModels;
using ComputerHardwareStoreDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace ComputerHardwareStoreDatabaseImplement.Implements
{
public class BuildStorage : IBuildStorage
{
public List<BuildViewModel> GetFullList()
{
using var context = new ComputerHardwareStoreDBContext();
return context.Builds
.Include(b => b.Comments)
.Include(b => b.Components)
.ThenInclude(b => b.Component)
.Select(b => b.GetViewModel)
.ToList();
}
public List<BuildViewModel> GetFilteredList(BuildSearchModel model)
{
if (string.IsNullOrEmpty(model.Name))
{
return new();
}
using var context = new ComputerHardwareStoreDBContext();
return context.Builds
.Where(b => b.Name.Contains(model.Name))
.Include(b => b.Comments)
.Include(b => b.Components)
.ThenInclude(b => b.Component)
.Select(b => b.GetViewModel)
.ToList();
}
public BuildViewModel? GetElement(BuildSearchModel model)
{
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
{
return null;
}
using var context = new ComputerHardwareStoreDBContext();
return context.Builds
.Include(b => b.Comments)
.Include(b => b.Components)
.ThenInclude(b => b.Component)
.FirstOrDefault(b =>
(!string.IsNullOrEmpty(model.Name) && b.Name == model.Name) ||
(model.Id.HasValue && b.Id == model.Id))?
.GetViewModel;
}
public BuildViewModel? Insert(BuildBindingModel model)
{
using var context = new ComputerHardwareStoreDBContext();
var newBuild = Build.Create(context, model);
if (newBuild == null)
{
return null;
}
context.Builds.Add(newBuild);
context.SaveChanges();
return context.Builds
.Include(b => b.Comments)
.Include(b => b.Components)
.ThenInclude(b => b.Component)
.FirstOrDefault(p => p.Id == newBuild.Id)?
.GetViewModel;
}
public BuildViewModel? Update(BuildBindingModel model)
{
using var context = new ComputerHardwareStoreDBContext();
var build = context.Builds
.Include(b => b.Comments)
.Include(b => b.Components)
.ThenInclude(b => b.Component)
.FirstOrDefault(p => p.Id == model.Id);
if (build == null)
{
return null;
}
context.Update(model);
context.SaveChanges();
return build.GetViewModel;
}
public BuildViewModel? Delete(BuildBindingModel model)
{
using var context = new ComputerHardwareStoreDBContext();
var build = context.Builds
.Include(b => b.Comments)
.Include(b => b.Components)
.ThenInclude(b => b.Component)
.FirstOrDefault(p => p.Id == model.Id);
if (build == null)
{
return null;
}
context.Builds.Remove(build);
context.SaveChanges();
return build.GetViewModel;
}
}
}

View File

@ -44,9 +44,9 @@ namespace ComputerHardwareStoreDatabaseImplement.Implements
return context.Products
.Include(p => p.Components)
.ThenInclude(p => p.Component)
.FirstOrDefault(c =>
(!string.IsNullOrEmpty(model.Name) && c.Name == model.Name) ||
(model.Id.HasValue && c.Id == model.Id))?
.FirstOrDefault(p =>
(!string.IsNullOrEmpty(model.Name) && p.Name == model.Name) ||
(model.Id.HasValue && p.Id == model.Id))?
.GetViewModel;
}

View File

@ -1,11 +1,9 @@

using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.BindingModels;
using ComputerHardwareStoreContracts.ViewModels;
using ComputerHardwareStoreDataModels.Models;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Runtime.InteropServices;
namespace ComputerHardwareStoreDatabaseImplement.Models
{
@ -16,7 +14,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Models
public string Name { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
[NotMapped]
private Dictionary<int, (IComponentModel, int)>? _buildComponents = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> BuildComponents
@ -77,7 +75,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Models
Price = model.Price;
}
public BuildViewModel GetViewModel() => new()
public BuildViewModel GetViewModel => new()
{
Id = Id,
Name = Name,