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() {