Beispiel #1
0
        public async Task <SignInOutput> Execute(SignInInput input)
        {
            var encryptedPassword = PasswordEncoder.Encrypt(input.Password);
            var user = await UserRepository.FindUserAsync(input.Login, encryptedPassword);

            return(null != user
                ? new SignInOutput(user, TokenService.CreateToken(user))
                : throw UserNotFound.InvalidCredentials());
        }
Beispiel #2
0
        public async Task <IActionResult> Delete(Guid userId)
        {
            var response = await _mediator.Send(new DeleteUserById(userId));

            return(response switch
            {
                UserNotFound _ => BadRequest($"UserId {userId} cannot be found"),
                _ => NoContent()
            });
Beispiel #3
0
        public async Task <IUpdateUserResult> Handle(UpdateUser request, CancellationToken cancellationToken)
        {
            var existingUser = await _mediator.Send(new GetUserById(request.UserId), cancellationToken);

            return(existingUser switch
            {
                UserNotFound _ => UserNotFound.Instance,
                UserFound userFound when IsNotMatchingETags(userFound.Etag, request.Etag) => new NewerVersionOfUserRecordFound(userFound.Etag),
                UserFound userFound => await UpdateUser(request, userFound, cancellationToken),
                _ => throw new ArgumentOutOfRangeException(nameof(existingUser))
            });
Beispiel #4
0
        public async Task <IActionResult> Patch(Guid userId, [FromHeader(Name = "If-Match")] string etag, [FromBody] JsonPatchDocument <UserDto> patchDoc)
        {
            if (string.IsNullOrWhiteSpace(etag))
            {
                return new ObjectResult(new ValidationProblemDetails {
                    Detail = "User record's ETag value is expected in the If-Match header"
                })
                       {
                           StatusCode = (int)HttpStatusCode.PreconditionRequired
                       }
            }
            ;
            var updateUserRequest = new UpdateUser(userId, etag, GetUpdatedUser);
            var updateUserResult  = await _mediator.Send(updateUserRequest);

            IActionResult response = updateUserResult switch
            {
                FailedToApplyUserModifications userRecord => BadRequest(userRecord.Errors),
                NewerVersionOfUserRecordFound _ => new ObjectResult(PreconditionFailed),
                UserSuccessfullyUpdated userRecord => new ObjectResult(ForUpdatedUser(userRecord.Etag)),
                UserNotFound _ => NotFound(),
                _ => throw new ArgumentOutOfRangeException(nameof(updateUserResult))
            };

            return(response);

            (List <string>, User) GetUpdatedUser(User existingUser)
            {
                // map the internal User model to the API model for the user (UserDto).
                var(errorMessage, existingUserDto) = _mapper.MapTo <UserDto>(existingUser);

                if (!string.IsNullOrWhiteSpace(errorMessage))
                {
                    throw new Exception(errorMessage);
                }
                // this makes it easy to call patchDoc.ApplyTo to create an updated UserDto based on the patchDoc object.
                patchDoc.ApplyTo(existingUserDto, ModelState);
                // if there are errors, exit early.
                if (!ModelState.IsValid)
                {
                    return(ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList(), null);
                }
                // Otherwise map the Dto back to the domain model user, which we will now attempt to replace in Cosmos DB.
                var(_, updatedUser) = _mapper.MapTo <User>(existingUserDto);
                return(new List <string>(), updatedUser);
            }
        }
Beispiel #5
0
        public async Task <IActionResult> GetUser(Guid userId, [FromHeader(Name = "If-None-Match")] string etag)
        {
            var userResult = await _mediator.Send(new GetUserById(userId, etag));

            var response = userResult switch
            {
                UserFound userFound => GetUserFoundResult(userFound),
                UserNotModified _ => new StatusCodeResult((int)HttpStatusCode.NotModified),
                UserNotFound _ => NotFound(),
                _ => throw new ArgumentOutOfRangeException(nameof(userResult))
            };

            return(response);

            IActionResult GetUserFoundResult(UserFound userFound)
            {
                var(errorMessage, userDto) = _mapper.MapTo <UserDto>(userFound.User);
                if (!string.IsNullOrWhiteSpace(errorMessage))
                {
                    throw new Exception(errorMessage);
                }
                return(new ObjectResult(ForExistingUser(userDto, userFound.Etag)));
            }
        }
Beispiel #6
0
 public UserNotFoundException(Guid id, UserNotFound innerException)
     : base("There is no user with the given ID `" + id + "'.", innerException)
 {
 }
Beispiel #7
0
 public UserNotFoundException(string login, UserNotFound innerException)
     : base("There is no user with the given login `" + login + "'.", innerException)
 {
 }