public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                //logic goes here
                var assetsLicenses = await _context.AssetsLicenses.Where(x => x.LicenseId == request.LicenseId).AsNoTracking().ToListAsync();

                foreach (var item in assetsLicenses)
                {
                    item.IsActive = "No";
                }

                var assetsLicense = new AssetsLicense()
                {
                    Id         = Guid.NewGuid(),
                    AssetId    = request.AssetId,
                    LicenseId  = request.LicenseId,
                    IssuedOn   = request.IssuedOn,
                    ReturnedOn = request.ReturnedOn,
                    IsActive   = request.IsActive
                };

                _context.AssetsLicenses.Add(assetsLicense);

                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(Unit.Value);
                }
                else
                {
                    throw new Exception("Problem saving changes");
                }
            }
        public async Task <ResponseModel> CreateAssetsLicense(AssetsLicense assetsLicense, string token)
        {
            try
            {
                var responseClient = _httpClientFactory.CreateClient("AssetAPI");

                responseClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

                var result = await responseClient.PostAsJsonAsync <AssetsLicense>("api/AssetsLicense", assetsLicense);

                result.Content.ReadAsStringAsync().ToString();

                if (result.StatusCode != HttpStatusCode.OK)
                {
                    var faliedResponse = await result.Content.ReadAsJsonAsync <RestException>();

                    return(new ResponseModel()
                    {
                        ResponseMessage = faliedResponse.Errors.ToString(),
                        ResponseCode = result.StatusCode.ToString()
                    });
                }

                return(new ResponseModel()
                {
                    ResponseCode = result.StatusCode.ToString()
                });
            }
            catch (Exception ex)
            {
                _logger.LogError($"Error encountered in AssetsLicenseService||CreateAssetsLicense ErrorMessage: {ex.Message}");
                throw ex;
            }
        }
        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"));
            }
        }