From b04fb32f77e764fa23534caa3b4cb456fe658475 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Sat, 23 Mar 2013 21:25:16 -0700 Subject: [PATCH] updated seriesrepo to use the new syntax. --- .../Datastore/ObjectDatabaseFixture.cs | 2 +- NzbDrone.Core/Datastore/BasicRepository.cs | 62 ++++++++++-------- NzbDrone.Core/Tv/EpisodeRepository.cs | Bin 6942 -> 6944 bytes NzbDrone.Core/Tv/SeriesRepository.cs | 12 ++-- 4 files changed, 42 insertions(+), 34 deletions(-) diff --git a/NzbDrone.Core.Test/Datastore/ObjectDatabaseFixture.cs b/NzbDrone.Core.Test/Datastore/ObjectDatabaseFixture.cs index b6d11a31e..e0084131b 100644 --- a/NzbDrone.Core.Test/Datastore/ObjectDatabaseFixture.cs +++ b/NzbDrone.Core.Test/Datastore/ObjectDatabaseFixture.cs @@ -114,7 +114,7 @@ namespace NzbDrone.Core.Test.Datastore childModel.Name = "B"; childModel.Tilte = "C"; - Subject.UpdateOnly(childModel, t=>t.Name); + Subject.UpdateFields(childModel, t=>t.Name); Db.All().Single().Address.Should().Be("Address"); Db.All().Single().Name.Should().Be("B"); diff --git a/NzbDrone.Core/Datastore/BasicRepository.cs b/NzbDrone.Core/Datastore/BasicRepository.cs index fb5c06970..d002587ef 100644 --- a/NzbDrone.Core/Datastore/BasicRepository.cs +++ b/NzbDrone.Core/Datastore/BasicRepository.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Data; using System.Linq; using System.Linq.Expressions; -using NzbDrone.Core.Tv; using ServiceStack.OrmLite; namespace NzbDrone.Core.Datastore @@ -12,6 +11,7 @@ namespace NzbDrone.Core.Datastore { IEnumerable All(); int Count(); + bool Any(Expression> predicate); TModel Get(int id); TModel Single(Expression> predicate); TModel SingleOrDefault(); @@ -28,36 +28,41 @@ namespace NzbDrone.Core.Datastore void Purge(); bool HasItems(); void DeleteMany(IEnumerable ids); - void UpdateOnly(TModel model, Expression> onlyFields); + void UpdateFields(TModel model, Expression> onlyFields); } public class BasicRepository : IBasicRepository where TModel : ModelBase, new() { + private readonly IDbConnection _database; + public BasicRepository(IDbConnection database) { - Database = database; + _database = database; } - public IDbConnection Database { get; private set; } - public IEnumerable All() { - return Database.Select(); + return _database.Select(); } public int Count() { - return (int)Database.Count(); + return (int)_database.Count(); + } + + public bool Any(Expression> predicate) + { + return _database.Exists(predicate); } public TModel Get(int id) { - return Database.GetById(id); + return _database.GetById(id); } public TModel Single(Expression> predicate) { - return Database.Select(predicate).Single(); + return _database.Select(predicate).Single(); } public TModel SingleOrDefault() @@ -72,73 +77,73 @@ namespace NzbDrone.Core.Datastore public TModel SingleOrDefault(Expression> predicate) { - return Database.Select(predicate).SingleOrDefault(); + return _database.Select(predicate).SingleOrDefault(); } public List Where(Expression> predicate) { - return Database.Select(predicate); + return _database.Select(predicate); } public TModel Insert(TModel model) { - Database.Insert(model); - model.Id = (int)Database.GetLastInsertId(); + _database.Insert(model); + model.Id = (int)_database.GetLastInsertId(); return model; } public TModel Update(TModel model) { - Database.Update(model); + _database.Update(model); return model; } public void Delete(TModel model) { - Database.Delete(model); + _database.Delete(model); } public void InsertMany(IList models) { - Database.InsertAll(models); + _database.InsertAll(models); } public void UpdateMany(IList models) { - Database.UpdateAll(models); + _database.UpdateAll(models); } public void DeleteMany(List models) { - Database.DeleteAll(models); + _database.DeleteAll(models); } public TModel Upsert(TModel model) { if (model.Id == 0) { - Database.Insert(model); - model.Id = (int)Database.GetLastInsertId(); + _database.Insert(model); + model.Id = (int)_database.GetLastInsertId(); return model; } - Database.Update(model); + _database.Update(model); return model; } public void Delete(int id) { - Database.DeleteById(id); + _database.DeleteById(id); } public void DeleteMany(IEnumerable ids) { - Database.DeleteByIds(ids); + _database.DeleteByIds(ids); } public void Purge() { - Database.DeleteAll(); + _database.DeleteAll(); } public bool HasItems() @@ -146,9 +151,14 @@ namespace NzbDrone.Core.Datastore return Count() > 0; } - public void UpdateOnly(TModel model, Expression> onlyFields) + public void UpdateFields(TModel model, Expression> onlyFields) { - Database.UpdateOnly(model, onlyFields); + if (model.Id == 0) + { + throw new InvalidOperationException("Attempted to updated model without ID"); + } + + _database.UpdateOnly(model, onlyFields, m => m.Id == model.Id); } } } diff --git a/NzbDrone.Core/Tv/EpisodeRepository.cs b/NzbDrone.Core/Tv/EpisodeRepository.cs index c282abf40f61387b04a98f88613612cbfe65e8ac..c8eb94002435f9d3ad424090c5a2de8f53e37971 100644 GIT binary patch delta 19 acmbPdw!mzIDi51mW@=7K@n%h)uY3SRi3ZdF delta 17 YcmZ2rHqUH>Di4c)UQXp^ZJw`u05|6b9{>OV diff --git a/NzbDrone.Core/Tv/SeriesRepository.cs b/NzbDrone.Core/Tv/SeriesRepository.cs index e66f032f4..a6386fbde 100644 --- a/NzbDrone.Core/Tv/SeriesRepository.cs +++ b/NzbDrone.Core/Tv/SeriesRepository.cs @@ -1,8 +1,6 @@ using System.Collections.Generic; using System.Data; -using System.Linq; using NzbDrone.Core.Datastore; -using ServiceStack.OrmLite; namespace NzbDrone.Core.Tv { @@ -24,27 +22,27 @@ namespace NzbDrone.Core.Tv public bool SeriesPathExists(string path) { - return Database.Exists("WHERE Path = {0}", path); + return Any(c => c.Path == path); } public List Search(string title) { - return Database.Select(s => s.Title.Contains(title)); + return Where(s => s.Title.Contains(title)); } public Series GetByTitle(string cleanTitle) { - return Database.Select(s => s.CleanTitle.Equals(cleanTitle)).SingleOrDefault(); + return SingleOrDefault(s => s.CleanTitle.Equals(cleanTitle)); } public Series FindByTvdbId(int tvdbId) { - return Database.Select(s => s.TvDbId.Equals(tvdbId)).SingleOrDefault(); + return SingleOrDefault(s => s.TvDbId.Equals(tvdbId)); } public void SetSeriesType(int seriesId, SeriesTypes seriesType) { - Database.UpdateOnly(new Series { SeriesType = seriesType }, s => s.SeriesType, s => s.Id == seriesId); + UpdateFields(new Series { Id = seriesId, SeriesType = seriesType }, s => s.SeriesType); } } } \ No newline at end of file