diff --git a/src/Ombi.Core/Engine/RecentlyAddedEngine.cs b/src/Ombi.Core/Engine/RecentlyAddedEngine.cs new file mode 100644 index 000000000..97c086e5b --- /dev/null +++ b/src/Ombi.Core/Engine/RecentlyAddedEngine.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Internal; +using Ombi.Core.Models; +using Ombi.Store.Entities; +using Ombi.Store.Repository; + +namespace Ombi.Core.Engine +{ + public class RecentlyAddedEngine + { + public RecentlyAddedEngine(IPlexContentRepository plex, IEmbyContentRepository emby) + { + _plex = plex; + _emby = emby; + } + + private readonly IPlexContentRepository _plex; + private readonly IEmbyContentRepository _emby; + + public IEnumerable GetRecentlyAddedMovies(TimeSpan from) + { + var model = new HashSet(); + var plexMovies = _plex.GetAll().Where(x => x.Type == PlexMediaTypeEntity.Movie && x.AddedAt.Subtract(from) <= x.AddedAt); + var embyMovies = _emby.GetAll().Where(x => x.Type == EmbyMediaType.Movie && x.AddedAt.Subtract(from) <= x.AddedAt); + + TransformPlexMovies(plexMovies, model); + TransformEmbyMovies(embyMovies, model); + + return model; + } + + public IEnumerable GetRecentlyAddedMovies() + { + var model = new HashSet(); + var plexMovies = _plex.GetAll().Where(x => x.Type == PlexMediaTypeEntity.Movie); + var embyMovies = _emby.GetAll().Where(x => x.Type == EmbyMediaType.Movie); + + TransformPlexMovies(plexMovies, model); + TransformEmbyMovies(embyMovies, model); + + return model; + } + + private static void TransformEmbyMovies(IQueryable embyMovies, HashSet model) + { + foreach (var emby in embyMovies) + { + model.Add(new RecentlyAddedMovieModel + { + Id = emby.Id, + ImdbId = emby.ProviderId, + AddedAt = emby.AddedAt, + Title = emby.Title, + }); + } + } + + private static void TransformPlexMovies(IQueryable plexMovies, HashSet model) + { + foreach (var plex in plexMovies) + { + model.Add(new RecentlyAddedMovieModel + { + Id = plex.Id, + ImdbId = plex.ImdbId, + TheMovieDbId = plex.TheMovieDbId, + AddedAt = plex.AddedAt, + Title = plex.Title, + Quality = plex.Quality, + ReleaseYear = plex.ReleaseYear + }); + } + } + } +} diff --git a/src/Ombi.Core/Models/RecentlyAddedMovieModel.cs b/src/Ombi.Core/Models/RecentlyAddedMovieModel.cs new file mode 100644 index 000000000..c63ea98d3 --- /dev/null +++ b/src/Ombi.Core/Models/RecentlyAddedMovieModel.cs @@ -0,0 +1,23 @@ +using System; + +namespace Ombi.Core.Models +{ + public class RecentlyAddedMovieModel + { + public int Id { get; set; } + public string Title { get; set; } + public string Overview { get; set; } + public string ImdbId { get; set; } + public string TvDbId { get; set; } + public string TheMovieDbId { get; set; } + public string ReleaseYear { get; set; } + public DateTime AddedAt { get; set; } + public string Quality { get; set; } + } + + public enum RecentlyAddedType + { + Plex, + Emby + } +} \ No newline at end of file diff --git a/src/Ombi.Store/Repository/EmbyContentRepository.cs b/src/Ombi.Store/Repository/EmbyContentRepository.cs index 519138edc..280243455 100644 --- a/src/Ombi.Store/Repository/EmbyContentRepository.cs +++ b/src/Ombi.Store/Repository/EmbyContentRepository.cs @@ -45,9 +45,9 @@ namespace Ombi.Store.Repository private IOmbiContext Db { get; } - public async Task> GetAll() + public IQueryable GetAll() { - return await Db.EmbyContent.ToListAsync(); + return Db.EmbyContent.AsQueryable(); } public async Task AddRange(IEnumerable content) diff --git a/src/Ombi.Store/Repository/IEmbyContentRepository.cs b/src/Ombi.Store/Repository/IEmbyContentRepository.cs index e6fe18067..3ed8d8abd 100644 --- a/src/Ombi.Store/Repository/IEmbyContentRepository.cs +++ b/src/Ombi.Store/Repository/IEmbyContentRepository.cs @@ -13,7 +13,7 @@ namespace Ombi.Store.Repository Task ContentExists(string providerId); IQueryable Get(); Task Get(string providerId); - Task> GetAll(); + IQueryable GetAll(); Task GetByEmbyId(string embyId); Task Update(EmbyContent existingContent); IQueryable GetAllEpisodes(); diff --git a/src/Ombi/Ombi.csproj b/src/Ombi/Ombi.csproj index 0f0dfb8c6..f55e6a507 100644 --- a/src/Ombi/Ombi.csproj +++ b/src/Ombi/Ombi.csproj @@ -28,15 +28,19 @@ + + + +