public async Task <UserDetailsModel> Handle(CreateUserAccountCommand request, CancellationToken cancellationToken) { var user = await _dbContext.Users .Include(x => x.Account) .SingleOrDefaultAsync(x => x.Id == request.UserId, cancellationToken); if (user == null) { throw new ArgumentException("Uer does not exist."); } ; if (user.Account != null) { throw new ArgumentException("An account has already been created for this user."); } var account = new Account { Id = Guid.NewGuid(), CreditBalance = 0m, CreditLimit = request.Request.CreditLimit, UserId = request.UserId }; await _dbContext.AddAsync(account, cancellationToken); await _dbContext.SaveChangesAsync(cancellationToken); var mappedUser = _mapper.Map <UserDetailsModel>(user); mappedUser.Account = _mapper.Map <UserAccountModel>(user.Account); return(mappedUser); }
public async Task <UserSummaryModel> Handle(CreateUserCommand request, CancellationToken cancellationToken) { var emailTaken = _dbContext.Users.Any(x => x.Email.Equals(request.Request.Email)); if (emailTaken) { throw new ArgumentException($"{request.Request.Email} has already been registered."); } var entity = new User { Id = request.Id, Email = request.Request.Email, MonthlySalary = request.Request.MonthlySalary, MonthlyExpenses = request.Request.MonthlyExpenses }; await _dbContext.AddAsync(entity, cancellationToken); await _dbContext.SaveChangesAsync(cancellationToken); return(_mapper.Map <UserSummaryModel>(entity)); }