From 65f5de994300c4279cbbb26b6fbf9f29b5733d2d Mon Sep 17 00:00:00 2001 From: Programmist73 Date: Mon, 27 Feb 2023 23:51:07 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20=D1=85?= =?UTF-8?q?=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8=D1=89.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/ManufactureStorage.cs | 127 ++++++++++++++++++ .../Implements/OrderStorage.cs | 125 +++++++++++++++++ .../Implements/WorkPieceStorage.cs | 2 +- 3 files changed, 253 insertions(+), 1 deletion(-) create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/ManufactureStorage.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/OrderStorage.cs diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/ManufactureStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/ManufactureStorage.cs new file mode 100644 index 0000000..2273dda --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/ManufactureStorage.cs @@ -0,0 +1,127 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.StoragesContracts; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopDatabaseImplement.Implements +{ + public class ManufactureStorage : IManufactureStorage + { + public List GetFullList() + { + using var context = new BlacksmithWorkshopDatabase(); + + return context.Manufactures + .Include(x => x.WorkPieces) + .ThenInclude(x => x.WorkPiece) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ManufactureSearchModel model) + { + if (string.IsNullOrEmpty(model.ManufactureName)) + { + return new(); + } + + using var context = new BlacksmithWorkshopDatabase(); + + return context.Manufactures + .Include(x => x.WorkPieces) + .ThenInclude(x => x.WorkPiece) + .Where(x => x.ManufactureName.Contains(model.ManufactureName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public ManufactureViewModel? GetElement(ManufactureSearchModel model) + { + if (string.IsNullOrEmpty(model.ManufactureName) && !model.Id.HasValue) + { + return null; + } + + using var context = new BlacksmithWorkshopDatabase(); + + return context.Manufactures + .Include(x => x.WorkPieces) + .ThenInclude(x => x.WorkPiece) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ManufactureName) && x.ManufactureName == model.ManufactureName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public ManufactureViewModel? Insert(ManufactureBindingModel model) + { + using var context = new BlacksmithWorkshopDatabase(); + var newManufacture = Manufacture.Create(context, model); + + if (newManufacture == null) + { + return null; + } + + context.Manufactures.Add(newManufacture); + context.SaveChanges(); + + return newManufacture.GetViewModel; + } + + public ManufactureViewModel? Update(ManufactureBindingModel model) + { + using var context = new BlacksmithWorkshopDatabase(); + using var transaction = context.Database.BeginTransaction(); + + try + { + var manufacture = context.Manufactures.FirstOrDefault(rec => rec.Id == model.Id); + + if (manufacture == null) + { + return null; + } + + manufacture.Update(model); + context.SaveChanges(); + manufacture.UpdateWorkPieces(context, model); + transaction.Commit(); + + return manufacture.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public ManufactureViewModel? Delete(ManufactureBindingModel model) + { + using var context = new BlacksmithWorkshopDatabase(); + var element = context.Manufactures + .Include(x => x.WorkPieces) + .Include(x => x.Orders) + .FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Manufactures.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/OrderStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..67dceeb --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,125 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.StoragesContracts; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new BlacksmithWorkshopDatabase(); + var element = context.Orders + .FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + + using var context = new BlacksmithWorkshopDatabase(); + + return context.Orders + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel; + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + + using var context = new BlacksmithWorkshopDatabase(); + + return context.Orders + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + + private static OrderViewModel GetViewModel(Order order) + { + var viewModel = order.GetViewModel; + using var context = new BlacksmithWorkshopDatabase(); + var element = context.Manufactures + .FirstOrDefault(x => x.Id == order.ManufactureId); + viewModel.ManufactureName = element.ManufactureName; + + return viewModel; + } + + public List GetFullList() + { + using var context = new BlacksmithWorkshopDatabase(); + + return context.Orders + .Select(x => new OrderViewModel + { + Id = x.Id, + ManufactureId = x.ManufactureId, + Count = x.Count, + Sum = x.Sum, + Status = x.Status, + DateCreate = x.DateCreate, + DateImplement = x.DateImplement, + //ManufactureName = x.Manufacture.ManufactureName //тут не будет работать + }) + .ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + + if (newOrder == null) + { + return null; + } + + using var context = new BlacksmithWorkshopDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + + return newOrder.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new BlacksmithWorkshopDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + + if (order == null) + { + return null; + } + + order.Update(model); + context.SaveChanges(); + + return order.GetViewModel; + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/WorkPieceStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/WorkPieceStorage.cs index fe53ba7..ec2cc00 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/WorkPieceStorage.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/WorkPieceStorage.cs @@ -11,7 +11,7 @@ using System.Threading.Tasks; namespace BlacksmithWorkshopDatabaseImplement.Implements { - internal class WorkPieceStorage : IWorkPieceStorage + public class WorkPieceStorage : IWorkPieceStorage { public List GetFullList() {