Esempio n. 1
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));
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        public async Task <ActionResult <User> > PostUser(UserPostDTO userPost)
        {
            var user = _mapper.Map <User>(userPost);

            try
            {
                _context.Users.Add(user);
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                return(BadRequest("The email address must be unique."));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }

            return(CreatedAtAction("GetUser", new { id = user.Id }, user));
        }
        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));
        }