diff --git a/Factory/FactoryDatabaseImplement/Implements/PlanProductionStorage.cs b/Factory/FactoryDatabaseImplement/Implements/PlanProductionStorage.cs index 040004e..aa06610 100644 --- a/Factory/FactoryDatabaseImplement/Implements/PlanProductionStorage.cs +++ b/Factory/FactoryDatabaseImplement/Implements/PlanProductionStorage.cs @@ -4,6 +4,7 @@ using FactoryContracts.SearchModels; using FactoryContracts.StoragesContracts; using FactoryContracts.ViewModels; using FactoryDatabaseImplement.Models; +using System.Reflection.PortableExecutable; namespace FactoryDatabaseImplement.Implements { @@ -97,19 +98,30 @@ namespace FactoryDatabaseImplement.Implements public PlanProductionViewModel? Update(PlanProductionBindingModel model) { using var context = new FactoryDatabase(); - var order = context.PlanProductions - .Include(x => x.Client) - .Include(x => x.Workpieces) - .ThenInclude(x => x.Workpiece) - .FirstOrDefault(x => x.Id == model.Id); - if (order == null) + using var transaction = context.Database.BeginTransaction(); + try { - return null; - } - order.Update(model); - context.SaveChanges(); - return order.GetViewModel; - } + var plan = context.PlanProductions + .Include(x => x.Client) + .Include(x => x.Workpieces) + .ThenInclude(x => x.Workpiece) + .FirstOrDefault(x => x.Id == model.Id); + if (plan == null) + { + return null; + } + plan.Update(model); + context.SaveChanges(); + plan.UpdateWorkpieces(context, model); + transaction.Commit(); + return plan.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } } } \ No newline at end of file diff --git a/Factory/FactoryDatabaseImplement/Models/PlanProduction.cs b/Factory/FactoryDatabaseImplement/Models/PlanProduction.cs index d7ffa90..5195162 100644 --- a/Factory/FactoryDatabaseImplement/Models/PlanProduction.cs +++ b/Factory/FactoryDatabaseImplement/Models/PlanProduction.cs @@ -84,11 +84,12 @@ namespace FactoryDatabaseImplement.Models }; public void UpdateWorkpieces(FactoryDatabase context, PlanProductionBindingModel model) { + var planProductionWorkpieces = context.PlanProductionWorkpieces.Where(rec => rec.PlanProductionId == model.Id).ToList(); - if (planProductionWorkpieces != null && planProductionWorkpieces.Count > 0) + if (planProductionWorkpieces != null && planProductionWorkpieces.Count > 0 && model.PlanProductionWorkpieces.Count > 0) { context.PlanProductionWorkpieces.RemoveRange(planProductionWorkpieces.Where(rec => !model.PlanProductionWorkpieces.ContainsKey(rec.WorkpieceId))); - context.SaveChanges(); + context.SaveChanges(); foreach (var updateWorkpiece in planProductionWorkpieces) { updateWorkpiece.Count = model.PlanProductionWorkpieces[updateWorkpiece.WorkpieceId].Item2; diff --git a/Factory/FactoryDatabaseImplement/Models/PlanProductionWorkpiece.cs b/Factory/FactoryDatabaseImplement/Models/PlanProductionWorkpiece.cs index 2edc666..b184689 100644 --- a/Factory/FactoryDatabaseImplement/Models/PlanProductionWorkpiece.cs +++ b/Factory/FactoryDatabaseImplement/Models/PlanProductionWorkpiece.cs @@ -15,8 +15,8 @@ namespace FactoryDatabaseImplement.Models [Required] public int Count { get; set; } - public virtual PlanProduction PlanProduction { get; set; } = new(); + public virtual PlanProduction PlanProduction { get; set; } - public virtual Workpiece Workpiece { get; set; } = new(); + public virtual Workpiece Workpiece { get; set; } } } \ No newline at end of file diff --git a/Factory/FactoryWorkerApp/Controllers/HomeController.cs b/Factory/FactoryWorkerApp/Controllers/HomeController.cs index a181e1c..350c207 100644 --- a/Factory/FactoryWorkerApp/Controllers/HomeController.cs +++ b/Factory/FactoryWorkerApp/Controllers/HomeController.cs @@ -155,6 +155,8 @@ namespace FactoryWorkerApp.Controllers [HttpGet] public IActionResult PlanProduction(int id) { + var workpieces = _logic.GetWorkpieces(Client.user!.Id); + ViewBag.AllWorkpieces = workpieces; if (id != 0) { var value = _logic.GetPlanProduction(id); @@ -186,27 +188,39 @@ namespace FactoryWorkerApp.Controllers { if (!IsLoggedIn) return RedirectToAction("Index"); - var plan = _logic.GetPlanProduction(id); - ViewBag.PlanProduction = plan; - var workpieces = _logic.GetWorkpieces(id); - return View(workpieces); + ViewBag.PlanProductions = _logic.GetPlanProductions(Client.user!.Id); + ViewBag.Workpieces = _logic.GetWorkpieces(Client.user!.Id); + return View(); } [HttpPost] - public IActionResult ConnectionPlanProductionWorkpiece(int planProductionId, int workpieceId) + public void ConnectionPlanProductionWorkpiece(int planId, int workpieceId, int count) { if (!IsLoggedIn) - return RedirectToAction("Index"); - var plan = _logic.GetPlanProduction(planProductionId); - if (plan == null) - return RedirectToAction("Index"); - PlanProductionBindingModel productBinding = new() { Id = planProductionId, Count = plan.Count, Deadline = plan.Deadline, ClientId= plan.ClientId, ProductionName= plan.ProductionName, PlanProductionWorkpieces = plan.PlanProductionWorkpieces}; - //productBinding.PlanProductionWorkpieces.Add(_logic.GetWorkpiece(workpieceId)); - _logic.UpdatePlanProduction(productBinding); - return RedirectToAction("Index"); + Response.Redirect("Index"); + PlanProductionViewModel planProduction = _logic.GetPlanProduction(planId)!; + WorkpieceViewModel workpiece = _logic.GetWorkpiece(workpieceId)!; + if(planProduction.PlanProductionWorkpieces.ContainsKey(workpieceId)) + planProduction.PlanProductionWorkpieces[workpieceId] = (workpiece, planProduction.PlanProductionWorkpieces[workpieceId].Item2 + count); + else + planProduction.PlanProductionWorkpieces.Add(workpieceId, (workpiece, count)); + _logic.UpdatePlanProduction( + new PlanProductionBindingModel + { + Id = planId, + ClientId = Client.user!.Id, + Count = planProduction.Count, + Deadline = planProduction.Deadline, + PlanProductionWorkpieces = planProduction.PlanProductionWorkpieces, + ProductionName = planProduction.ProductionName, + } + ); - } + Response.Redirect("Index"); - [HttpGet] + + } + + [HttpGet] public IActionResult Reports() { return View(); @@ -304,7 +318,6 @@ namespace FactoryWorkerApp.Controllers return View(new ExecutionPhaseViewModel()); } [HttpPost] - // доделать public IActionResult ExecutionPhase(int id, string name, ExecutionPhaseStatus status, string impFIO, int planId) { ExecutionPhaseBindingModel model = new ExecutionPhaseBindingModel(); diff --git a/Factory/FactoryWorkerApp/Views/Home/ConnectionPlanProductionWorkpiece.cshtml b/Factory/FactoryWorkerApp/Views/Home/ConnectionPlanProductionWorkpiece.cshtml index 073977a..2ba8a50 100644 --- a/Factory/FactoryWorkerApp/Views/Home/ConnectionPlanProductionWorkpiece.cshtml +++ b/Factory/FactoryWorkerApp/Views/Home/ConnectionPlanProductionWorkpiece.cshtml @@ -1,7 +1,6 @@ -@using FactoryDataModels.Enums - -@{ +@{ ViewData["Title"] = "Connect plan and workpieces"; + }