From 012a82ca2dcdb9c704eff30ba7bd666ade2cfe0d Mon Sep 17 00:00:00 2001 From: tidusjar Date: Sat, 2 Sep 2017 00:18:54 +0100 Subject: [PATCH] Added the Emby Cacher, we now cache the Emby data! --- src/Ombi.Api.Emby/EmbyApi.cs | 8 ++ src/Ombi.Api.Emby/IEmbyApi.cs | 3 + .../Models/Search/SearchViewModel.cs | 2 +- src/Ombi.Core/Ombi.Core.csproj | 4 + .../Rule/Rules/Search/EmbyAvailabilityRule.cs | 27 ++++ src/Ombi.DependencyInjection/IocExtensions.cs | 5 +- src/Ombi.Helpers/LoggingEvents.cs | 1 + src/Ombi.Schedule/JobSetup.cs | 14 +- .../Jobs/Emby/EmbyContentCacher.cs | 128 ++++++++++++++++-- .../Jobs/Emby/IEmbyContentCacher.cs | 9 ++ src/Ombi.Store/Context/IOmbiContext.cs | 1 + src/Ombi.Store/Context/OmbiContext.cs | 3 +- src/Ombi.Store/Entities/EmbyContent.cs | 49 +++++++ ...r.cs => 20170901230032_Inital.Designer.cs} | 22 ++- ...646_Inital.cs => 20170901230032_Inital.cs} | 20 +++ .../Migrations/OmbiContextModelSnapshot.cs | 20 +++ .../Repository/EmbyContentRepository.cs | 112 +++++++++++++++ .../Repository/IEmbyContentRepository.cs | 19 +++ src/Ombi/Controllers/SettingsController.cs | 12 +- 19 files changed, 440 insertions(+), 19 deletions(-) create mode 100644 src/Ombi.Core/Rule/Rules/Search/EmbyAvailabilityRule.cs create mode 100644 src/Ombi.Schedule/Jobs/Emby/IEmbyContentCacher.cs create mode 100644 src/Ombi.Store/Entities/EmbyContent.cs rename src/Ombi.Store/Migrations/{20170825114646_Inital.Designer.cs => 20170901230032_Inital.Designer.cs} (97%) rename src/Ombi.Store/Migrations/{20170825114646_Inital.cs => 20170901230032_Inital.cs} (97%) create mode 100644 src/Ombi.Store/Repository/EmbyContentRepository.cs create mode 100644 src/Ombi.Store/Repository/IEmbyContentRepository.cs diff --git a/src/Ombi.Api.Emby/EmbyApi.cs b/src/Ombi.Api.Emby/EmbyApi.cs index 2f85051ac..3ff12d318 100644 --- a/src/Ombi.Api.Emby/EmbyApi.cs +++ b/src/Ombi.Api.Emby/EmbyApi.cs @@ -66,6 +66,14 @@ namespace Ombi.Api.Emby return obj; } + public async Task> GetCollection(string mediaId, string apiKey, string userId, string baseUrl) + { + var request = new Request($"emby/users/{userId}/items?parentId={mediaId}", baseUrl, HttpMethod.Get); + AddHeaders(request, apiKey); + + return await Api.Request>(request); + } + public async Task> GetAllMovies(string apiKey, string userId, string baseUri) { return await GetAll("Movie", apiKey, userId, baseUri); diff --git a/src/Ombi.Api.Emby/IEmbyApi.cs b/src/Ombi.Api.Emby/IEmbyApi.cs index 8cf31fca8..4ca7ee78a 100644 --- a/src/Ombi.Api.Emby/IEmbyApi.cs +++ b/src/Ombi.Api.Emby/IEmbyApi.cs @@ -17,6 +17,9 @@ namespace Ombi.Api.Emby Task> GetAllEpisodes(string apiKey, string userId, string baseUri); Task> GetAllShows(string apiKey, string userId, string baseUri); + Task> GetCollection(string mediaId, string apiKey, string userId, + string baseUrl); + Task GetSeriesInformation(string mediaId, string apiKey, string userId, string baseUrl); Task GetMovieInformation(string mediaId, string apiKey, string userId, string baseUrl); Task GetEpisodeInformation(string mediaId, string apiKey, string userId, string baseUrl); diff --git a/src/Ombi.Core/Models/Search/SearchViewModel.cs b/src/Ombi.Core/Models/Search/SearchViewModel.cs index 85ff4786e..fec2af075 100644 --- a/src/Ombi.Core/Models/Search/SearchViewModel.cs +++ b/src/Ombi.Core/Models/Search/SearchViewModel.cs @@ -13,7 +13,7 @@ namespace Ombi.Core.Models.Search /// - /// This is used for the PlexAvailabilityCheck rule + /// This is used for the PlexAvailabilityCheck/EmbyAvailabilityRule rule /// /// /// The custom identifier. diff --git a/src/Ombi.Core/Ombi.Core.csproj b/src/Ombi.Core/Ombi.Core.csproj index 871a50400..a9befe3f8 100644 --- a/src/Ombi.Core/Ombi.Core.csproj +++ b/src/Ombi.Core/Ombi.Core.csproj @@ -38,4 +38,8 @@ + + + + \ No newline at end of file diff --git a/src/Ombi.Core/Rule/Rules/Search/EmbyAvailabilityRule.cs b/src/Ombi.Core/Rule/Rules/Search/EmbyAvailabilityRule.cs new file mode 100644 index 000000000..44670e7f8 --- /dev/null +++ b/src/Ombi.Core/Rule/Rules/Search/EmbyAvailabilityRule.cs @@ -0,0 +1,27 @@ +using System.Threading.Tasks; +using Ombi.Core.Models.Search; +using Ombi.Core.Rule.Interfaces; +using Ombi.Store.Repository; + +namespace Ombi.Core.Rule.Rules.Search +{ + public class EmbyAvailabilityRule : BaseSearchRule, IRules + { + public EmbyAvailabilityRule(IEmbyContentRepository repo) + { + EmbyContentRepository = repo; + } + + private IEmbyContentRepository EmbyContentRepository { get; } + + public async Task Execute(SearchViewModel obj) + { + var item = await EmbyContentRepository.Get(obj.CustomId); + if (item != null) + { + obj.Available = true; + } + return Success(); + } + } +} \ No newline at end of file diff --git a/src/Ombi.DependencyInjection/IocExtensions.cs b/src/Ombi.DependencyInjection/IocExtensions.cs index 2dfe02c1d..9ca43c7fd 100644 --- a/src/Ombi.DependencyInjection/IocExtensions.cs +++ b/src/Ombi.DependencyInjection/IocExtensions.cs @@ -36,6 +36,7 @@ using Ombi.Api.Service; using Ombi.Api.Slack; using Ombi.Core.Rule.Interfaces; using Ombi.Core.Senders; +using Ombi.Schedule.Jobs.Emby; using Ombi.Schedule.Jobs.Ombi; using Ombi.Schedule.Jobs.Plex; using Ombi.Schedule.Ombi; @@ -63,7 +64,7 @@ namespace Ombi.DependencyInjection services.AddTransient(); services.AddTransient(); services.AddTransient(); - services.AddSingleton(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); } @@ -95,6 +96,7 @@ namespace Ombi.DependencyInjection services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -124,6 +126,7 @@ namespace Ombi.DependencyInjection public static void RegisterJobs(this IServiceCollection services) { services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); diff --git a/src/Ombi.Helpers/LoggingEvents.cs b/src/Ombi.Helpers/LoggingEvents.cs index 9c45e0831..657ac1a37 100644 --- a/src/Ombi.Helpers/LoggingEvents.cs +++ b/src/Ombi.Helpers/LoggingEvents.cs @@ -12,6 +12,7 @@ namespace Ombi.Helpers public static EventId Cacher => new EventId(2000); public static EventId RadarrCacher => new EventId(2001); public static EventId PlexEpisodeCacher => new EventId(2001); + public static EventId EmbyContentCacher => new EventId(2002); public static EventId MovieSender => new EventId(3000); diff --git a/src/Ombi.Schedule/JobSetup.cs b/src/Ombi.Schedule/JobSetup.cs index 6816fe677..14806e312 100644 --- a/src/Ombi.Schedule/JobSetup.cs +++ b/src/Ombi.Schedule/JobSetup.cs @@ -1,5 +1,6 @@ using Hangfire; using Ombi.Schedule.Jobs; +using Ombi.Schedule.Jobs.Emby; using Ombi.Schedule.Jobs.Radarr; using Ombi.Schedule.Ombi; @@ -7,19 +8,24 @@ namespace Ombi.Schedule { public class JobSetup : IJobSetup { - public JobSetup(IPlexContentCacher cacher, IRadarrCacher radarrCacher, IOmbiAutomaticUpdater updater) + public JobSetup(IPlexContentCacher plexContentCacher, IRadarrCacher radarrCacher, + IOmbiAutomaticUpdater updater, IEmbyContentCacher embyCacher) { - Cacher = cacher; + PlexContentCacher = plexContentCacher; RadarrCacher = radarrCacher; Updater = updater; + EmbyContentCacher = embyCacher; } - private IPlexContentCacher Cacher { get; } + private IPlexContentCacher PlexContentCacher { get; } private IRadarrCacher RadarrCacher { get; } private IOmbiAutomaticUpdater Updater { get; } + private IEmbyContentCacher EmbyContentCacher { get; } + public void Setup() { - RecurringJob.AddOrUpdate(() => Cacher.CacheContent(), Cron.Hourly); + RecurringJob.AddOrUpdate(() => PlexContentCacher.CacheContent(), Cron.Hourly); + RecurringJob.AddOrUpdate(() => EmbyContentCacher.Start(), Cron.Hourly); RecurringJob.AddOrUpdate(() => RadarrCacher.CacheContent(), Cron.Hourly); //RecurringJob.AddOrUpdate(() => Updater.Update(), Cron.Daily); diff --git a/src/Ombi.Schedule/Jobs/Emby/EmbyContentCacher.cs b/src/Ombi.Schedule/Jobs/Emby/EmbyContentCacher.cs index c8fad858a..d6dd16780 100644 --- a/src/Ombi.Schedule/Jobs/Emby/EmbyContentCacher.cs +++ b/src/Ombi.Schedule/Jobs/Emby/EmbyContentCacher.cs @@ -1,32 +1,140 @@ -using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; using Ombi.Api.Emby; +using Ombi.Api.Emby.Models.Movie; using Ombi.Core.Settings; using Ombi.Core.Settings.Models.External; +using Ombi.Helpers; +using Ombi.Store.Entities; +using Ombi.Store.Repository; using Serilog; +using EmbyMediaType = Ombi.Store.Entities.EmbyMediaType; namespace Ombi.Schedule.Jobs.Emby { - public class EmbyContentCacher + public class EmbyContentCacher : IEmbyContentCacher { - public EmbyContentCacher(ISettingsService settings, IEmbyApi api, ILogger logger) + public EmbyContentCacher(ISettingsService settings, IEmbyApi api, ILogger logger, + IEmbyContentRepository repo) { + _logger = logger; + _settings = settings; + _api = api; + _repo = repo; + } + + private readonly ILogger _logger; + private readonly ISettingsService _settings; + private readonly IEmbyApi _api; + private readonly IEmbyContentRepository _repo; + + + public async Task Start() + { + var embySettings = await _settings.GetSettingsAsync(); + if (!embySettings.Enable) + return; + foreach (var server in embySettings.Servers) + await StartServerCache(server); + + // Episodes + //BackgroundJob.Enqueue(() => ); } - private bool ValidateSettings(EmbySettings emby) + + + private async Task StartServerCache(EmbyServers server) { - if (emby.Enable) + if (!ValidateSettings(server)) + return; + + var movies = await _api.GetAllMovies(server.ApiKey, server.AdministratorId, server.FullUri); + var mediaToAdd = new List(); + foreach (var movie in movies.Items) { - foreach (var server in emby.Servers) + if (movie.Type.Equals("boxset", StringComparison.CurrentCultureIgnoreCase)) { - if (server?.Ip == null || string.IsNullOrEmpty(server?.ApiKey)) + var movieInfo = + await _api.GetCollection(movie.Id, server.ApiKey, server.AdministratorId, server.FullUri); + foreach (var item in movieInfo.Items) { - //Log.Warn("A setting is null, Ensure Emby is configured correctly, and we have a Emby Auth token."); - return false; + var info = await _api.GetMovieInformation(item.Id, server.ApiKey, + server.AdministratorId, server.FullUri); + await ProcessMovies(info, mediaToAdd); } } + else + { + // Regular movie + var movieInfo = await _api.GetMovieInformation(movie.Id, server.ApiKey, + server.AdministratorId, server.FullUri); + + await ProcessMovies(movieInfo, mediaToAdd); + } } - return emby.Enable; + // TV Time + var tv = await _api.GetAllShows(server.ApiKey, server.AdministratorId, server.FullUri); + + foreach (var tvShow in tv.Items) + { + var tvInfo = await _api.GetSeriesInformation(tvShow.Id, server.ApiKey, server.AdministratorId, + server.FullUri); + if (string.IsNullOrEmpty(tvInfo.ProviderIds?.Tvdb)) + { + Log.Error("Provider Id on tv {0} is null", tvShow.Name); + continue; + } + + var existingTv = await _repo.GetByEmbyId(tvShow.Id); + if (existingTv == null) + mediaToAdd.Add(new EmbyContent + { + ProviderId = tvInfo.ProviderIds.Tvdb, + Title = tvInfo.Name, + Type = EmbyMediaType.Series, + EmbyId = tvShow.Id, + AddedAt = DateTime.UtcNow + }); + } + + if (mediaToAdd.Any()) + await _repo.AddRange(mediaToAdd); + } + + private async Task ProcessMovies(MovieInformation movieInfo, ICollection content) + { + if (string.IsNullOrEmpty(movieInfo.ProviderIds.Imdb)) + { + Log.Error("Provider Id on movie {0} is null", movieInfo.Name); + return; + } + // Check if it exists + var existingMovie = await _repo.GetByEmbyId(movieInfo.Id); + + if (existingMovie == null) + content.Add(new EmbyContent + { + ProviderId = movieInfo.ProviderIds.Imdb, + Title = movieInfo.Name, + Type = EmbyMediaType.Movie, + EmbyId = movieInfo.Id, + AddedAt = DateTime.UtcNow, + }); } + private bool ValidateSettings(EmbyServers server) + { + if (server?.Ip == null || string.IsNullOrEmpty(server?.ApiKey)) + { + _logger.LogInformation(LoggingEvents.EmbyContentCacher, $"Server {server?.Name} is not configured correctly"); + return false; + } + + return true; + } } + } \ No newline at end of file diff --git a/src/Ombi.Schedule/Jobs/Emby/IEmbyContentCacher.cs b/src/Ombi.Schedule/Jobs/Emby/IEmbyContentCacher.cs new file mode 100644 index 000000000..075156a63 --- /dev/null +++ b/src/Ombi.Schedule/Jobs/Emby/IEmbyContentCacher.cs @@ -0,0 +1,9 @@ +using System.Threading.Tasks; + +namespace Ombi.Schedule.Jobs.Emby +{ + public interface IEmbyContentCacher + { + Task Start(); + } +} \ No newline at end of file diff --git a/src/Ombi.Store/Context/IOmbiContext.cs b/src/Ombi.Store/Context/IOmbiContext.cs index bb00bd8a1..3757e26fd 100644 --- a/src/Ombi.Store/Context/IOmbiContext.cs +++ b/src/Ombi.Store/Context/IOmbiContext.cs @@ -17,6 +17,7 @@ namespace Ombi.Store.Context DbSet PlexContent { get; set; } DbSet PlexEpisode { get; set; } DbSet RadarrCache { get; set; } + DbSet EmbyContent { get; set; } DatabaseFacade Database { get; } EntityEntry Entry(T entry) where T : class; EntityEntry Attach(TEntity entity) where TEntity : class; diff --git a/src/Ombi.Store/Context/OmbiContext.cs b/src/Ombi.Store/Context/OmbiContext.cs index 7c1fea71f..9eb0764a7 100644 --- a/src/Ombi.Store/Context/OmbiContext.cs +++ b/src/Ombi.Store/Context/OmbiContext.cs @@ -28,7 +28,8 @@ namespace Ombi.Store.Context public DbSet PlexContent { get; set; } public DbSet PlexEpisode { get; set; } public DbSet RadarrCache { get; set; } - + public DbSet EmbyContent { get; set; } + public DbSet MovieRequests { get; set; } public DbSet TvRequests { get; set; } public DbSet ChildRequests { get; set; } diff --git a/src/Ombi.Store/Entities/EmbyContent.cs b/src/Ombi.Store/Entities/EmbyContent.cs new file mode 100644 index 000000000..340f8dc26 --- /dev/null +++ b/src/Ombi.Store/Entities/EmbyContent.cs @@ -0,0 +1,49 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2017 Jamie Rees +// File: EmbyContent.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion + +using System; +using System.ComponentModel.DataAnnotations.Schema; + +namespace Ombi.Store.Entities +{ + [Table("EmbyContent")] + public class EmbyContent : Entity + { + public string Title { get; set; } + public string ProviderId { get; set; } + public string EmbyId { get; set; } + public EmbyMediaType Type { get; set; } + public DateTime AddedAt { get; set; } + } + + public enum EmbyMediaType + { + Movie = 0, + Series = 1, + Music = 2 + } +} \ No newline at end of file diff --git a/src/Ombi.Store/Migrations/20170825114646_Inital.Designer.cs b/src/Ombi.Store/Migrations/20170901230032_Inital.Designer.cs similarity index 97% rename from src/Ombi.Store/Migrations/20170825114646_Inital.Designer.cs rename to src/Ombi.Store/Migrations/20170901230032_Inital.Designer.cs index 4f5573b00..4e85bd89b 100644 --- a/src/Ombi.Store/Migrations/20170825114646_Inital.Designer.cs +++ b/src/Ombi.Store/Migrations/20170901230032_Inital.Designer.cs @@ -10,7 +10,7 @@ using Ombi.Helpers; namespace Ombi.Store.Migrations { [DbContext(typeof(OmbiContext))] - [Migration("20170825114646_Inital")] + [Migration("20170901230032_Inital")] partial class Inital { protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -159,6 +159,26 @@ namespace Ombi.Store.Migrations b.ToTable("Audit"); }); + modelBuilder.Entity("Ombi.Store.Entities.EmbyContent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd(); + + b.Property("AddedAt"); + + b.Property("EmbyId"); + + b.Property("ProviderId"); + + b.Property("Title"); + + b.Property("Type"); + + b.HasKey("Id"); + + b.ToTable("EmbyContent"); + }); + modelBuilder.Entity("Ombi.Store.Entities.GlobalSettings", b => { b.Property("Id") diff --git a/src/Ombi.Store/Migrations/20170825114646_Inital.cs b/src/Ombi.Store/Migrations/20170901230032_Inital.cs similarity index 97% rename from src/Ombi.Store/Migrations/20170825114646_Inital.cs rename to src/Ombi.Store/Migrations/20170901230032_Inital.cs index 3229f6fbf..f71430088 100644 --- a/src/Ombi.Store/Migrations/20170825114646_Inital.cs +++ b/src/Ombi.Store/Migrations/20170901230032_Inital.cs @@ -67,6 +67,23 @@ namespace Ombi.Store.Migrations table.PrimaryKey("PK_Audit", x => x.Id); }); + migrationBuilder.CreateTable( + name: "EmbyContent", + columns: table => new + { + Id = table.Column(nullable: false) + .Annotation("Sqlite:Autoincrement", true), + AddedAt = table.Column(nullable: false), + EmbyId = table.Column(nullable: true), + ProviderId = table.Column(nullable: true), + Title = table.Column(nullable: true), + Type = table.Column(nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_EmbyContent", x => x.Id); + }); + migrationBuilder.CreateTable( name: "GlobalSettings", columns: table => new @@ -621,6 +638,9 @@ namespace Ombi.Store.Migrations migrationBuilder.DropTable( name: "Audit"); + migrationBuilder.DropTable( + name: "EmbyContent"); + migrationBuilder.DropTable( name: "GlobalSettings"); diff --git a/src/Ombi.Store/Migrations/OmbiContextModelSnapshot.cs b/src/Ombi.Store/Migrations/OmbiContextModelSnapshot.cs index 585745fea..31386ec62 100644 --- a/src/Ombi.Store/Migrations/OmbiContextModelSnapshot.cs +++ b/src/Ombi.Store/Migrations/OmbiContextModelSnapshot.cs @@ -158,6 +158,26 @@ namespace Ombi.Store.Migrations b.ToTable("Audit"); }); + modelBuilder.Entity("Ombi.Store.Entities.EmbyContent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd(); + + b.Property("AddedAt"); + + b.Property("EmbyId"); + + b.Property("ProviderId"); + + b.Property("Title"); + + b.Property("Type"); + + b.HasKey("Id"); + + b.ToTable("EmbyContent"); + }); + modelBuilder.Entity("Ombi.Store.Entities.GlobalSettings", b => { b.Property("Id") diff --git a/src/Ombi.Store/Repository/EmbyContentRepository.cs b/src/Ombi.Store/Repository/EmbyContentRepository.cs new file mode 100644 index 000000000..d32f80498 --- /dev/null +++ b/src/Ombi.Store/Repository/EmbyContentRepository.cs @@ -0,0 +1,112 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2017 Jamie Rees +// File: PlexContentRepository.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion + +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Ombi.Store.Context; +using Ombi.Store.Entities; + +namespace Ombi.Store.Repository +{ + public class EmbyContentRepository : IEmbyContentRepository + { + + public EmbyContentRepository(IOmbiContext db) + { + Db = db; + } + + private IOmbiContext Db { get; } + + public async Task> GetAll() + { + return await Db.EmbyContent.ToListAsync(); + } + + public async Task AddRange(IEnumerable content) + { + Db.EmbyContent.AddRange(content); + await Db.SaveChangesAsync(); + } + + public async Task ContentExists(string providerId) + { + return await Db.EmbyContent.AnyAsync(x => x.ProviderId == providerId); + } + + public async Task Add(EmbyContent content) + { + await Db.EmbyContent.AddAsync(content); + await Db.SaveChangesAsync(); + return content; + } + + public async Task Get(string providerId) + { + return await Db.EmbyContent.FirstOrDefaultAsync(x => x.ProviderId == providerId); + } + + public IQueryable Get() + { + return Db.EmbyContent.AsQueryable(); + } + + public async Task GetByEmbyId(string embyId) + { + return await Db.EmbyContent./*Include(x => x.Seasons).*/FirstOrDefaultAsync(x => x.EmbyId == embyId); + } + + public async Task Update(EmbyContent existingContent) + { + Db.EmbyContent.Update(existingContent); + await Db.SaveChangesAsync(); + } + + //public IQueryable GetAllEpisodes() + //{ + // return Db.PlexEpisode.AsQueryable(); + //} + + //public async Task Add(PlexEpisode content) + //{ + // await Db.PlexEpisode.AddAsync(content); + // await Db.SaveChangesAsync(); + // return content; + //} + //public async Task GetEpisodeByKey(int key) + //{ + // return await Db.PlexEpisode.FirstOrDefaultAsync(x => x.Key == key); + //} + //public async Task AddRange(IEnumerable content) + //{ + // Db.PlexEpisode.AddRange(content); + // await Db.SaveChangesAsync(); + //} + } +} \ No newline at end of file diff --git a/src/Ombi.Store/Repository/IEmbyContentRepository.cs b/src/Ombi.Store/Repository/IEmbyContentRepository.cs new file mode 100644 index 000000000..2e6eb0384 --- /dev/null +++ b/src/Ombi.Store/Repository/IEmbyContentRepository.cs @@ -0,0 +1,19 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Ombi.Store.Entities; + +namespace Ombi.Store.Repository +{ + public interface IEmbyContentRepository + { + Task Add(EmbyContent content); + Task AddRange(IEnumerable content); + Task ContentExists(string providerId); + IQueryable Get(); + Task Get(string providerId); + Task> GetAll(); + Task GetByEmbyId(string embyId); + Task Update(EmbyContent existingContent); + } +} \ No newline at end of file diff --git a/src/Ombi/Controllers/SettingsController.cs b/src/Ombi/Controllers/SettingsController.cs index a8946eab0..eaedc81ef 100644 --- a/src/Ombi/Controllers/SettingsController.cs +++ b/src/Ombi/Controllers/SettingsController.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using AutoMapper; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using Ombi.Api.Emby; using Ombi.Attributes; using Ombi.Core.Models.UI; using Ombi.Core.Settings; @@ -33,16 +34,19 @@ namespace Ombi.Controllers /// The resolver. /// The mapper. /// The templateRepo. - public SettingsController(ISettingsResolver resolver, IMapper mapper, INotificationTemplatesRepository templateRepo) + public SettingsController(ISettingsResolver resolver, IMapper mapper, INotificationTemplatesRepository templateRepo, + IEmbyApi embyApi) { SettingsResolver = resolver; Mapper = mapper; TemplateRepository = templateRepo; + _embyApi = embyApi; } private ISettingsResolver SettingsResolver { get; } private IMapper Mapper { get; } private INotificationTemplatesRepository TemplateRepository { get; } + private readonly IEmbyApi _embyApi; /// /// Gets the Ombi settings. @@ -114,6 +118,12 @@ namespace Ombi.Controllers [HttpPost("emby")] public async Task EmbySettings([FromBody]EmbySettings emby) { + foreach (var server in emby.Servers) + { + var users = await _embyApi.GetUsers(server.FullUri, server.ApiKey); + var admin = users.FirstOrDefault(x => x.Policy.IsAdministrator); + server.AdministratorId = admin?.Id; + } return await Save(emby); }