public static CheckModelState EnterStandardIntoDatabase(StockStandardCreateViewModel model, InventoryItem inventoryItem, int numOfItems, Department department, string user, UnitOfWork _uow) { CheckModelState result = CheckModelState.Invalid;//default to invalid to expect the worst var repo = _uow.StockStandardRepository; StockStandard createStandard = new StockStandard() { LotNumber = model.LotNumber, StockStandardName = model.StockStandardName, Purity = model.Purity, SolventUsed = model.SolventUsed, Concentration = model.Concentration.ToString() + " " + model.InitialConcentrationUnits, DateReceived = model.DateReceived, DateCreated = DateTime.Today, DateOpened = null, DaysUntilExpired = model.DaysUntilExpired, ExpiryDate = model.ExpiryDate, CreatedBy = user, DateModified = null, CatalogueCode = model.CatalogueCode.ToUpper() }; if (model.NumberOfBottles > 1) { //for (int i = 1; i <= model.NumberOfBottles; i++) { createStandard.IdCode = department.Location.LocationCode + "-" + (numOfItems + 1) + "-" + model.LotNumber + "/" + model.NumberOfBottles; //append number of bottles createStandard.InventoryItems.Add(inventoryItem); repo.Create(createStandard); result = _uow.Commit(); //creation wasn't successful - break from loop and let switch statement handle the problem //if (result != CheckModelState.Valid) { break; } //} } else { createStandard.IdCode = department.Location.LocationCode + "-" + (numOfItems + 1) + "-" + model.LotNumber + "/" + model.NumberOfBottles;//only 1 bottle, no need to concatenate createStandard.InventoryItems.Add(inventoryItem); repo.Create(createStandard); result = _uow.Commit(); } return(result); }
public void CheckModelState_onEnd_is_Ok() { CheckModelState checkModelState = new CheckModelState(); string viewName = "invalidViewName"; // Arrange checkModelState.GoToIfInvalidOnEnd = viewName; var FakeActionExecutedContext = FilterActionFake.FakeActionExecutedContext(Container.createHomeController()); // Act checkModelState.OnActionExecuted(FakeActionExecutedContext); // Assert var result = FakeActionExecutedContext.Result as ViewResultBase; Assert.IsTrue(result == null, "It does not return the right view."); }
public void CheckModelState_return_invalidView_onEnd() { CheckModelState checkModelState = new CheckModelState(); string viewName = "invalidViewName"; string testErrorMessage = "testError"; // Arrange checkModelState.GoToIfInvalidOnEnd = viewName; var actionExecuted = FilterActionFake.FakeActionExecutedContext(Container.createHomeController()); // Act actionExecuted.Controller.ViewData.ModelState.AddModelError(testErrorMessage, testErrorMessage); checkModelState.OnActionExecuted(actionExecuted); // Assert var result = actionExecuted.Result as ViewResultBase; Assert.AreEqual("invalidViewName", result.ViewName, "It does not return the right view."); Assert.IsTrue(result.ViewData.ModelState[testErrorMessage].Errors.Count > 0, "The error is not build."); }
public void CheckModelState_return_json_onEnd() { CheckModelState checkModelState = new CheckModelState(); string viewName = "invalidViewName"; string testErrorMessage = "testError"; // Arrange checkModelState.GoToIfInvalidOnStart = viewName; var FakeActionExecutingContext = FilterActionFake.FakeActionExecutingContext(Container.createHomeController(), true); // Act FakeActionExecutingContext.Controller.ViewData.ModelState.AddModelError(testErrorMessage, testErrorMessage); checkModelState.OnActionExecuting(FakeActionExecutingContext); // Assert var result = FakeActionExecutingContext.Result as HttpStatusCodeResult; Assert.IsTrue(result != null && result.StatusCode == (int)HttpStatusCode.BadRequest, "It does not return a bad request."); Assert.IsTrue(FakeActionExecutingContext.Controller.ViewData.ModelState[testErrorMessage].Errors.Count > 0, "The error is not build."); }
public ActionResult Create([Bind(Include = "StockStandardName,SupplierName,CatalogueCode,StorageRequirements,MSDSNotes,LotNumber,MSDSNotes,UsedFor,SolventUsed,Purity,ExpiryDate,NumberOfBottles,InitialAmount,Concentration,DateReceived,IsExpiryDateBasedOnDays,DaysUntilExpired,OtherUnitExplained,ConcentrationOtherUnitExplained")] StockStandardCreateViewModel model, string[] AmountUnit, string[] ConcentrationUnit, HttpPostedFileBase uploadCofA, HttpPostedFileBase uploadMSDS, string submit) { //model isn't valid, return to the form if (!ModelState.IsValid) { var errors = ModelState.Values.SelectMany(v => v.Errors); return(View(SetStockStandard(model))); } var inventoryRepository = _uow.InventoryItemRepository; //catalogue code must be unique - let's verify bool doesCatalogueCodeExist = inventoryRepository.Get() .Any(item => item.CatalogueCode != null && item.CatalogueCode.Equals(model.CatalogueCode)); if (doesCatalogueCodeExist) { ModelState.AddModelError("", "The Catalogue Code provided is not unique. If the Catalogue Code provided is in fact correct, add the item as a new Lot Number under the existing Catalogue Code."); return(View(SetStockStandard(model))); } var devicesUsed = Request.Form["Devices"]; var user = _uow.GetCurrentUser(); var department = _uow.GetUserDepartment(); var numOfItems = inventoryRepository.Get().Count(); if (devicesUsed == null) { ModelState.AddModelError("", "You must select a device that was used."); return(View(SetStockStandard(model))); } model = BuildReagentOrStandard.BuildStandard(model, devicesUsed, AmountUnit, ConcentrationUnit, uploadCofA, uploadMSDS, _uow); InventoryItem inventoryItem = BuildReagentOrStandard.BuildStandardInventoryItem(model, department); StockStandard createStandard = null; CheckModelState result = BuildReagentOrStandard.EnterStandardIntoDatabase(model, inventoryItem, numOfItems, department, user.UserName, _uow); switch (result) { case CheckModelState.Invalid: ModelState.AddModelError("", "The creation of " + createStandard.StockStandardName + " failed. Please double check all inputs and try again."); return(View(SetStockStandard(model))); case CheckModelState.DataError: ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists please contact your system administrator."); return(View(SetStockStandard(model))); case CheckModelState.Error: ModelState.AddModelError("", "There was an error. Please try again."); return(View(SetStockStandard(model))); case CheckModelState.Valid: if (!string.IsNullOrEmpty(submit) && submit.Equals("Save")) { //save pressed return(RedirectToAction("Index")); } else { //save & new pressed return(RedirectToAction("Create")); } default: ModelState.AddModelError("", "An unknown error occurred."); return(View(SetStockStandard(model))); } }