public async Task <ValidationContext <UserLoginCommandResult> > Handle(AddAdminCommand request, CancellationToken cancellationToken)
        {
            var result = new ValidationContext <UserLoginCommandResult>();

            _logger.LogDebug("Attempting to create first Admin account");

            if (await result.AddErrorIf(HasAdminAlreadyBeenCreated, "Unable to add Plex Admin", "An Admin account has already been created"))
            {
                return(result);
            }

            _logger.LogDebug("No existing Admin account, attempting Plex SignIn");
            var plexUser = await _plexApi.SignIn(request.Username, request.Password);

            if (result.AddErrorIf(() => plexUser == null, "Invalid PlexCredentials", "The Login credentials for Plex were invalid"))
            {
                _logger.LogDebug("Invalid PlexCredentials");
                return(result);
            }

            if (!result.IsSuccessful)
            {
                return(result);
            }

            _logger.LogDebug("Plex SignIn Successful");

            var refreshToken = _tokenService.CreateRefreshToken();

            var adminUser = await AddAdminUser(plexUser, refreshToken);

            await CreateAdminServer(plexUser);

            adminUser.LastLoginUtc = DateTime.UtcNow;

            await _unitOfWork.CommitAsync();

            result.Data = new UserLoginCommandResult
            {
                AccessToken  = _tokenService.CreateToken(adminUser),
                RefreshToken = refreshToken.Token
            };

            return(result);
        }
Esempio n. 2
0
        public async Task <ValidationContext <GetOneIssueQueryResult> > Handle(GetOneIssueQuery request, CancellationToken cancellationToken)
        {
            var resultContext = new ValidationContext <GetOneIssueQueryResult>();

            var issue = await _issueService.GetIssueById(request.Id);

            resultContext.AddErrorIf(() => issue == null, "Invalid Id", "No issue found for the given id");

            if (!resultContext.IsSuccessful)
            {
                return(resultContext);
            }

            var issueModel = _mapper.Map <IssueDetailModel>(issue);

            resultContext.Data = new GetOneIssueQueryResult
            {
                Issue = issueModel
            };

            return(resultContext);
        }