diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/BuildStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/BuildStorage.cs new file mode 100644 index 0000000..72c2a4b --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/BuildStorage.cs @@ -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 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 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; + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs index 9c74297..ae83b6b 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs @@ -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; } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs index 1675e6a..c4c8f29 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs @@ -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? _buildComponents = null; [NotMapped] public Dictionary BuildComponents @@ -77,7 +75,7 @@ namespace ComputerHardwareStoreDatabaseImplement.Models Price = model.Price; } - public BuildViewModel GetViewModel() => new() + public BuildViewModel GetViewModel => new() { Id = Id, Name = Name,