public async Task <ActionResult <AccountFullResponseDTO> > PostAccount(AccountPostDTO accountPostDTO)
        {
            AccountPostDtoValidator validator = new AccountPostDtoValidator();
            ValidationResult        results   = validator.Validate(accountPostDTO);

            if (!results.IsValid)
            {
                return(BadRequest(results.ToString()));
            }

            Account accountResponse;

            try
            {
                accountResponse = await _accountService.AddAsync(mapper.Map <Account>(accountPostDTO), accountPostDTO.Password, accountPostDTO.RoleId.ToString());
            }
            catch (AutoMapperMappingException ex)
            {
                return(BadRequest(ex.ToString()));
            }
            catch (BadRequestException ex)
            {
                return(BadRequest(ex.ErrorMessage));
            }
            catch (ConflictException ex)
            {
                return(Conflict(ex.ErrorMessage));
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }

            return(CreatedAtAction("GetRole", new { id = accountResponse.Id }, mapper.Map <AccountFullResponseDTO>(accountResponse)));
        }
예제 #2
0
        public async Task <ActionResult <Account> > PostAccount(AccountPostDTO accountPost)
        {
            var account = _mapper.Map <Account>(accountPost);

            try
            {
                var user = _context.Users.Find(account.UserId);
                if (user == null)
                {
                    return(BadRequest("The user is not found."));
                }
                else if (user.MonthlySalary - user.MonthlyExpenses < 1000)
                {
                    return(BadRequest("The user's monthly salary - expense is less than $1000. Cannot create account for the user."));
                }
                else
                {
                    _context.Accounts.Add(account);
                    await _context.SaveChangesAsync();
                }
            }
            catch (DbUpdateException)
            {
                return(BadRequest("One user can only have one account."));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }

            return(CreatedAtAction("GetAccount", new { id = account.Id }, account));
        }