Ejemplo n.º 1
0
        public IHttpActionResult Login(LoginRequestModel requestModel)
        {
            if (ModelState.IsValid)
            {
                AccountCommand accountCommand = Mapper.Map <AccountCommand>(requestModel);
                IEnumerable <ValidationResult> validations = commandDispatcher.Validate(accountCommand);

                ModelState.AddModelErrors(validations);

                if (ModelState.IsValid)
                {
                    AccountByUsernameQuery query = new AccountByUsernameQuery(requestModel.Username);
                    Account account = queryDispatcher.Dispatch <AccountByUsernameQuery, Account>(query);

                    AuthCommand    loginCommand   = new AuthCommand(account.Id, requestModel.RememberMe);
                    Authentication authentication = commandDispatcher.Dispatch <AuthCommand, Authentication>(loginCommand);

                    AccountResponseModel responseModel = Mapper
                                                         .Map <AccountResponseModel>(authentication)
                                                         .Map(account);

                    return(Ok(responseModel));
                }
            }

            return(BadRequest(ModelState));
        }
Ejemplo n.º 2
0
        public IEnumerable <ValidationResult> Validate(AccountCommand command)
        {
            AccountByUsernameQuery query = new AccountByUsernameQuery(command.Username);
            Account account = accountHandler.Handle(query);

            if (account == null)
            {
                return(new List <ValidationResult>()
                {
                    new ValidationResult(nameof(command.Username), "Could not find account with such username!")
                });
            }

            PasswordCommand encryptPasswordCommand = new PasswordCommand(command.Password);
            string          encryptedPassword      = encryptPasswordHandler.Handle(encryptPasswordCommand);

            if (encryptedPassword != account.Password)
            {
                return(new List <ValidationResult>()
                {
                    new ValidationResult(nameof(command.Password), "Invalid password!")
                });
            }

            return(Enumerable.Empty <ValidationResult>());
        }
        public IEnumerable <ValidationResult> Validate(UsernameCommand command)
        {
            AccountByUsernameQuery query = new AccountByUsernameQuery(command.Username);
            Account account = accountHandler.Handle(query);

            if (account != null)
            {
                yield return(new ValidationResult(
                                 nameof(command.Username),
                                 string.Format(MessageConstants.ALREADY_REGISTERED, nameof(command.Username).ToLower())));
            }
        }
Ejemplo n.º 4
0
        public string Handle(PlaceBetCommand command)
        {
            Bet bet = Mapper.Map <Bet>(command);

            dbContext.GetCollection <Bet>().InsertOne(bet);

            AccountByUsernameQuery accountQuery = new AccountByUsernameQuery(command.Username);
            Account account = accountByUsernameHandler.Handle(accountQuery);

            account.Balance -= command.Stake;

            UpdateAccountCommand updateAccountCommand = Mapper.Map <UpdateAccountCommand>(account);

            updateAccountHandler.Handle(updateAccountCommand);

            return(bet.Id);
        }
Ejemplo n.º 5
0
        public IEnumerable <ValidationResult> Validate(PlaceBetCommand command)
        {
            AccountByUsernameQuery accountQuery = new AccountByUsernameQuery(command.Username);
            Account account = accountByUsernameHandler.Handle(accountQuery);

            if (account == null)
            {
                return(new List <ValidationResult>()
                {
                    new ValidationResult(nameof(command.Username), "Could not find account with such username!")
                });
            }

            if (account.Balance - command.Stake < 0)
            {
                return(new List <ValidationResult>()
                {
                    new ValidationResult(nameof(command.Stake), "User does not have enough balance!")
                });
            }

            return(Enumerable.Empty <ValidationResult>());
        }