diff --git a/ComputersShop/ComputersShop/Program.cs b/ComputersShop/ComputersShop/Program.cs index d87c4da..44c9fac 100644 --- a/ComputersShop/ComputersShop/Program.cs +++ b/ComputersShop/ComputersShop/Program.cs @@ -1,7 +1,7 @@ using ComputersShopBusinessLogic; using ComputersShopContracts.BusinessLogicsContracts; using ComputersShopContracts.StoragesContracts; -using ComputersShopFileImplement.Implements; +using ComputersShopDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; diff --git a/ComputersShop/ComputersShopDatabaseImplement/Implements/ComponentStorage.cs b/ComputersShop/ComputersShopDatabaseImplement/Implements/ComponentStorage.cs index 2e88eee..ed37758 100644 --- a/ComputersShop/ComputersShopDatabaseImplement/Implements/ComponentStorage.cs +++ b/ComputersShop/ComputersShopDatabaseImplement/Implements/ComponentStorage.cs @@ -6,7 +6,7 @@ using ComputersShopDatabaseImplement.Models; namespace ComputersShopDatabaseImplement.Implements { - internal class ComponentStorage : IComponentStorage + public class ComponentStorage : IComponentStorage { public List GetFullList() { diff --git a/ComputersShop/ComputersShopDatabaseImplement/Implements/ComputerStorage.cs b/ComputersShop/ComputersShopDatabaseImplement/Implements/ComputerStorage.cs index 7f375df..f7527dc 100644 --- a/ComputersShop/ComputersShopDatabaseImplement/Implements/ComputerStorage.cs +++ b/ComputersShop/ComputersShopDatabaseImplement/Implements/ComputerStorage.cs @@ -1,12 +1,102 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; namespace ComputersShopDatabaseImplement.Implements { - internal class ComputerStorage + public class ComputerStorage : IComputerStorage { + public List GetFullList() + { + using var context = new ComputersShopDatabase(); + return context.Computers + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ComputerSearchModel model) + { + if (string.IsNullOrEmpty(model.ComputerName)) + { + return new(); + } + using var context = new ComputersShopDatabase(); + return context.Computers + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.ComputerName.Contains(model.ComputerName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public ComputerViewModel? GetElement(ComputerSearchModel model) + { + if (string.IsNullOrEmpty(model.ComputerName) && + !model.Id.HasValue) + { + return null; + } + using var context = new ComputersShopDatabase(); + return context.Computers + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComputerName) && x.ComputerName == model.ComputerName) || (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + public ComputerViewModel? Insert(ComputerBindingModel model) + { + using var context = new ComputersShopDatabase(); + var newComputer = Computer.Create(context, model); + if (newComputer == null) + { + return null; + } + context.Computers.Add(newComputer); + context.SaveChanges(); + return newComputer.GetViewModel; + } + public ComputerViewModel? Update(ComputerBindingModel model) + { + using var context = new ComputersShopDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var computer = context.Computers.FirstOrDefault(rec => + rec.Id == model.Id); + if (computer == null) + { + return null; + } + computer.Update(model); + context.SaveChanges(); + computer.UpdateComponents(context, model); + transaction.Commit(); + return computer.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public ComputerViewModel? Delete(ComputerBindingModel model) + { + using var context = new ComputersShopDatabase(); + var element = context.Computers + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Computers.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } } } diff --git a/ComputersShop/ComputersShopDatabaseImplement/Implements/OrderStorage.cs b/ComputersShop/ComputersShopDatabaseImplement/Implements/OrderStorage.cs index 9aa9e49..35551be 100644 --- a/ComputersShop/ComputersShopDatabaseImplement/Implements/OrderStorage.cs +++ b/ComputersShop/ComputersShopDatabaseImplement/Implements/OrderStorage.cs @@ -1,12 +1,91 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopDatabaseImplement.Models; namespace ComputersShopDatabaseImplement.Implements { - internal class OrderStorage + public class OrderStorage : IOrderStorage { + public List GetFullList() + { + using var context = new ComputersShopDatabase(); + List orderList = context.Orders + .Select(x => x.GetViewModel) + .ToList(); + foreach (var order in orderList) + { + string manufactureName = context.Computers + .SingleOrDefault(x => x.Id == order.ComputerId)?.ComputerName ?? string.Empty; + order.ComputerName = manufactureName; + } + return orderList; + } + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new ComputersShopDatabase(); + List orderList = context.Orders + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + foreach (var order in orderList) + { + string manufactureName = context.Computers + .SingleOrDefault(x => x.Id == order.ComputerId)?.ComputerName ?? string.Empty; + order.ComputerName = manufactureName; + } + return orderList; + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new ComputersShopDatabase(); + return context.Orders + .FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + } + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new ComputersShopDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return newOrder.GetViewModel; + } + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new ComputersShopDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return order.GetViewModel; + } + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new ComputersShopDatabase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } } } diff --git a/ComputersShop/ComputersShopDatabaseImplement/Models/Order.cs b/ComputersShop/ComputersShopDatabaseImplement/Models/Order.cs index 06721fb..b600817 100644 --- a/ComputersShop/ComputersShopDatabaseImplement/Models/Order.cs +++ b/ComputersShop/ComputersShopDatabaseImplement/Models/Order.cs @@ -20,6 +20,7 @@ namespace ComputersShopDatabaseImplement.Models [Required] public DateTime DateCreate { get; private set; } = DateTime.Now; public DateTime? DateImplement { get; private set; } + public static Order? Create(OrderBindingModel model) { if (model == null)