From eb90040dd6ab6e41b61a70212bdcdaadb65307d6 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Fri, 15 Feb 2013 20:03:54 -0800 Subject: [PATCH] elq id is now ignored by petapoco. --- .../Datastore/ObjectDatabaseFixture.cs | 18 ++++++++- .../Datastore/BaseRepositoryModel.cs | 1 + NzbDrone.Core/Datastore/EloqueraDb.cs | 36 +++++++++++++----- .../PetaPoco/EpisodeSeasonRelator.cs | 2 +- NzbDrone.Core/Providers/SeasonProvider.cs | Bin 7104 -> 7063 bytes 5 files changed, 44 insertions(+), 13 deletions(-) diff --git a/NzbDrone.Core.Test/Datastore/ObjectDatabaseFixture.cs b/NzbDrone.Core.Test/Datastore/ObjectDatabaseFixture.cs index 146efd742..fc460629f 100644 --- a/NzbDrone.Core.Test/Datastore/ObjectDatabaseFixture.cs +++ b/NzbDrone.Core.Test/Datastore/ObjectDatabaseFixture.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using Eloquera.Client; @@ -41,6 +42,20 @@ namespace NzbDrone.Core.Test.Datastore Db.AsQueryable().Should().HaveCount(1); } + [Test] + public void double_insert_should_fail() + { + Db.Insert(testSeries); + Assert.Throws(() => Db.Insert(testSeries)); + } + + [Test] + public void update_item_with_root_index_0_should_faile() + { + testSeries.Id = 0; + Assert.Throws(() => Db.Update(testSeries)); + } + [Test] public void should_not_store_dirty_data_in_cache() { @@ -92,11 +107,10 @@ namespace NzbDrone.Core.Test.Datastore } [Test] - public void new_existing_object_should_get_new_id() + public void new_object_should_get_new_id() { testSeries.Id = 0; Db.Insert(testSeries); - Db.Insert(testSeries); Db.AsQueryable().Should().HaveCount(1); testSeries.Id.Should().Be(1); diff --git a/NzbDrone.Core/Datastore/BaseRepositoryModel.cs b/NzbDrone.Core/Datastore/BaseRepositoryModel.cs index 538239534..01d8d4928 100644 --- a/NzbDrone.Core/Datastore/BaseRepositoryModel.cs +++ b/NzbDrone.Core/Datastore/BaseRepositoryModel.cs @@ -8,6 +8,7 @@ namespace NzbDrone.Core.Datastore [ID] private long _eqId; + [PetaPoco.Ignore] public int Id { get; set; } } } diff --git a/NzbDrone.Core/Datastore/EloqueraDb.cs b/NzbDrone.Core/Datastore/EloqueraDb.cs index 3d8bc7a41..ca9909d93 100644 --- a/NzbDrone.Core/Datastore/EloqueraDb.cs +++ b/NzbDrone.Core/Datastore/EloqueraDb.cs @@ -25,35 +25,51 @@ namespace NzbDrone.Core.Datastore public T Insert(T obj) where T : BaseRepositoryModel { + if (obj.Id != 0) + { + throw new InvalidOperationException("Attempted to insert object with existing ID as new object"); + } + _idService.EnsureIds(obj, new HashSet()); Db.Store(obj); return obj; } - public IList InsertMany(IList objects) where T : BaseRepositoryModel + public T Update(T obj) where T : BaseRepositoryModel { - _idService.EnsureIds(objects, new HashSet()); - return DoMany(objects, Insert); - } + if (obj.Id == 0) + { + throw new InvalidOperationException("Attempted to update object without ID"); + } - public T Update(T obj) - { + _idService.EnsureIds(obj, new HashSet()); Db.Store(obj); return obj; } - public IList UpdateMany(IList objects) + public IList InsertMany(IList objects) where T : BaseRepositoryModel + { + _idService.EnsureIds(objects, new HashSet()); + return DoMany(objects, Insert); + } + + public IList UpdateMany(IList objects) where T : BaseRepositoryModel { _idService.EnsureIds(objects, new HashSet()); return DoMany(objects, Update); } - public void Delete(T obj) where T : new() + public void Delete(T obj) where T : BaseRepositoryModel { + if (obj.Id == 0) + { + throw new InvalidOperationException("Attempted to delete an object without an ID"); + } + Db.Delete(obj); } - public void DeleteMany(IEnumerable objects) where T : new() + public void DeleteMany(IEnumerable objects) where T : BaseRepositoryModel { foreach (var o in objects) { @@ -61,7 +77,7 @@ namespace NzbDrone.Core.Datastore } } - private IList DoMany(IEnumerable objects, Func function) + private IList DoMany(IEnumerable objects, Func function) where T : BaseRepositoryModel { return objects.Select(function).ToList(); } diff --git a/NzbDrone.Core/Datastore/PetaPoco/EpisodeSeasonRelator.cs b/NzbDrone.Core/Datastore/PetaPoco/EpisodeSeasonRelator.cs index ddad384e7..462b1c11d 100644 --- a/NzbDrone.Core/Datastore/PetaPoco/EpisodeSeasonRelator.cs +++ b/NzbDrone.Core/Datastore/PetaPoco/EpisodeSeasonRelator.cs @@ -2,7 +2,7 @@ using System.Linq; using NzbDrone.Core.Repository; -namespace NzbDrone.Core.Datastore.PetaPoco +namespace PetaPoco { public class EpisodeSeasonRelator { diff --git a/NzbDrone.Core/Providers/SeasonProvider.cs b/NzbDrone.Core/Providers/SeasonProvider.cs index 500fc5c58fa63400359dc3cd676ec39ebec693d1..7e65b268be4345a60e67f4fc30fe602e6fd57a80 100644 GIT binary patch delta 10 RcmX?LKHYpm`o;+c(f}F+1W5n@ delta 29 kcmbPke!zS}x{ym^Nn&wHeo?AkKx#>1Kz?%m#sGI|0IVkqA^-pY