#region Copyright // /************************************************************************ // Copyright (c) 2017 Jamie Rees // File: PlexServerContentRepository.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.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Ombi.Helpers; using Ombi.Store.Context; using Ombi.Store.Entities; namespace Ombi.Store.Repository { public class PlexServerContentRepository : MediaServerContentRepository, IPlexContentRepository { public override RecentlyAddedType RecentlyAddedType => RecentlyAddedType.Plex; public PlexServerContentRepository(ExternalContext db) : base(db) { } public async Task ContentExists(string providerId) { var any = await Db.PlexServerContent.AnyAsync(x => x.ImdbId == providerId); if (!any) { any = await Db.PlexServerContent.AnyAsync(x => x.TheMovieDbId == providerId); if (!any) { any = await Db.PlexServerContent.AnyAsync(x => x.TvDbId == providerId); } } return any; } public async Task Get(string providerId, ProviderType type) { switch (type) { case ProviderType.ImdbId: return await Db.PlexServerContent.FirstOrDefaultAsync(x => x.ImdbId == providerId); case ProviderType.TheMovieDbId: return await Db.PlexServerContent.FirstOrDefaultAsync(x => x.TheMovieDbId == providerId); case ProviderType.TvDbId: return await Db.PlexServerContent.FirstOrDefaultAsync(x => x.TvDbId == providerId); default: break; } return null; } public async Task GetByType(string providerId, ProviderType type, MediaType mediaType) { switch (type) { case ProviderType.ImdbId: return await Db.PlexServerContent.FirstOrDefaultAsync(x => x.ImdbId == providerId && x.Type == mediaType); case ProviderType.TheMovieDbId: return await Db.PlexServerContent.FirstOrDefaultAsync(x => x.TheMovieDbId == providerId && x.Type == mediaType); case ProviderType.TvDbId: return await Db.PlexServerContent.FirstOrDefaultAsync(x => x.TvDbId == providerId && x.Type == mediaType); default: break; } return null; } public async Task GetByKey(string key) { return await Db.PlexServerContent.Include(x => x.Seasons).FirstOrDefaultAsync(x => x.Key == key); } public IEnumerable GetWhereContentByCustom(Expression> predicate) { return Db.PlexServerContent.Where(predicate); } public async Task GetFirstContentByCustom(Expression> predicate) { return await Db.PlexServerContent .Include(x => x.Seasons) .Include(x => x.Episodes) .FirstOrDefaultAsync(predicate); } public override async Task Update(IMediaServerContent existingContent) { Db.PlexServerContent.Update((PlexServerContent)existingContent); await InternalSaveChanges(); } public override void UpdateWithoutSave(IMediaServerContent existingContent) { Db.PlexServerContent.Update((PlexServerContent)existingContent); } public async Task UpdateRange(IEnumerable existingContent) { Db.PlexServerContent.UpdateRange(existingContent); await InternalSaveChanges(); } public override IQueryable GetAllEpisodes() { return Db.PlexEpisode.Include(x => x.Series).AsQueryable(); } public void DeleteWithoutSave(PlexServerContent content) { Db.PlexServerContent.Remove(content); } public void DeleteWithoutSave(PlexEpisode content) { Db.PlexEpisode.Remove(content); } public override async Task Add(IMediaServerEpisode content) { await Db.PlexEpisode.AddAsync((PlexEpisode)content); await InternalSaveChanges(); return content; } public async Task DeleteEpisode(PlexEpisode content) { Db.PlexEpisode.Remove(content); await InternalSaveChanges(); } public async Task GetEpisodeByKey(string key) { return await Db.PlexEpisode.FirstOrDefaultAsync(x => x.Key == key); } public override async Task AddRange(IEnumerable content) { Db.PlexEpisode.AddRange((IEnumerable)content); await InternalSaveChanges(); } public override Task UpdateRange(IEnumerable existingContent) { Db.PlexServerContent.UpdateRange((IEnumerable)existingContent); return InternalSaveChanges(); } } }