Exemple #1
0
        public async Task <Result <DownloadTask> > Handle(GetDownloadTaskByIdQuery request, CancellationToken cancellationToken)
        {
            var query = _dbContext.DownloadTasks.AsQueryable();

            if (request.IncludeServer)
            {
                query = query.Include(x => x.PlexServer);
            }

            if (request.IncludePlexLibrary)
            {
                query = query.Include(x => x.PlexLibrary);
            }

            var downloadTask = await query
                               .Include(x => x.DownloadWorkerTasks)
                               .Include(x => x.DownloadFolder)
                               .Include(x => x.DestinationFolder)
                               .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);

            if (downloadTask == null)
            {
                return(ResultExtensions.GetEntityNotFound(nameof(DownloadTask), request.Id));
            }

            return(Result.Ok(downloadTask));
        }
Exemple #2
0
        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));
        }
        public async Task <Result <PlexTvShow> > Handle(GetPlexTvShowByIdWithEpisodesQuery request, CancellationToken cancellationToken)
        {
            IQueryable <PlexTvShow> query = _dbContext.PlexTvShows.Include(x => x.Seasons)
                                            .ThenInclude(x => x.Episodes)
                                            .ThenInclude(x => x.EpisodeData)
                                            .ThenInclude(x => x.Parts);

            if (request.IncludeLibrary)
            {
                query = query
                        .Include(x => x.PlexLibrary)
                        .ThenInclude(x => x.PlexServer);
            }

            var plexTvShow = await query
                             .OrderBy(x => x.Key)
                             .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);

            if (plexTvShow == null)
            {
                return(ResultExtensions.GetEntityNotFound(nameof(PlexTvShow), request.Id));
            }

            plexTvShow.Seasons = plexTvShow.Seasons.OrderBy(x => x.Title).ToList();

            return(Result.Ok(plexTvShow));
        }
Exemple #4
0
        public async Task <Result <FolderPath> > Handle(GetFolderPathByIdQuery request, CancellationToken cancellationToken)
        {
            var folderPath = await _dbContext.FolderPaths.FindAsync(request.Id);

            if (folderPath == null)
            {
                return(ResultExtensions.GetEntityNotFound(nameof(FolderPath), request.Id));
            }

            return(Result.Ok(folderPath));
        }
Exemple #5
0
        public async Task <Result <bool> > Handle(HideNotificationCommand command, CancellationToken cancellationToken)
        {
            var notification = _dbContext.Notifications.AsTracking().FirstOrDefault(x => x.Id == command.Id);

            if (notification == null)
            {
                return(ResultExtensions.GetEntityNotFound(nameof(Notification), command.Id));
            }

            notification.Hidden = true;
            await SaveChangesAsync();

            return(Result.Ok(true));
        }
        public async Task <Result <FolderPath> > Handle(UpdateFolderPathCommand command, CancellationToken cancellationToken)
        {
            var folderPath = await _dbContext.FolderPaths.AsTracking().FirstOrDefaultAsync(x => x.Id == command.FolderPath.Id, cancellationToken);

            if (folderPath == null)
            {
                return(ResultExtensions.GetEntityNotFound(nameof(FolderPath), command.FolderPath.Id));
            }

            _dbContext.Entry(folderPath).CurrentValues.SetValues(command.FolderPath);
            await _dbContext.SaveChangesAsync(cancellationToken);

            return(Result.Ok(folderPath));
        }
Exemple #7
0
        public async Task <Result <PlexAccount> > Handle(GetPlexAccountByIdQuery 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.Id == request.Id, cancellationToken);

            if (plexAccount == null)
            {
                return(ResultExtensions.GetEntityNotFound(nameof(PlexAccount), request.Id));
            }

            if (request.IncludePlexServers && request.IncludePlexLibraries)
            {
                // 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();
                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 async Task <Result <bool> > Handle(UpdateDownloadTaskByIdCommand command, CancellationToken cancellationToken)
        {
            var downloadTask = await _dbContext.DownloadTasks.AsTracking()
                               .FirstOrDefaultAsync(x => x.Id == command.DownloadTask.Id, cancellationToken);

            if (downloadTask == null)
            {
                return(ResultExtensions.GetEntityNotFound(nameof(downloadTask), command.DownloadTask.Id));
            }

            _dbContext.Entry(downloadTask).CurrentValues.SetValues(command.DownloadTask);
            await _dbContext.SaveChangesAsync(cancellationToken);

            return(Result.Ok(true));
        }
Exemple #9
0
        public async Task <Result <bool> > Handle(DeletePlexAccountCommand command, CancellationToken cancellationToken)
        {
            var plexAccount = await _dbContext.PlexAccounts.AsTracking().FirstOrDefaultAsync(x => x.Id == command.Id, cancellationToken);

            if (plexAccount == null)
            {
                return(ResultExtensions.GetEntityNotFound(nameof(PlexAccount), command.Id));
            }

            _dbContext.PlexAccounts.Remove(plexAccount);
            await _dbContext.SaveChangesAsync(cancellationToken);

            Log.Debug($"Deleted PlexAccount with Id: {command.Id} from the database");

            return(Result.Ok(true));
        }
Exemple #10
0
        public async Task <Result <DownloadFileTask> > Handle(GetFileTaskByIdQuery request, CancellationToken cancellationToken)
        {
            var fileTask = await _dbContext.FileTasks
                           .Include(x => x.DownloadTask)
                           .ThenInclude(x => x.DownloadWorkerTasks)
                           .Include(x => x.DownloadTask)
                           .ThenInclude(x => x.DownloadFolder)
                           .Include(x => x.DownloadTask)
                           .ThenInclude(x => x.DestinationFolder)
                           .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);

            if (fileTask == null)
            {
                return(ResultExtensions.GetEntityNotFound(nameof(DownloadFileTask), request.Id));
            }

            return(Result.Ok(fileTask));
        }
Exemple #11
0
        public async Task <Result <PlexTvShowEpisode> > Handle(GetPlexTvShowEpisodeByIdQuery request, CancellationToken cancellationToken)
        {
            var plexTvShowEpisode = await _dbContext.PlexTvShowEpisodes
                                    .Include(x => x.TvShowSeason)
                                    .ThenInclude(x => x.TvShow)
                                    .Include(x => x.PlexLibrary)
                                    .ThenInclude(x => x.PlexServer)
                                    .Include(x => x.EpisodeData)
                                    .ThenInclude(x => x.Parts)
                                    .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);

            if (plexTvShowEpisode == null)
            {
                return(ResultExtensions.GetEntityNotFound(nameof(PlexTvShowEpisode), request.Id));
            }

            return(Result.Ok(plexTvShowEpisode));
        }
        public async Task <Result <bool> > Handle(UpdatePlexAccountCommand command, CancellationToken cancellationToken)
        {
            var plexAccount = command.PlexAccount;
            var accountInDb = await _dbContext.PlexAccounts
                              .Include(x => x.PlexAccountServers)
                              .ThenInclude(x => x.PlexServer)
                              .AsTracking().FirstOrDefaultAsync(x => x.Id == plexAccount.Id, cancellationToken);

            if (accountInDb == null)
            {
                return(ResultExtensions.GetEntityNotFound(nameof(PlexAccount), plexAccount.Id));
            }

            _dbContext.Entry(accountInDb).CurrentValues.SetValues(plexAccount);
            await _dbContext.SaveChangesAsync(cancellationToken);

            return(Result.Ok(true));
        }
Exemple #13
0
        public async Task <Result <PlexServerStatus> > Handle(GetPlexServerStatusByIdQuery request, CancellationToken cancellationToken)
        {
            var query = _dbContext.PlexServerStatuses.AsQueryable();

            if (request.IncludePlexServer)
            {
                query = query
                        .Include(x => x.PlexServer);
            }

            var status = await query.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);

            if (status == null)
            {
                return(ResultExtensions.GetEntityNotFound(nameof(PlexServerStatus), request.Id));
            }

            return(Result.Ok(status));
        }
