public IActionResult EditShelf(string id, Shelf shelf) { if (id != shelf.ShelfId) { return(NotFound()); } var standShelfSize = ctx.GetStandardShellSize(1); if (ModelState.IsValid) { try { //lấy tổng số row của table cell, floor của id int currentCellNumber = ctx.CellCount(id); int currentFloorNumber = ctx.FloorCount(id); //thay đổi floor number tăng so với floor number hiện tại if (currentFloorNumber < shelf.FloorNumber) { ctx.UpdateShelf(shelf); for (int i = 1; i <= shelf.FloorNumber; i++) { string myFloorId = $"{shelf.ShelfId}-{i}"; if (!ctx.FloorExists(myFloorId)) { Floor floor = new Floor { FloorId = $"{shelf.ShelfId}-{i}", ShelfId = shelf.ShelfId }; floor.Status = true; ctx.AddFloor(floor); } } ; } //thay đổi cell number tăng hoặc floor number tăng với cell number, floor number hiện tại if (currentCellNumber < (shelf.CellNumber * shelf.FloorNumber) || (currentFloorNumber != shelf.FloorNumber && currentFloorNumber < shelf.FloorNumber)) { ctx.UpdateShelf(shelf); for (int i = 1; i <= shelf.FloorNumber; i++) { for (int j = 1; j <= shelf.CellNumber; j++) { string myCellId = $"{shelf.ShelfId}-{i}-{j}"; if (!ctx.CellExists(myCellId)) { Cell cell = new Cell { CellId = $"{shelf.ShelfId}-{i}-{j}", FloorId = $"{shelf.ShelfId}-{i}" }; cell.Status = true; ctx.AddCell(cell); } } } ; } //if (currentCellNumber > (shelf.CellNumber * shelf.FloorNumber) || // currentFloorNumber > shelf.FloorNumber) //{ // int oldCellNumber = currentCellNumber / currentFloorNumber; // for (int i = 1; i <= currentFloorNumber; i++) // { // for (int j = 1; j <= oldCellNumber; j++) // { // var cellId = $"{shelf.ShelfId}-{i}-{j}"; // var cell = await ctx.Cells.FindAsync(cellId); // ctx.Cells.Remove(cell); // await ctx.SaveChangesAsync(); // } // }; // for (int i = 1; i <= currentFloorNumber; i++) // { // var floorId = $"{shelf.ShelfId}-{i}"; // var floor = await ctx.Floors.FindAsync(floorId); // ctx.Floors.Remove(floor); // await ctx.SaveChangesAsync(); // }; // ctx.Update(shelf); // await ctx.SaveChangesAsync(); // for (int i = 1; i <= shelf.FloorNumber; i++) // { // Floor floor = new Floor // { // FloorId = $"{shelf.ShelfId}-{i}", // ShelfId = shelf.ShelfId // }; // ctx.Add(floor); // await ctx.SaveChangesAsync(); // } // for (int i = 1; i <= shelf.FloorNumber; i++) // { // for (int j = 1; j <= shelf.CellNumber; j++) // { // Cell cell = new Cell // { // CellId = $"{shelf.ShelfId}-{i}-{j}", // FloorId = $"{shelf.ShelfId}-{i}" // }; // ctx.Add(cell); // await ctx.SaveChangesAsync(); // } // }; //} if (standShelfSize.StandardCell < (currentCellNumber / currentFloorNumber) || standShelfSize.StandardFloor < currentFloorNumber) { ViewBag.UpdateShelfSize = "Cant decrease shelf size, please remove all packages !!!"; ViewBag.StandardSize = new StandardSize { StandardFloor = standShelfSize.StandardFloor, StandardCell = standShelfSize.StandardCell }; return(View(shelf)); } if (standShelfSize.StandardCell == shelf.CellNumber && standShelfSize.StandardFloor == shelf.FloorNumber) { shelf.Status = true; ctx.UpdateShelf(shelf); } } catch (DbUpdateConcurrencyException) { if (!ShelfExists(shelf.ShelfId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(ListAllShelf))); } ViewBag.StandardSize = new StandardSize { StandardFloor = standShelfSize.StandardFloor, StandardCell = standShelfSize.StandardCell }; return(View(shelf)); }