public ActionResult Create(Asset asset) { if (ModelState.IsValid) { // Asset Title is Mandatory if (string.IsNullOrEmpty(asset.Title)) { asset.Title = "No Title"; } _assetRepository.Create(asset); _unitOfWork.Commit(); // Update Tag Number var selectedAsset = _assetRepository.Get(asset.Id); if (selectedAsset != null) { selectedAsset.TagNumber = $"LA{asset.Id.ToString("D" + 6)}"; _assetRepository.Update(selectedAsset); _unitOfWork.Commit(); } #if !DEBUG if (selectedAsset != null) { _emailComposerService.AssetStateChanged(selectedAsset.Id); } #endif return(RedirectToAction("Index")); } return(View(asset)); }
public JsonResult Update(AssetModel assetModel) { ApiResult <Asset> apiResult; if (ModelState.IsValid) { if (assetModel.Id > 0) { apiResult = TryExecute(() => { var selectedAsset = _assetRepository.Get(assetModel.Id); if (selectedAsset != null) { var employee = _employeeRepository.GetBy(u => u.UserId == WebUser.Id); // Log new Changes in Allocation Table if (selectedAsset.State != assetModel.State || selectedAsset.AllocatedEmployeeId != assetModel.AllocatedEmployeeId) { var assetAllocation = new AssetAllocation { State = assetModel.State, AllocatedEmployeeId = assetModel.AllocatedEmployeeId, AllocatedOn = DateTime.UtcNow, AllocatedByEmployeeId = employee.Id, AssetId = selectedAsset.Id }; _assetAllocationRepository.Create(assetAllocation); _unitOfWork.Commit(); } // Update main Asset selectedAsset.SerialNumber = assetModel.SerialNumber; selectedAsset.Title = assetModel.Title; selectedAsset.Description = assetModel.Description; selectedAsset.Specifications = assetModel.Specifications; selectedAsset.Brand = assetModel.Brand; selectedAsset.Cost = assetModel.Cost; selectedAsset.TagNumber = assetModel.TagNumber; selectedAsset.ModelNumber = assetModel.ModelNumber; selectedAsset.IsBrandNew = assetModel.IsBrandNew; selectedAsset.State = assetModel.State; selectedAsset.PurchaseDate = assetModel.PurchaseDate; selectedAsset.WarrantyExpiryDate = assetModel.WarrantyExpiryDate; selectedAsset.AssetCategoryId = assetModel.AssetCategoryId; selectedAsset.DepartmentId = assetModel.DepartmentId; selectedAsset.VendorId = assetModel.VendorId; selectedAsset.AllocatedEmployeeId = assetModel.AllocatedEmployeeId; _assetRepository.Update(selectedAsset); _unitOfWork.Commit(); } // Asset State Changed #if !DEBUG _emailComposerService.AssetStateChanged(selectedAsset.Id); #endif return(selectedAsset); }, "Asset updated sucessfully"); } else { apiResult = TryExecute(() => { var asset = new Asset { Title = assetModel.Title, SerialNumber = assetModel.SerialNumber, Description = assetModel.Description, Specifications = assetModel.Specifications, Brand = assetModel.Brand, Cost = assetModel.Cost, ModelNumber = assetModel.ModelNumber, IsBrandNew = assetModel.IsBrandNew, State = assetModel.State, PurchaseDate = assetModel.PurchaseDate, WarrantyExpiryDate = assetModel.WarrantyExpiryDate, AssetCategoryId = assetModel.AssetCategoryId, DepartmentId = assetModel.DepartmentId, VendorId = assetModel.VendorId, AllocatedEmployeeId = assetModel.AllocatedEmployeeId, Id = assetModel.Id }; _assetRepository.Create(asset); _unitOfWork.Commit(); // Update Tag Number var selectedAsset = _assetRepository.Get(asset.Id); if (selectedAsset != null) { selectedAsset.TagNumber = $"LA{asset.Id.ToString("D" + 6)}"; _assetRepository.Update(selectedAsset); _unitOfWork.Commit(); } #if !DEBUG if (selectedAsset != null) { _emailComposerService.AssetStateChanged(selectedAsset.Id); } #endif return(asset); }, "Asset created sucessfully"); } } else { apiResult = ApiResultFromModelErrors <Asset>(); } return(Json(apiResult, JsonRequestBehavior.AllowGet)); }