public ActionResult Create(RubberRoller rubberRoller, FormCollection collection) { // Check if roller ID exist from DB var dbRoller = _db.rubberRollers.Where(r => r.rollerID == rubberRoller.rollerID).FirstOrDefault(); if (dbRoller != null) { ModelState.AddModelError("rollerID", "There is already an existing roller with the same roller ID."); } if (!ModelState.IsValid || dbRoller != null) { ViewData["rollerCatList"] = getRollerCategories(); return(View("CreateEditForm", rubberRoller)); } rubberRoller.isRefurbished = false; _db.rubberRollers.Add(rubberRoller); // Create initial rubber roller location RollerLocation rollerLocation = new RollerLocation(); rollerLocation.dateTimeIn = DateTime.Now; rollerLocation.rollerID = rubberRoller.id; rollerLocation.RubberRoller = rubberRoller; rollerLocation.location = collection["rollLocat"]; _db.rollerLocations.Add(rollerLocation); int result = _db.SaveChanges(); if (result > 0) { LogAction.log(this._controllerName, "POST", "Added new rubber roller", User.Identity.GetUserId()); TempData["formStatus"] = true; TempData["formStatusMsg"] = "<b>STATUS</b>: New rubber roller has been successfully added!"; } else { LogAction.log(this._controllerName, "POST", "Error adding new rubber roller", User.Identity.GetUserId()); TempData["formStatus"] = false; TempData["formStatusMsg"] = "<b>ALERT</b>: Oops! Something went wrong. The rubber roller has not been successfully added."; } return(Redirect(Request.UrlReferrer.ToString())); }
public static bool UpdateRollerLocation(RubberRoller rubberRoller, string location) { ApplicationDbContext _db = new ApplicationDbContext(); RubberRoller rubber = _db.rubberRollers.FirstOrDefault(r => r.id == rubberRoller.id); if (rubber == null) { return(false); } // Update roller location RollerLocation currentLocation = rubber.RollerLocations.LastOrDefault(); if (currentLocation != null) { currentLocation.dateTimeOut = DateTime.Now; } RollerLocation rollerLocation = new RollerLocation(); rollerLocation.dateTimeIn = DateTime.Now; rollerLocation.rollerID = rubber.id; rollerLocation.RubberRoller = rubber; rollerLocation.location = location; if (location.StartsWith("Operation Line")) { rollerLocation.operationLine = int.Parse(location.Substring(15)); } else { rollerLocation.operationLine = 0; } // Add new records _db.rollerLocations.Add(rollerLocation); var result = _db.SaveChanges(); return(result > 0 ? true : false); }