public async Task <IActionResult> PostAsync(int userId, [FromBody] SaveAccountResource resource) { if (!ModelState.IsValid) { return(BadRequest(ModelState.GetErrorMessages())); } var existingUser = await _userService.GetByIdAsync(userId); if (!existingUser.Success) { return(BadRequest(existingUser.Message)); } var account = _mapper.Map <SaveAccountResource, AccountFactory>(resource); account.User = existingUser.Resource; var result = await _accountService.SaveAsync(account); if (!result.Success) { return(BadRequest(result.Message)); } var accountResource = _mapper.Map <Account, AccountResource>(result.Resource); return(Ok(accountResource)); }
public async Task PostAccountAsync_InvalidObjectPassed_ReturnsBadRequest() { // Arrange var nameMissingItem = new SaveAccountResource() { InitialCredit = 23 }; _accountController.ModelState.AddModelError("ClientID", "Required"); // Act var badResponse = await _accountController.PostAccountAsync(nameMissingItem); // Assert Assert.IsInstanceOf(typeof(BadRequestObjectResult), badResponse); }
public async Task <IActionResult> PostAccountAsync([FromBody] SaveAccountResource resource) { if (!ModelState.IsValid) { return(BadRequest(ModelState.GetErrorMessages())); } var result = await _accountService.PostAccountAsync(resource.CustomerID, resource.InitialCredit); if (!result.Success) { return(BadRequest(result.Message)); } return(Ok(result.Account)); }
public async Task <IActionResult> PostAsync([FromBody] SaveAccountResource resource) { if (!ModelState.IsValid) { return(BadRequest(ModelState.GetErrorMessages())); } var account = _mapper.Map <SaveAccountResource, Account>(resource); var result = await _accountService.SaveAsync(account); if (!result.Success) { return(BadRequest(result.Message)); } var accountResource = _mapper.Map <Account, AccountResource>(result.Resource); return(Ok(accountResource)); }
public void Setup() { saveAccountResource = new SaveAccountResource() { CustomerID = 1, InitialCredit = 21 }; accountToTestSave = new AccountViewModel() { ID = 0, Balance = 21 }; _accountServiceMock = new Mock <IAccountService>(); _accountServiceMock.Setup(a => a.PostAccountAsync(1, 21)).ReturnsAsync(new SaveAccountResponse(accountToTestSave)); _accountController = new AccountController(_accountServiceMock.Object); }