public async Task <IActionResult> AssignLicenseAsset(string licenseId) { try { var license = await _licenseInterface.GetLicense(licenseId, Request.Cookies["AssetReference"].ToString()); if (license == null) { return(RedirectToAction("Index", "Error")); } var assets = await _assetInterface.GetAssets(Request.Cookies["AssetReference"].ToString()); if (assets == null) { return(RedirectToAction("Index", "Error")); } //Map the objects results to corresponding DTO's LicenseDTO licenseDTO = _mapper.Map <LicenseDTO>(license); List <AssetsDTO> assetsDTOs = _mapper.Map <List <AssetsDTO> >(assets); //Instantiate LicenseAssetsViewModel var licenseAssetsViewModel = new LicenseAssetsViewModel() { LicenseDTO = licenseDTO, AssetsDTOs = assetsDTOs }; //Set the Date to its initial value var date = DateTime.Now; ViewBag.Date = date.ToString("yyyy-MM-dd"); ViewBag.LicenseId = licenseAssetsViewModel.LicenseDTO.Id; return(View(licenseAssetsViewModel)); } catch (Exception ex) { _logger.LogError($"Error encountered in LicenseController||AssignLicenseAsset ErrorMessage: {ex.Message}"); return(RedirectToAction("Index", "Error")); } }
public async Task <IActionResult> AssignLicenseAsset(LicenseAssetsViewModel licenseAssetsViewModel) { try { if (!ModelState.IsValid) { return(View(licenseAssetsViewModel)); } var userLicense = new UserLicense() { LicenseId = licenseAssetsViewModel.LicenseId, IsActive = "No" }; //Update all existing user of specific license to IsActive = No //Check if the asset is already assigned in target asset var checkUser = await _assetsLicenseInterface.GetAssetsOfLicense(licenseAssetsViewModel.AssetId.ToString(), Request.Cookies["AssetReference"].ToString()); var assetsLicense = new AssetsLicense() { LicenseId = licenseAssetsViewModel.LicenseId, AssetId = licenseAssetsViewModel.AssetId, IssuedOn = licenseAssetsViewModel.IssuedOn, ReturnedOn = licenseAssetsViewModel.ReturnedOn, IsActive = "Yes" }; var result = await _assetsLicenseInterface.CreateAssetsLicense(assetsLicense, Request.Cookies["AssetReference"].ToString()); if (result.ResponseCode != HttpStatusCode.OK.ToString()) { ViewBag.ErrorResponse = result.ResponseMessage; return(View()); } var license = new License() { Id = licenseAssetsViewModel.LicenseId, IsAssigned = "Yes" }; var response = await _licenseInterface.EditLicense(license, Request.Cookies["AssetReference"].ToString()); if (response.ResponseCode != HttpStatusCode.OK.ToString()) { return(RedirectToAction("Index", "Error")); } var assets = await _assetInterface.GetAssets(Request.Cookies["AssetReference"].ToString()); if (assets == null) { return(RedirectToAction("Index", "Error")); } //Map the objects results to corresponding DTO's List <AssetsDTO> assetsDTOs = _mapper.Map <List <AssetsDTO> >(assets); //Instantiate AssetsUserVIewModel var model = new LicenseAssetsViewModel() { AssetsDTOs = assetsDTOs }; ViewBag.LicenseId = assetsLicense.LicenseId; return(View(model)); } catch (Exception ex) { _logger.LogError($"Error encountered in LicenseController||AssignLicenseAsset ErrorMessage: {ex.Message}"); return(RedirectToAction("Index", "Error")); } }