Exemple #14
0
        public async Task <Result <bool> > Handle(DeleteMediaFromPlexLibraryCommand command, CancellationToken cancellationToken)
        {
            try
            {
                // First retrieve only the plexLibrary to determine the media type.
                var entity = await _dbContext.PlexLibraries.AsNoTracking().FirstOrDefaultAsync(x => x.Id == command.PlexLibraryId, cancellationToken);

                if (entity == null)
                {
                    return(ResultExtensions.GetEntityNotFound(nameof(PlexLibrary), command.PlexLibraryId));
                }

                // Then construct the database query,
                // this improves performance such as not to check the tables which will return no result anyway.
                var plexLibraryQuery = GetPlexLibraryQueryableByType(entity.Type, false, true);

                // We only want to delete the media and preserve the PlexLibrary entry in the Db.
                var plexLibrary = await plexLibraryQuery.AsTracking().FirstOrDefaultAsync(x => x.Id == command.PlexLibraryId, cancellationToken);

                switch (plexLibrary.Type)
                {
                case PlexMediaType.Movie:
                    _dbContext.PlexMovies.RemoveRange(plexLibrary.Movies);
                    break;

                case PlexMediaType.TvShow:
                    _dbContext.PlexTvShows.RemoveRange(plexLibrary.TvShows);
                    break;

                default:
                    return(Result.Fail($"PlexLibrary with Id {plexLibrary.Id} and MediaType {plexLibrary.Type} is currently not supported"));
                }

                await _dbContext.SaveChangesAsync(cancellationToken);

                return(Result.Ok(true));
            }
            catch (Exception e)
            {
                Log.Error(e.Message);
                return(Result.Fail(new ExceptionalError(e)));
            }
        }
        public async Task <Result <PlexTvShowSeason> > Handle(GetPlexTvShowSeasonByIdWithEpisodesQuery request, CancellationToken cancellationToken)
        {
            var plexTvShowSeason = await _dbContext.PlexTvShowSeason
                                   .Include(x => x.TvShow)
                                   .Include(x => x.PlexLibrary)
                                   .ThenInclude(x => x.PlexServer)
                                   .Include(x => x.Episodes)
                                   .ThenInclude(x => x.EpisodeData)
                                   .ThenInclude(x => x.Parts)
                                   .OrderBy(x => x.Key)
                                   .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);

            if (plexTvShowSeason == null)
            {
                return(ResultExtensions.GetEntityNotFound(nameof(PlexTvShowSeason), request.Id));
            }

            plexTvShowSeason.Episodes = plexTvShowSeason.Episodes.OrderBy(x => x.Key).ToList();

            return(Result.Ok(plexTvShowSeason));
        }
        public async Task <Result <PlexLibrary> > Handle(GetPlexLibraryByIdQuery request, CancellationToken cancellationToken)
        {
            var query = PlexLibraryQueryable;

            var result = await query.FirstOrDefaultAsync(x => x.Id == request.Id);

            if (result == null)
            {
                return(ResultExtensions.GetEntityNotFound(nameof(PlexLibrary), request.Id));
            }

            var plexLibrary = await GetPlexLibraryQueryableByType(result.Type, request.IncludePlexServer, request.IncludeMedia, request.TopLevelMediaOnly)
                              .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);

            if (plexLibrary == null)
            {
                return(ResultExtensions.GetEntityNotFound(nameof(PlexLibrary), request.Id));
            }

            return(Result.Ok(plexLibrary.SortMedia()));
        }
Exemple #17
0
        public async Task <Result <PlexMovie> > Handle(GetPlexMovieByIdQuery request, CancellationToken cancellationToken)
        {
            var query = PlexMoviesQueryable;

            if (request.IncludePlexLibrary && !request.IncludePlexServer)
            {
                query = query.IncludePlexLibrary();
            }

            if (request.IncludePlexLibrary && request.IncludePlexServer)
            {
                query = query.IncludeServer();
            }

            var plexMovie = await query.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);

            if (plexMovie == null)
            {
                return(ResultExtensions.GetEntityNotFound(nameof(PlexMovie), request.Id));
            }

            return(Result.Ok(plexMovie));
        }