Beispiel #1
0
        public UpdateUserResponse Handle(UpdateUserRequest request)
        {
            var response = new UpdateUserResponse();

            response.Errors = Validate(request);

            if (response.HasErrors)
            {
                return(response);
            }


            if (!string.IsNullOrEmpty(request.UserToUpdate.Password)) //nes gali ir nepaduoti
            {
                request.UserToUpdate.Password = PasswordsHelper.HashPassword(request.UserToUpdate.Password);
            }

            try
            {
                UpdateUserObject(request.UserOrigin, request.UserToUpdate);

                _usersRepository.Update(request.UserOrigin);

                return(response);
            }
            catch (Exception ex)
            {
                response.Errors.Add(new ErrorStatus(ex.Message));

                //response.Errors.Add(new ErrorStatus("BAD_REQUEST"));

                return(response);
            }
        }
        public LoginUserResponse Handle(LoginUserRequest request)
        {
            var response = new LoginUserResponse();

            response.Errors = Validate(request);


            if (response.HasErrors)
            {
                return(response);
            }
            try
            {
                request.Password = PasswordsHelper.HashPassword(request.Password);

                AddSession(request, response);

                return(response);
            }
            catch (Exception)
            {
                response.Errors.Add(new ErrorStatus("BAD_REQUEST"));
                throw;
            }
        }
        private IList <ErrorStatus> Validate(LoginUserRequest request)
        {
            var errors = new List <ErrorStatus>();


            if (string.IsNullOrEmpty(request.Email) || string.IsNullOrEmpty(request.Password))
            {
                errors.Add(new ErrorStatus("WRONG_INPUT"));
                return(errors);
            }

            var tempolarlyHash = PasswordsHelper.HashPassword(request.Password);

            var user = _usersRepository.ReturnIfExsists(request.Email, tempolarlyHash);

            if (user == null)
            {
                errors.Add(new ErrorStatus("INVALID_USERNAME_OR_PASSWORD"));
            }
            return(errors);
        }
        public CreateUserResponse Handle(CreateUserRequest request)
        {
            var response = new CreateUserResponse();

            response.Errors = Validate(request);

            if (response.HasErrors)
            {
                return(response);
            }
            try
            {
                var currentDate = DateTime.Now;

                request.Password = PasswordsHelper.HashPassword(request.Password);

                int userId = _userRepository.CreateUserAndGetHisId(new User()
                {
                    DateModified   = currentDate,
                    DateRegistered = currentDate,
                    Name           = request.Username,
                    Password       = request.Password,
                    Email          = request.Email,
                    IsActive       = true
                });

                EmailHelper.SendEmail(request);

                response.UserId = userId;

                return(response);
            }
            catch (Exception ex)
            {
                response.Errors.Add(new ErrorStatus("BAD_REQUEST"));
                //response.Errors.Add(new ErrorStatus(ex.Message));

                return(response);
            }
        }