From ace7910f2a9b128784ae15ff46b829e579d91404 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Fri, 8 Feb 2013 00:05:43 -0800 Subject: [PATCH] Fixed up Eloquera integration, working much better now. --- NzbDrone.Api/RootFolders/RootFolderModule.cs | 4 +-- NzbDrone.Api/Series/SeriesModule.cs | 6 ++++ .../Datastore/ObjectDatabaseFixture.cs | 14 +++++---- .../Datastore/BaseRepositoryModel.cs | 2 +- NzbDrone.Core/Datastore/BasicRepository.cs | 8 ++--- NzbDrone.Core/Datastore/EloqueraDb.cs | 9 ++++-- NzbDrone.Core/Datastore/EloqueraDbFactory.cs | 12 +++++++ NzbDrone.Core/Repository/Episode.cs | 4 +++ NzbDrone.Core/Repository/Series.cs | 1 - .../RootFolders/RootFolderRepository.cs | 9 ++++-- .../RootFolders/RootFolderService.cs | 31 ++++++++++++------- 11 files changed, 70 insertions(+), 30 deletions(-) diff --git a/NzbDrone.Api/RootFolders/RootFolderModule.cs b/NzbDrone.Api/RootFolders/RootFolderModule.cs index ed44fe195..faa152e92 100644 --- a/NzbDrone.Api/RootFolders/RootFolderModule.cs +++ b/NzbDrone.Api/RootFolders/RootFolderModule.cs @@ -18,7 +18,7 @@ namespace NzbDrone.Api.RootFolders Get["/"] = x => GetRootFolders(); Post["/"] = x => AddRootFolder(); - Delete["/{id}"] = x => DeleteRootFolder((int)x.id); + Delete["/{id}"] = x => DeleteRootFolder((long)x.id); } private Response AddRootFolder() @@ -32,7 +32,7 @@ namespace NzbDrone.Api.RootFolders return _rootFolderService.All().AsResponse(); } - private Response DeleteRootFolder(int folderId) + private Response DeleteRootFolder(long folderId) { _rootFolderService.Remove(folderId); return new Response { StatusCode = HttpStatusCode.OK }; diff --git a/NzbDrone.Api/Series/SeriesModule.cs b/NzbDrone.Api/Series/SeriesModule.cs index b19381ea7..ca0fe7274 100644 --- a/NzbDrone.Api/Series/SeriesModule.cs +++ b/NzbDrone.Api/Series/SeriesModule.cs @@ -26,6 +26,12 @@ namespace NzbDrone.Api.Series { var request = Request.Body.FromJson(); + //Todo: Alert the user if this series already exists + //Todo: We need to create the folder if the user is adding a new series + //(we can just create the folder and it won't blow up if it already exists) + //We also need to remove any special characters from the filename before attempting to create it + + _seriesProvider.AddSeries("", request.Path, request.SeriesId, request.QualityProfileId, null); _jobProvider.QueueJob(typeof(ImportNewSeriesJob)); diff --git a/NzbDrone.Core.Test/Datastore/ObjectDatabaseFixture.cs b/NzbDrone.Core.Test/Datastore/ObjectDatabaseFixture.cs index c057a85d1..fe75c205d 100644 --- a/NzbDrone.Core.Test/Datastore/ObjectDatabaseFixture.cs +++ b/NzbDrone.Core.Test/Datastore/ObjectDatabaseFixture.cs @@ -26,6 +26,7 @@ namespace NzbDrone.Core.Test.Datastore testEpisode = Builder .CreateNew() + .With(e => e.Id = 0) .Build(); @@ -35,9 +36,7 @@ namespace NzbDrone.Core.Test.Datastore public void should_be_able_to_write_to_database() { Db.Insert(testSeries); - Db.AsQueryable().Should().HaveCount(1); - } [Test] @@ -66,7 +65,10 @@ namespace NzbDrone.Core.Test.Datastore [Test] public void should_update_nested_objects() { - testEpisode.Series = Builder.CreateNew().Build(); + testEpisode.Series = Builder + .CreateNew() + .With(s => s.Id = 0) + .Build(); Db.Insert(testEpisode); @@ -82,14 +84,14 @@ namespace NzbDrone.Core.Test.Datastore [Test] public void new_objects_should_get_id() { - Db.Insert(testSeries); + testSeries.Id = Db.InsertAndGetId(testSeries); testSeries.Id.Should().NotBe(0); } [Test] public void should_have_id_when_returned_from_database() { - Db.Insert(testSeries); + testSeries.Id = Db.InsertAndGetId(testSeries); var item = Db.AsQueryable(); item.Should().HaveCount(1); @@ -100,7 +102,7 @@ namespace NzbDrone.Core.Test.Datastore [Test] public void should_be_able_to_find_object_by_id() { - Db.Insert(testSeries); + testSeries.Id = Db.InsertAndGetId(testSeries); var item = Db.AsQueryable().Single(c => c.Id == testSeries.Id); item.Id.Should().NotBe(0); diff --git a/NzbDrone.Core/Datastore/BaseRepositoryModel.cs b/NzbDrone.Core/Datastore/BaseRepositoryModel.cs index e7133afd4..80bbf54e2 100644 --- a/NzbDrone.Core/Datastore/BaseRepositoryModel.cs +++ b/NzbDrone.Core/Datastore/BaseRepositoryModel.cs @@ -9,6 +9,6 @@ namespace NzbDrone.Core.Datastore public abstract class BaseRepositoryModel { [ID] - public int Id; + public long Id; } } diff --git a/NzbDrone.Core/Datastore/BasicRepository.cs b/NzbDrone.Core/Datastore/BasicRepository.cs index 752dc4c46..a58ab04c8 100644 --- a/NzbDrone.Core/Datastore/BasicRepository.cs +++ b/NzbDrone.Core/Datastore/BasicRepository.cs @@ -8,9 +8,9 @@ namespace NzbDrone.Core.Datastore public interface IBasicRepository { List All(); - TModel Get(int rootFolderId); + TModel Get(long rootFolderId); TModel Add(TModel rootFolder); - void Delete(int rootFolderId); + void Delete(long rootFolderId); } public abstract class BasicRepository : IBasicRepository where TModel : BaseRepositoryModel, new() @@ -27,7 +27,7 @@ namespace NzbDrone.Core.Datastore return EloqueraDb.AsQueryable().ToList(); } - public TModel Get(int id) + public TModel Get(long id) { return EloqueraDb.AsQueryable().Single(c => c.Id == id); } @@ -37,7 +37,7 @@ namespace NzbDrone.Core.Datastore return EloqueraDb.Insert(model); } - public void Delete(int id) + public void Delete(long id) { var itemToDelete = Get(id); EloqueraDb.Delete(itemToDelete); diff --git a/NzbDrone.Core/Datastore/EloqueraDb.cs b/NzbDrone.Core/Datastore/EloqueraDb.cs index be00281de..9e598ecb4 100644 --- a/NzbDrone.Core/Datastore/EloqueraDb.cs +++ b/NzbDrone.Core/Datastore/EloqueraDb.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using Eloquera.Client; @@ -19,13 +20,18 @@ namespace NzbDrone.Core.Datastore return _db.Query(); } - public T Insert(T obj) { + //Todo: need to verify that this is properly setting the ID of T _db.Store(obj); return obj; } + public long InsertAndGetId(T obj) + { + return _db.Store(obj); + } + public IList InsertMany(IEnumerable objects) { return DoMany(objects, Insert); @@ -42,7 +48,6 @@ namespace NzbDrone.Core.Datastore return DoMany(objects, Update); } - public void Delete(T obj) where T : new() { _db.Delete(obj); diff --git a/NzbDrone.Core/Datastore/EloqueraDbFactory.cs b/NzbDrone.Core/Datastore/EloqueraDbFactory.cs index a85f8fb11..1f1c138ba 100644 --- a/NzbDrone.Core/Datastore/EloqueraDbFactory.cs +++ b/NzbDrone.Core/Datastore/EloqueraDbFactory.cs @@ -3,6 +3,7 @@ using System.IO; using System.Linq; using Eloquera.Client; using NzbDrone.Common; +using NzbDrone.Core.Repository; using NzbDrone.Core.RootFolders; namespace NzbDrone.Core.Datastore @@ -39,6 +40,7 @@ namespace NzbDrone.Core.Datastore private EloqueraDb InternalCreate(string connectionString, string databaseName) { var db = new DB(connectionString); + try { db.OpenDatabase(databaseName); @@ -49,6 +51,9 @@ namespace NzbDrone.Core.Datastore db.OpenDatabase(databaseName); } + //This seemse to cause Invalid Cast Exceptions... WTF + //db.RefreshMode = ObjectRefreshMode.AlwaysReturnUpdatedValues; + RegisterTypeRules(); RegisterTypes(db); @@ -59,13 +64,20 @@ namespace NzbDrone.Core.Datastore { RootFolder rootFolder = null; DB.TypeRules + //.SetIDField(() => rootFolder.Id) .IgnoreProperty(() => rootFolder.FreeSpace) .IgnoreProperty(() => rootFolder.UnmappedFolders); + + //Series series = null; + //DB.TypeRules + // .SetIDField(() => series.Id); } private void RegisterTypes(DB db) { db.RegisterType(typeof(RootFolder)); + db.RegisterType(typeof(Series)); + db.RegisterType(typeof(Episode)); } } } diff --git a/NzbDrone.Core/Repository/Episode.cs b/NzbDrone.Core/Repository/Episode.cs index b99f9658c..2c45125ab 100644 --- a/NzbDrone.Core/Repository/Episode.cs +++ b/NzbDrone.Core/Repository/Episode.cs @@ -1,4 +1,5 @@ using System; +using Eloquera.Client; using NzbDrone.Core.Model; using PetaPoco; @@ -8,6 +9,9 @@ namespace NzbDrone.Core.Repository [PrimaryKey("EpisodeId", autoIncrement = true)] public class Episode { + [ID] + public long Id; + public int EpisodeId { get; set; } public int? TvDbEpisodeId { get; set; } diff --git a/NzbDrone.Core/Repository/Series.cs b/NzbDrone.Core/Repository/Series.cs index 984f6acdd..4525b3e7c 100644 --- a/NzbDrone.Core/Repository/Series.cs +++ b/NzbDrone.Core/Repository/Series.cs @@ -10,7 +10,6 @@ namespace NzbDrone.Core.Repository [PrimaryKey("SeriesId", autoIncrement = false)] public class Series { - [ID] public long Id; diff --git a/NzbDrone.Core/RootFolders/RootFolderRepository.cs b/NzbDrone.Core/RootFolders/RootFolderRepository.cs index 1aaa5f531..9d7162a87 100644 --- a/NzbDrone.Core/RootFolders/RootFolderRepository.cs +++ b/NzbDrone.Core/RootFolders/RootFolderRepository.cs @@ -11,14 +11,19 @@ namespace NzbDrone.Core.RootFolders } - //This way we only need to implement none_custom methods for repos, like custom queries etc... rest is done automagically. public class RootFolderRepository : BasicRepository, IRootFolderRepository { public RootFolderRepository(EloqueraDb eloqueraDb) : base(eloqueraDb) { + } - } + public RootFolder Add(RootFolder rootFolder) + { + rootFolder.Id = EloqueraDb.InsertAndGetId(rootFolder); + return rootFolder; + } + } } diff --git a/NzbDrone.Core/RootFolders/RootFolderService.cs b/NzbDrone.Core/RootFolders/RootFolderService.cs index 2afa40c34..0319baa2b 100644 --- a/NzbDrone.Core/RootFolders/RootFolderService.cs +++ b/NzbDrone.Core/RootFolders/RootFolderService.cs @@ -13,7 +13,7 @@ namespace NzbDrone.Core.RootFolders { List All(); RootFolder Add(RootFolder rootDir); - void Remove(int rootDirId); + void Remove(long rootDirId); List GetUnmappedFolders(string path); Dictionary FreeSpaceOnDrives(); } @@ -34,28 +34,36 @@ namespace NzbDrone.Core.RootFolders public virtual List All() { - return _rootFolderRepository.All(); + var rootFolders = _rootFolderRepository.All(); + + rootFolders.ForEach(folder => + { + folder.FreeSpace = _diskProvider.FreeDiskSpace(new DirectoryInfo(folder.Path)); + folder.UnmappedFolders = GetUnmappedFolders(folder.Path); + }); + + return rootFolders; } - public virtual RootFolder Add(RootFolder rootDir) + public virtual RootFolder Add(RootFolder rootFolder) { - if (String.IsNullOrWhiteSpace(rootDir.Path) || !Path.IsPathRooted(rootDir.Path)) + if (String.IsNullOrWhiteSpace(rootFolder.Path) || !Path.IsPathRooted(rootFolder.Path)) throw new ArgumentException("Invalid path"); - if (!_diskProvider.FolderExists(rootDir.Path)) + if (!_diskProvider.FolderExists(rootFolder.Path)) throw new DirectoryNotFoundException("Can't add root directory that doesn't exist."); - if (All().Exists(r => DiskProvider.PathEquals(r.Path, rootDir.Path))) + if (All().Exists(r => DiskProvider.PathEquals(r.Path, rootFolder.Path))) throw new InvalidOperationException("Root directory already exist."); - _rootFolderRepository.Add(rootDir); + _rootFolderRepository.Add(rootFolder); - rootDir.FreeSpace = _diskProvider.FreeDiskSpace(new DirectoryInfo(rootDir.Path)); - rootDir.UnmappedFolders = GetUnmappedFolders(rootDir.Path); - return rootDir; + rootFolder.FreeSpace = _diskProvider.FreeDiskSpace(new DirectoryInfo(rootFolder.Path)); + rootFolder.UnmappedFolders = GetUnmappedFolders(rootFolder.Path); + return rootFolder; } - public virtual void Remove(int rootDirId) + public virtual void Remove(long rootDirId) { _rootFolderRepository.Delete(rootDirId); } @@ -86,7 +94,6 @@ namespace NzbDrone.Core.RootFolders return results; } - public virtual Dictionary FreeSpaceOnDrives() { var freeSpace = new Dictionary();