public async Task <ActionResult> Post([FromBody] CreateUserAccountDto createUserAccountDto) { if (!ModelState.IsValid) { return(BadRequest()); } try { var userAccount = await _userAccountService.CreateUserAccountAsync(createUserAccountDto); if (createUserAccountDto.UserAccountExists) { return(BadRequest(string.Format(UserAccountFound, createUserAccountDto.UserId))); } if (createUserAccountDto.UserNotFound) { return(NotFound(string.Format(UserNotFound, createUserAccountDto.UserId))); } if (createUserAccountDto.MinSavingsAmountNotMet) { return(BadRequest(string.Format(MinSavingsAmountNotMetRequestMessage))); } if (!createUserAccountDto.Created) { return(BadRequest(string.Format(UserAccountSaveFailRequestMessage, createUserAccountDto.UserId))); } return(Created(new Uri(Request.GetDisplayUrl() + "/" + userAccount.Id), userAccount)); } catch (Exception ex) { return(BadRequest(string.Format(UserAccountSaveFailRequestMessage, createUserAccountDto.UserId))); } }
public async Task <UserAccountDto> CreateUserAccountAsync(CreateUserAccountDto createUserAccountDto) { try { // check user account is already created if (IsUserAccountAlreadyExists(createUserAccountDto.UserId)) { createUserAccountDto.UserAccountExists = true; return(null); } // Get user by id from User Table User user = GetUserById(createUserAccountDto.UserId); if (user == null) { createUserAccountDto.UserNotFound = true; return(null); } // validate monthly savings should be grater the Min amount if ((user.Salary - user.Expenses) < MinSavingsAmount) { createUserAccountDto.MinSavingsAmountNotMet = true; } // create the user account object var userAccountDto = new UserAccountDto() { Email = user.Email, Name = user.Name, Savings = (user.Salary - user.Expenses), MaxAmountEligibleForLoan = MaxLoanEligibleAmount, UserId = user.Id }; var userAccount = _mapper.Map <UserAccount>(userAccountDto); // create user account await _userAccountRepository.InsertAsync(userAccount); await _userAccountRepository.SaveAsync(); createUserAccountDto.Created = true; userAccountDto.Id = userAccount.Id; return(userAccountDto); } catch (Exception) { // Error handle createUserAccountDto.Created = false; return(null); } }