Ejemplo n.º 1
0
        public async Task GetByIdShouldSucceed()
        {
#if DEBUG
            // Arrange
            var id = Guid.NewGuid();
            RepositoryHelper.CreateUser(id, "login", "password", "FirstName", "LastName");

            // Act
            var result = await _finder.Get(id);

            // Assert
            result.Should().NotBeNull();
#endif
        }
Ejemplo n.º 2
0
        public async Task <Result> Handle(GetQuery query, CancellationToken cancellationToken)
        {
            Result result;

            try
            {
                var user = await _userFinder.Get(query.Id);

                var userModel = _mapper.Map <UserModel>(user);

                result = Result.Ok(userModel, user.Version);
            }
            catch (EntityNotFoundDbException)
            {
                result = Result.Fail(new System.Collections.Generic.List <Failure>()
                {
                    new HandlerFault()
                    {
                        Code    = HandlerFaultCode.NotFound.Name,
                        Message = HandlerFailures.NotFound,
                        Target  = "id"
                    }
                }
                                     );
            }
            catch
            {
                result = Result.Fail(CustomFailures.GetUserFailure);
            }

            return(result);
        }
Ejemplo n.º 3
0
        public async Task <Result> Handle(LoginQuery request, CancellationToken cancellationToken)
        {
            Result result;

            var passwordHash = _passwordProvider.Hash(request.Password);

            try
            {
                var user = await _userFinder.Get(request.Login);

                var passwordMatch = _passwordProvider.Verify(request.Password, user.Password);
                if (passwordMatch)
                {
                    result = Result.Ok(new AuthenticatedUserModel {
                        Id = user.Id
                    });
                }
                else
                {
                    throw new EntityNotFoundDbException();
                }
            }
            catch (EntityNotFoundDbException)
            {
                result = Result.Fail(new System.Collections.Generic.List <Failure>()
                {
                    new HandlerFault()
                    {
                        Code    = HandlerFaultCode.InvalidCredentials.Name,
                        Message = HandlerFailures.InvalidCredentials,
                        Target  = "credentials"
                    }
                }
                                     );
            }
            catch
            {
                result = Result.Fail(CustomFailures.LoginUserFailure);
            }

            return(result);
        }
Ejemplo n.º 4
0
        public async Task <Result> Handle(UpdateCommand command, CancellationToken cancellationToken)
        {
            Result result;

            try
            {
                var user = await _userFinder.Get(command.Id);

                if (user.Version != command.Version)
                {
                    throw new CommandVersionException();
                }
                if (command.Login.HasValue)
                {
                    var updatedLogin = command.Login.Value;
                    user.ChangeLogin(new Login(updatedLogin));
                }
                if (command.Password.HasValue)
                {
                    var passwordHashed = _passwordProvider.Hash(command.Password.Value);
                    user.ChangePassword(passwordHashed);
                }
                if (command.FirstName.HasValue)
                {
                    user.FullName.ChangeFirstName(command.FirstName.Value);
                }
                if (command.LastName.HasValue)
                {
                    user.FullName.ChangeLastName(command.LastName.Value);
                }
                user.Version = _versionProvider.Generate();
                await _userRepository.Update(user);

                result = Result.Ok(user.Version);
                await _publishIntegrationEventsService.PublishUserUpdated(user.Id, user.Login.Value, user.FullName.FirstName.Value, user.FullName.LastName.Value);
            }
            catch (EntityNotFoundDbException)
            {
                result = Result.Fail(new System.Collections.Generic.List <Failure>()
                {
                    new HandlerFault()
                    {
                        Code    = HandlerFaultCode.NotFound.Name,
                        Message = HandlerFailures.NotFound,
                        Target  = "id"
                    }
                }
                                     );
            }
            catch (CommandVersionException)
            {
                result = Result.Fail(new System.Collections.Generic.List <Failure>()
                {
                    new HandlerFault()
                    {
                        Code    = HandlerFaultCode.NotMet.Name,
                        Message = HandlerFailures.NotMet,
                        Target  = "version"
                    }
                }
                                     );
            }
            catch (UniqueKeyException)
            {
                result = Result.Fail(new System.Collections.Generic.List <Failure>()
                {
                    new HandlerFault()
                    {
                        Code    = HandlerFaultCode.Conflict.Name,
                        Message = HandlerFailures.Conflict,
                        Target  = "login"
                    }
                }
                                     );
            }
            catch
            {
                result = Result.Fail(CustomFailures.UpdateUserFailure);
            }

            return(result);
        }