private async Task <Account> CreateNewAccount(AccountCreationModel accountCreationModel, UserModel user) { var account = new Account { FirstName = accountCreationModel.FirstName, SecondName = accountCreationModel.SecondName, CreationDate = DateTime.UtcNow, UserId = user.Id }; _dbContext.Accounts.Add(account); await _dbContext.SaveChangesAsync(); var result = await _dbContext.Accounts.Include(a => a.User).FirstOrDefaultAsync(x => x.Id == account.Id); return(result); }
public async Task <UserModel> CreateUser(UserModel newUser) { if (newUser.MonthlySalary - newUser.MonthlyExpenses < 1000) { throw new BadRequestException($"Not possibe to create the user." + $" Difference beetween Salary and Expenses should be more than 1000$."); } _logger.LogInformation($"Creating new user {newUser.Name} email: {newUser.Email}"); var user = await _dbContext.Users.FirstOrDefaultAsync(u => u.Email == newUser.Email); if (user != null) { _logger.LogInformation($"Creating new user failed because the email: {newUser.Email} already is used"); throw new BadRequestException($"The user with email {newUser.Email} already exists"); } user = _mapper.Map <User>(newUser); _dbContext.Users.Add(user); await _dbContext.SaveChangesAsync(); return(_mapper.Map <UserModel>(user)); }