mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-07-13 16:52:56 -07:00
Added in the external repo so we can rip out external stuff
This commit is contained in:
parent
69b573a393
commit
479ca819d7
7 changed files with 241 additions and 96 deletions
|
@ -129,9 +129,11 @@ namespace Ombi.DependencyInjection
|
|||
public static void RegisterStore(this IServiceCollection services) {
|
||||
services.AddEntityFrameworkSqlite().AddDbContext<OmbiContext>();
|
||||
services.AddEntityFrameworkSqlite().AddDbContext<SettingsContext>();
|
||||
services.AddEntityFrameworkSqlite().AddDbContext<ExternalContext>();
|
||||
|
||||
services.AddScoped<IOmbiContext, OmbiContext>(); // https://docs.microsoft.com/en-us/aspnet/core/data/entity-framework-6
|
||||
services.AddScoped<ISettingsContext, SettingsContext>(); // https://docs.microsoft.com/en-us/aspnet/core/data/entity-framework-6
|
||||
services.AddScoped<IExternalContext, ExternalContext>(); // https://docs.microsoft.com/en-us/aspnet/core/data/entity-framework-6
|
||||
services.AddTransient<ISettingsRepository, SettingsJsonRepository>();
|
||||
services.AddTransient<ISettingsResolver, SettingsResolver>();
|
||||
services.AddTransient<IPlexContentRepository, PlexServerContentRepository>();
|
||||
|
@ -146,6 +148,7 @@ namespace Ombi.DependencyInjection
|
|||
services.AddTransient<ITokenRepository, TokenRepository>();
|
||||
services.AddTransient(typeof(ISettingsService<>), typeof(SettingsService<>));
|
||||
services.AddTransient(typeof(IRepository<>), typeof(Repository<>));
|
||||
services.AddTransient(typeof(IExternalRepository<>), typeof(ExternalRepository<>));
|
||||
}
|
||||
public static void RegisterServices(this IServiceCollection services)
|
||||
{
|
||||
|
|
67
src/Ombi.Store/Context/ExternalContext.cs
Normal file
67
src/Ombi.Store/Context/ExternalContext.cs
Normal file
|
@ -0,0 +1,67 @@
|
|||
using System.IO;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Ombi.Helpers;
|
||||
using Ombi.Store.Entities;
|
||||
|
||||
namespace Ombi.Store.Context
|
||||
{
|
||||
public sealed class ExternalContext : DbContext, IExternalContext
|
||||
{
|
||||
private static bool _created;
|
||||
public ExternalContext()
|
||||
{
|
||||
if (_created) return;
|
||||
|
||||
_created = true;
|
||||
Database.Migrate();
|
||||
}
|
||||
|
||||
public DbSet<PlexServerContent> PlexServerContent { get; set; }
|
||||
public DbSet<PlexEpisode> PlexEpisode { get; set; }
|
||||
public DbSet<RadarrCache> RadarrCache { get; set; }
|
||||
public DbSet<CouchPotatoCache> CouchPotatoCache { get; set; }
|
||||
public DbSet<EmbyContent> EmbyContent { get; set; }
|
||||
public DbSet<EmbyEpisode> EmbyEpisode { get; set; }
|
||||
|
||||
public DbSet<SonarrCache> SonarrCache { get; set; }
|
||||
public DbSet<LidarrArtistCache> LidarrArtistCache { get; set; }
|
||||
public DbSet<LidarrAlbumCache> LidarrAlbumCache { get; set; }
|
||||
public DbSet<SonarrEpisodeCache> SonarrEpisodeCache { get; set; }
|
||||
public DbSet<SickRageCache> SickRageCache { get; set; }
|
||||
public DbSet<SickRageEpisodeCache> SickRageEpisodeCache { get; set; }
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
var i = StoragePathSingleton.Instance;
|
||||
if (string.IsNullOrEmpty(i.StoragePath))
|
||||
{
|
||||
i.StoragePath = string.Empty;
|
||||
}
|
||||
optionsBuilder.UseSqlite($"Data Source={Path.Combine(i.StoragePath, "ExternalOmbi.db")}");
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder builder)
|
||||
{
|
||||
builder.Entity<PlexServerContent>().HasMany(x => x.Episodes)
|
||||
.WithOne(x => x.Series)
|
||||
.HasPrincipalKey(x => x.Key)
|
||||
.HasForeignKey(x => x.GrandparentKey);
|
||||
|
||||
builder.Entity<EmbyEpisode>()
|
||||
.HasOne(p => p.Series)
|
||||
.WithMany(b => b.Episodes)
|
||||
.HasPrincipalKey(x => x.EmbyId)
|
||||
.HasForeignKey(p => p.ParentId);
|
||||
|
||||
base.OnModelCreating(builder);
|
||||
}
|
||||
|
||||
|
||||
public void Seed()
|
||||
{
|
||||
// VACUUM;
|
||||
Database.ExecuteSqlCommand("VACUUM;");
|
||||
SaveChanges();
|
||||
}
|
||||
}
|
||||
}
|
21
src/Ombi.Store/Context/IExternalContext.cs
Normal file
21
src/Ombi.Store/Context/IExternalContext.cs
Normal file
|
@ -0,0 +1,21 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using Ombi.Store.Entities;
|
||||
|
||||
namespace Ombi.Store.Context
|
||||
{
|
||||
public interface IExternalContext : IDbContext
|
||||
{
|
||||
DbSet<CouchPotatoCache> CouchPotatoCache { get; set; }
|
||||
DbSet<EmbyContent> EmbyContent { get; set; }
|
||||
DbSet<EmbyEpisode> EmbyEpisode { get; set; }
|
||||
DbSet<LidarrAlbumCache> LidarrAlbumCache { get; set; }
|
||||
DbSet<LidarrArtistCache> LidarrArtistCache { get; set; }
|
||||
DbSet<PlexEpisode> PlexEpisode { get; set; }
|
||||
DbSet<PlexServerContent> PlexServerContent { get; set; }
|
||||
DbSet<RadarrCache> RadarrCache { get; set; }
|
||||
DbSet<SickRageCache> SickRageCache { get; set; }
|
||||
DbSet<SickRageEpisodeCache> SickRageEpisodeCache { get; set; }
|
||||
DbSet<SonarrCache> SonarrCache { get; set; }
|
||||
DbSet<SonarrEpisodeCache> SonarrEpisodeCache { get; set; }
|
||||
}
|
||||
}
|
105
src/Ombi.Store/Repository/BaseRepository.cs
Normal file
105
src/Ombi.Store/Repository/BaseRepository.cs
Normal file
|
@ -0,0 +1,105 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Query;
|
||||
using Ombi.Store.Context;
|
||||
using Ombi.Store.Entities;
|
||||
|
||||
namespace Ombi.Store.Repository
|
||||
{
|
||||
public class BaseRepository<T, U> : IRepository<T> where T : Entity where U : IDbContext
|
||||
{
|
||||
public BaseRepository(U ctx)
|
||||
{
|
||||
_ctx = ctx;
|
||||
_db = _ctx.Set<T>();
|
||||
}
|
||||
public DbSet<T> _db { get; }
|
||||
private readonly U _ctx;
|
||||
|
||||
public async Task<T> Find(object key)
|
||||
{
|
||||
return await _db.FindAsync(key);
|
||||
}
|
||||
|
||||
public IQueryable<T> GetAll()
|
||||
{
|
||||
return _db.AsQueryable();
|
||||
}
|
||||
|
||||
public async Task<T> FirstOrDefaultAsync(Expression<Func<T,bool>> predicate)
|
||||
{
|
||||
return await _db.FirstOrDefaultAsync(predicate);
|
||||
}
|
||||
|
||||
public async Task AddRange(IEnumerable<T> content, bool save = true)
|
||||
{
|
||||
_db.AddRange(content);
|
||||
if (save)
|
||||
{
|
||||
await _ctx.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<T> Add(T content)
|
||||
{
|
||||
await _db.AddAsync(content);
|
||||
await _ctx.SaveChangesAsync();
|
||||
return content;
|
||||
}
|
||||
|
||||
public async Task Delete(T request)
|
||||
{
|
||||
_db.Remove(request);
|
||||
await _ctx.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task DeleteRange(IEnumerable<T> req)
|
||||
{
|
||||
_db.RemoveRange(req);
|
||||
await _ctx.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task<int> SaveChangesAsync()
|
||||
{
|
||||
return await _ctx.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public IIncludableQueryable<TEntity, TProperty> Include<TEntity, TProperty>(
|
||||
IQueryable<TEntity> source, Expression<Func<TEntity, TProperty>> navigationPropertyPath)
|
||||
where TEntity : class
|
||||
{
|
||||
return source.Include(navigationPropertyPath);
|
||||
}
|
||||
|
||||
public async Task ExecuteSql(string sql)
|
||||
{
|
||||
await _ctx.Database.ExecuteSqlCommandAsync(sql);
|
||||
}
|
||||
|
||||
|
||||
private bool _disposed;
|
||||
// Protected implementation of Dispose pattern.
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (_disposed)
|
||||
return;
|
||||
|
||||
if (disposing)
|
||||
{
|
||||
_ctx?.Dispose();
|
||||
}
|
||||
|
||||
_disposed = true;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
}
|
12
src/Ombi.Store/Repository/ExternalRepository.cs
Normal file
12
src/Ombi.Store/Repository/ExternalRepository.cs
Normal file
|
@ -0,0 +1,12 @@
|
|||
using Ombi.Store.Context;
|
||||
using Ombi.Store.Entities;
|
||||
|
||||
namespace Ombi.Store.Repository
|
||||
{
|
||||
public class ExternalRepository<T> : BaseRepository<T, IExternalContext>, IExternalRepository<T> where T : Entity
|
||||
{
|
||||
public ExternalRepository(IExternalContext ctx) : base(ctx)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
30
src/Ombi.Store/Repository/IExternalRepository.cs
Normal file
30
src/Ombi.Store/Repository/IExternalRepository.cs
Normal file
|
@ -0,0 +1,30 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Query;
|
||||
using Ombi.Store.Entities;
|
||||
|
||||
namespace Ombi.Store.Repository
|
||||
{
|
||||
public interface IExternalRepository<T> : IDisposable where T : Entity
|
||||
{
|
||||
Task<T> Find(object key);
|
||||
IQueryable<T> GetAll();
|
||||
Task<T> FirstOrDefaultAsync(Expression<Func<T, bool>> predicate);
|
||||
Task AddRange(IEnumerable<T> content, bool save = true);
|
||||
Task<T> Add(T content);
|
||||
Task DeleteRange(IEnumerable<T> req);
|
||||
Task Delete(T request);
|
||||
Task<int> SaveChangesAsync();
|
||||
|
||||
IIncludableQueryable<TEntity, TProperty> Include<TEntity, TProperty>(
|
||||
IQueryable<TEntity> source, Expression<Func<TEntity, TProperty>> navigationPropertyPath)
|
||||
where TEntity : class;
|
||||
|
||||
Task ExecuteSql(string sql);
|
||||
DbSet<T> _db { get; }
|
||||
}
|
||||
}
|
|
@ -1,105 +1,12 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Query;
|
||||
using Ombi.Store.Context;
|
||||
using Ombi.Store.Context;
|
||||
using Ombi.Store.Entities;
|
||||
|
||||
namespace Ombi.Store.Repository
|
||||
{
|
||||
public class Repository<T> : IRepository<T> where T : Entity
|
||||
public class Repository<T> : BaseRepository<T,IOmbiContext>, IRepository<T> where T : Entity
|
||||
{
|
||||
public Repository(IOmbiContext ctx)
|
||||
public Repository(IOmbiContext ctx) : base(ctx)
|
||||
{
|
||||
_ctx = ctx;
|
||||
_db = _ctx.Set<T>();
|
||||
}
|
||||
public DbSet<T> _db { get; }
|
||||
private readonly IOmbiContext _ctx;
|
||||
|
||||
public async Task<T> Find(object key)
|
||||
{
|
||||
return await _db.FindAsync(key);
|
||||
}
|
||||
|
||||
public IQueryable<T> GetAll()
|
||||
{
|
||||
return _db.AsQueryable();
|
||||
}
|
||||
|
||||
public async Task<T> FirstOrDefaultAsync(Expression<Func<T,bool>> predicate)
|
||||
{
|
||||
return await _db.FirstOrDefaultAsync(predicate);
|
||||
}
|
||||
|
||||
public async Task AddRange(IEnumerable<T> content, bool save = true)
|
||||
{
|
||||
_db.AddRange(content);
|
||||
if (save)
|
||||
{
|
||||
await _ctx.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<T> Add(T content)
|
||||
{
|
||||
await _db.AddAsync(content);
|
||||
await _ctx.SaveChangesAsync();
|
||||
return content;
|
||||
}
|
||||
|
||||
public async Task Delete(T request)
|
||||
{
|
||||
_db.Remove(request);
|
||||
await _ctx.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task DeleteRange(IEnumerable<T> req)
|
||||
{
|
||||
_db.RemoveRange(req);
|
||||
await _ctx.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task<int> SaveChangesAsync()
|
||||
{
|
||||
return await _ctx.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public IIncludableQueryable<TEntity, TProperty> Include<TEntity, TProperty>(
|
||||
IQueryable<TEntity> source, Expression<Func<TEntity, TProperty>> navigationPropertyPath)
|
||||
where TEntity : class
|
||||
{
|
||||
return source.Include(navigationPropertyPath);
|
||||
}
|
||||
|
||||
public async Task ExecuteSql(string sql)
|
||||
{
|
||||
await _ctx.Database.ExecuteSqlCommandAsync(sql);
|
||||
}
|
||||
|
||||
|
||||
private bool _disposed;
|
||||
// Protected implementation of Dispose pattern.
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (_disposed)
|
||||
return;
|
||||
|
||||
if (disposing)
|
||||
{
|
||||
_ctx?.Dispose();
|
||||
}
|
||||
|
||||
_disposed = true;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue