public async Task <ActionResult <Commodity> > CreateCommodity(Commodity commodity) { try { // - Validate : CommodityGroup(parent) must be in order. if (commodity.CommodityGroupId < 1) { throw new Exception("The CommodityGroup(parent) of the Commodity to be created must be specified..."); } bool commodityGroupExists = await mc_CommodityRepository.CommodityGroupExists(commodity.CommodityGroupId); if (!commodityGroupExists) { throw new Exception("The CommodityGroup(parent) of the Commodity to be created must exist on the system..."); } // - Put it away! var commodityCreated = await mc_CommodityRepository.CreateCommodity(commodity); return(Ok(commodityCreated)); } catch (Exception exception) { string message = "Error occurred in CommodityController.CreateCommodity" + Environment.NewLine; message += exception.Message; return(StatusCode(StatusCodes.Status500InternalServerError, message)); } }
public IActionResult Save(Commodity commodity) { Commodity commoditySaved; if (ModelState.IsValid) { if (commodity.CommodityId < 1) { var commodityCreatedResult = mc_CommodityRepository.CreateCommodity(commodity); commoditySaved = commodityCreatedResult.Result as Commodity; } else { var commodityUpdatedResult = mc_CommodityRepository.UpdateCommodity(commodity); commoditySaved = commodityUpdatedResult.Result as Commodity; } return(View("Details", commoditySaved)); } // - Houston we have a problem... // - Major Tom to ground control: lets go back home. var commoditySaveViewModel = new CommoditySaveViewModel(); var commodityGroupsResult = mc_CommodityRepository.GetCommodityGroups(""); commoditySaveViewModel.CommodityGroups = commodityGroupsResult.Result as List <CommodityGroup>; return(View("Save", commoditySaveViewModel)); }