public async Task <IActionResult> Get(string userName = null, CancellationToken cancellationToken = default) { return(await _exceptionHandler.HandleExceptionAsync <IActionResult>(async c => { IEnumerable <Loan> loans = null; var nameIdentifierClaim = _user.Claims.SingleOrDefault(x => x.Type == ClaimTypes.NameIdentifier); if (_user.IsInRole(Role.Customer.ToString())) { var customerId = Convert.ToInt32(nameIdentifierClaim.Value); loans = await _loanQueryableDomainService.Get().Where(x => x.CustomerID == customerId).ToListAsync(c).ConfigureAwait(false); } else { if (userName.IsNullOrEmpty()) { return BadRequest("userName in querystring cannot be null or empty for Admin role."); } var customerUrl = _configuration.GetValue <string>("CustomerUrl"); var tokenCacheData = await _tokenManager.GetTokenCacheDataAsync(nameIdentifierClaim.Value, c); if (tokenCacheData.IsNull()) { return Problem("Please try after sometime"); } var jwtToken = tokenCacheData.Token; var customer = await _httpClientFactory.GetDataAsync <Customer, int>(customerUrl + userName.Trim(), jwtToken); loans = await _loanQueryableDomainService.Get().Where(x => x.CustomerID == customer.Id).ToListAsync(c).ConfigureAwait(false); } return Ok(loans); }, cancellationToken)); }
public async Task <ActionResult <string> > Register(User user, CancellationToken cancellationToken = default) { return(await _exceptionHandler.HandleExceptionAsync <ActionResult <string> >(async c => { User userInDB = await _userQueryableDomainService.Get().SingleOrDefaultAsync(x => x.LoginData.UserName.ToLower().Trim() == user.LoginData.UserName.ToLower().Trim(), c).ConfigureAwait(false); if (userInDB.IsNotNull()) { return BadRequest("A user with userName : "******", already exists.Please use a different user name."); } byte[] passwordHash = null; byte[] passwordSalt = null; PasswordUtility.CreatePasswordHash(user.LoginData.Password, out passwordHash, out passwordSalt); user.PasswordHash = passwordHash; user.PasswordSalt = passwordSalt; await _userCommandDomainServiceAsync.InsertAsync(user, c).ConfigureAwait(false); return Ok("User created successfully"); }, cancellationToken)); }