public JsonResult Create(Stock stock) { bool isSuccess = false; bool isRequiredSetup = false; string password = string.Empty; if (ModelState.IsValid) { //TODO Improve this : decrease number of calls to the DB var company = DB.Companies.Find(stock.CompanyId); var material = DB.GenericMaterials.Find(stock.GenericMaterialId); var seqNumber = GetNextSeqNumber(stock.CompanyId, stock.GenericMaterialId); //Fill stock entity properties stock.SeqNumber = seqNumber; stock.StockStatusId = StockStatus.InStock; stock.ProductCode = GenerateProductCode(company.CompanyCodeName, material.MaterialCode, seqNumber); try { DB.Stocks.Add(stock); DB.SaveChanges(); //Create password entity isRequiredSetup = DB.GenericMaterials .Where(x => x.GenericMaterialId == stock.GenericMaterialId) .Select(x => x.RequiresSetup) .FirstOrDefault(); if (isRequiredSetup) { password = CreateRandomPassword(6); var pass = new ProductPassword { Password = password, StockId = stock.StockId }; DB.ProductPasswords.Add(pass); DB.SaveChanges(); } isSuccess = true; //Create history ? //TODO return details view of the newly created product in the stock } catch (Exception e) { ErrorSignal.FromCurrentContext().Raise(e); } } return Json(new { success = isSuccess, isRequiredPassword = isRequiredSetup, productCode = stock.ProductCode, password = password }, JsonRequestBehavior.AllowGet); }
private string GetPassword(Stock productInStock) { // TODO when pass crypted and salted update this method to get the password uncrypted var productPassword = productInStock.ProductPassword.FirstOrDefault(); if (productPassword != null) return productPassword.Password; bool isRequiedSetup = DB.GenericMaterials .Where(x => x.GenericMaterialId == productInStock.GenericMaterialId) .Select(x => x.RequiresSetup) .FirstOrDefault(); if (!isRequiedSetup) { return null; } else { var p = new ProductPassword { StockId = productInStock.StockId, Password = CreateRandomPassword(6), }; DB.ProductPasswords.Add(p); DB.SaveChanges(); return p.Password; } }