public async Task <Result <PlexServer> > Handle(GetPlexServerByPlexTvShowEpisodeIdQuery request, CancellationToken cancellationToken) { var plexTvShowEpisode = await _dbContext.PlexTvShowEpisodes .Include(x => x.TvShowSeason) .ThenInclude(x => x.TvShow) .ThenInclude(x => x.PlexLibrary) .ThenInclude(x => x.PlexServer) .ThenInclude(x => x.ServerStatus) .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken); if (plexTvShowEpisode == null) { return(ResultExtensions.Create404NotFoundResult()); } var plexServer = plexTvShowEpisode?.TvShowSeason?.TvShow?.PlexLibrary?.PlexServer; if (plexServer == null) { return(ResultExtensions.GetEntityNotFound(nameof(PlexServer), request.Id) .LogError(null, $"Could not retrieve the PlexServer from PlexTvShowEpisode with id: {request.Id}")); } return(Result.Ok(plexServer)); }
/// <summary> /// Check if a <see cref="PlexDownloadClient"/> has already been assigned to this <see cref="DownloadTask"/>. /// </summary> /// <param name="downloadTaskId">The id of the <see cref="DownloadTask"/>.</param> /// <returns>Returns the <see cref="PlexDownloadClient"/> if found and fails otherwise.</returns> private Result <PlexDownloadClient> GetDownloadClient(int downloadTaskId) { var downloadClient = _downloadsList.Find(x => x.DownloadTaskId == downloadTaskId); if (downloadClient == null) { return(ResultExtensions .Create404NotFoundResult($"There is no DownloadClient currently working on a downloadTask with Id {downloadTaskId}")); } return(Result.Ok(downloadClient)); }
public async Task <Result <bool> > Handle(UpdateDownloadStatusOfDownloadTaskCommand command, CancellationToken cancellationToken) { var downloadTask = await _dbContext.DownloadTasks.AsTracking() .FirstOrDefaultAsync(x => x.Id == command.DownloadTaskId, cancellationToken); if (downloadTask != null) { downloadTask.DownloadStatus = command.DownloadStatus; await _dbContext.SaveChangesAsync(cancellationToken); return(Result.Ok(true)); } return(ResultExtensions.Create404NotFoundResult()); }
public async Task <Result <PlexAccount> > Handle(GetPlexAccountByUsernameQuery request, CancellationToken cancellationToken) { var query = _dbContext.PlexAccounts.AsQueryable(); if (request.IncludePlexServers && !request.IncludePlexLibraries) { query = query .Include(x => x.PlexAccountServers) .ThenInclude(x => x.PlexServer); } if (request.IncludePlexServers && request.IncludePlexLibraries) { query = query .Include(v => v.PlexAccountServers) .ThenInclude(x => x.PlexServer) .ThenInclude(x => x.PlexLibraries) .ThenInclude(x => x.PlexAccountLibraries); } var plexAccount = await query .FirstOrDefaultAsync(x => x.Username == request.Username, cancellationToken); if (plexAccount == null) { return(ResultExtensions.Create404NotFoundResult($"Could not find a {nameof(PlexAccount)} with the username: {request.Username}")); } // Remove any PlexLibraries the plexAccount has no access to // TODO This might be improved further since now all PlexLibraries will be retrieved from the database. var plexServers = plexAccount?.PlexAccountServers?.Select(x => x.PlexServer).ToList() ?? new List <PlexServer>(); foreach (var plexServer in plexServers) { // Remove inaccessible PlexLibraries for (int i = plexServer.PlexLibraries.Count - 1; i >= 0; i--) { var x = plexServer?.PlexLibraries[i].PlexAccountLibraries.Select(y => y.PlexAccountId).ToList(); if (!x.Contains(plexAccount.Id)) { plexServer.PlexLibraries.RemoveAt(i); } } } return(Result.Ok(plexAccount)); }
public void ShouldHave404NotFoundError() { // Arrange string testMessage = "TestMessage2"; // Act var result = ResultExtensions.Create404NotFoundResult(testMessage).LogError(); var result2 = Result.Fail("").Add404NotFoundError(testMessage); // Assert result.Has404NotFoundError().Should().BeTrue(); result.Has400BadRequestError().Should().BeFalse(); result.Errors.Count.Should().Be(1); result.Errors[0].Message.Should().Be(testMessage); result2.Has404NotFoundError().Should().BeTrue(); result2.Has400BadRequestError().Should().BeFalse(); result2.Errors.Count.Should().Be(2); result2.Errors[1].Message.Should().Be(testMessage); }
public async Task <Result <DownloadTask> > Handle(GetDownloadTaskByRatingKeyQuery request, CancellationToken cancellationToken) { var query = _dbContext.DownloadTasks.AsQueryable(); if (request.IncludeServer) { query = query.Include(x => x.PlexServer); } var downloadTask = await query .Include(x => x.DownloadWorkerTasks) .Include(x => x.DestinationFolder) .Include(x => x.DownloadFolder) .FirstOrDefaultAsync(x => x.RatingKey == request.RatingKey, cancellationToken); if (downloadTask == null) { return(ResultExtensions.Create404NotFoundResult($"Could not find {nameof(downloadTask)} with ratingKey: {request.RatingKey}")); } return(Result.Ok(downloadTask)); }
public async Task <Result <PlexServer> > Handle(GetPlexServerByPlexLibraryIdQuery request, CancellationToken cancellationToken) { var query = _dbContext.PlexServers .Include(x => x.ServerStatus) .AsQueryable(); if (request.IncludePlexLibraries) { query = query.Include(x => x.PlexLibraries); } var plexServer = await query .Where(x => x.PlexLibraries.Any(y => y.Id == request.Id)) .FirstOrDefaultAsync(cancellationToken); if (plexServer == null) { return(ResultExtensions.Create404NotFoundResult($"Could not find PlexLibrary with Id {request.Id} in any PlexServer")); } return(Result.Ok(plexServer)); }