updated seriesrepo to use the new syntax.

pull/4/head
kay.one 12 years ago
parent 1a4f3fad25
commit b04fb32f77

@ -114,7 +114,7 @@ namespace NzbDrone.Core.Test.Datastore
childModel.Name = "B"; childModel.Name = "B";
childModel.Tilte = "C"; childModel.Tilte = "C";
Subject.UpdateOnly(childModel, t=>t.Name); Subject.UpdateFields(childModel, t=>t.Name);
Db.All<BaiscType>().Single().Address.Should().Be("Address"); Db.All<BaiscType>().Single().Address.Should().Be("Address");
Db.All<BaiscType>().Single().Name.Should().Be("B"); Db.All<BaiscType>().Single().Name.Should().Be("B");

@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Data; using System.Data;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using NzbDrone.Core.Tv;
using ServiceStack.OrmLite; using ServiceStack.OrmLite;
namespace NzbDrone.Core.Datastore namespace NzbDrone.Core.Datastore
@ -12,6 +11,7 @@ namespace NzbDrone.Core.Datastore
{ {
IEnumerable<TModel> All(); IEnumerable<TModel> All();
int Count(); int Count();
bool Any(Expression<Func<TModel, bool>> predicate);
TModel Get(int id); TModel Get(int id);
TModel Single(Expression<Func<TModel, bool>> predicate); TModel Single(Expression<Func<TModel, bool>> predicate);
TModel SingleOrDefault(); TModel SingleOrDefault();
@ -28,36 +28,41 @@ namespace NzbDrone.Core.Datastore
void Purge(); void Purge();
bool HasItems(); bool HasItems();
void DeleteMany(IEnumerable<int> ids); void DeleteMany(IEnumerable<int> ids);
void UpdateOnly<TKey>(TModel model, Expression<Func<TModel, TKey>> onlyFields); void UpdateFields<TKey>(TModel model, Expression<Func<TModel, TKey>> onlyFields);
} }
public class BasicRepository<TModel> : IBasicRepository<TModel> where TModel : ModelBase, new() public class BasicRepository<TModel> : IBasicRepository<TModel> where TModel : ModelBase, new()
{ {
private readonly IDbConnection _database;
public BasicRepository(IDbConnection database) public BasicRepository(IDbConnection database)
{ {
Database = database; _database = database;
} }
public IDbConnection Database { get; private set; }
public IEnumerable<TModel> All() public IEnumerable<TModel> All()
{ {
return Database.Select<TModel>(); return _database.Select<TModel>();
} }
public int Count() public int Count()
{ {
return (int)Database.Count<TModel>(); return (int)_database.Count<TModel>();
}
public bool Any(Expression<Func<TModel, bool>> predicate)
{
return _database.Exists<TModel>(predicate);
} }
public TModel Get(int id) public TModel Get(int id)
{ {
return Database.GetById<TModel>(id); return _database.GetById<TModel>(id);
} }
public TModel Single(Expression<Func<TModel, bool>> predicate) public TModel Single(Expression<Func<TModel, bool>> predicate)
{ {
return Database.Select(predicate).Single(); return _database.Select(predicate).Single();
} }
public TModel SingleOrDefault() public TModel SingleOrDefault()
@ -72,73 +77,73 @@ namespace NzbDrone.Core.Datastore
public TModel SingleOrDefault(Expression<Func<TModel, bool>> predicate) public TModel SingleOrDefault(Expression<Func<TModel, bool>> predicate)
{ {
return Database.Select(predicate).SingleOrDefault(); return _database.Select(predicate).SingleOrDefault();
} }
public List<TModel> Where(Expression<Func<TModel, bool>> predicate) public List<TModel> Where(Expression<Func<TModel, bool>> predicate)
{ {
return Database.Select(predicate); return _database.Select(predicate);
} }
public TModel Insert(TModel model) public TModel Insert(TModel model)
{ {
Database.Insert(model); _database.Insert(model);
model.Id = (int)Database.GetLastInsertId(); model.Id = (int)_database.GetLastInsertId();
return model; return model;
} }
public TModel Update(TModel model) public TModel Update(TModel model)
{ {
Database.Update(model); _database.Update(model);
return model; return model;
} }
public void Delete(TModel model) public void Delete(TModel model)
{ {
Database.Delete(model); _database.Delete(model);
} }
public void InsertMany(IList<TModel> models) public void InsertMany(IList<TModel> models)
{ {
Database.InsertAll(models); _database.InsertAll(models);
} }
public void UpdateMany(IList<TModel> models) public void UpdateMany(IList<TModel> models)
{ {
Database.UpdateAll(models); _database.UpdateAll(models);
} }
public void DeleteMany(List<TModel> models) public void DeleteMany(List<TModel> models)
{ {
Database.DeleteAll(models); _database.DeleteAll(models);
} }
public TModel Upsert(TModel model) public TModel Upsert(TModel model)
{ {
if (model.Id == 0) if (model.Id == 0)
{ {
Database.Insert(model); _database.Insert(model);
model.Id = (int)Database.GetLastInsertId(); model.Id = (int)_database.GetLastInsertId();
return model; return model;
} }
Database.Update(model); _database.Update(model);
return model; return model;
} }
public void Delete(int id) public void Delete(int id)
{ {
Database.DeleteById<TModel>(id); _database.DeleteById<TModel>(id);
} }
public void DeleteMany(IEnumerable<int> ids) public void DeleteMany(IEnumerable<int> ids)
{ {
Database.DeleteByIds<TModel>(ids); _database.DeleteByIds<TModel>(ids);
} }
public void Purge() public void Purge()
{ {
Database.DeleteAll<TModel>(); _database.DeleteAll<TModel>();
} }
public bool HasItems() public bool HasItems()
@ -146,9 +151,14 @@ namespace NzbDrone.Core.Datastore
return Count() > 0; return Count() > 0;
} }
public void UpdateOnly<TKey>(TModel model, Expression<Func<TModel, TKey>> onlyFields) public void UpdateFields<TKey>(TModel model, Expression<Func<TModel, TKey>> 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);
} }
} }
} }

@ -1,8 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
using System.Linq;
using NzbDrone.Core.Datastore; using NzbDrone.Core.Datastore;
using ServiceStack.OrmLite;
namespace NzbDrone.Core.Tv namespace NzbDrone.Core.Tv
{ {
@ -24,27 +22,27 @@ namespace NzbDrone.Core.Tv
public bool SeriesPathExists(string path) public bool SeriesPathExists(string path)
{ {
return Database.Exists<Series>("WHERE Path = {0}", path); return Any(c => c.Path == path);
} }
public List<Series> Search(string title) public List<Series> Search(string title)
{ {
return Database.Select<Series>(s => s.Title.Contains(title)); return Where(s => s.Title.Contains(title));
} }
public Series GetByTitle(string cleanTitle) public Series GetByTitle(string cleanTitle)
{ {
return Database.Select<Series>(s => s.CleanTitle.Equals(cleanTitle)).SingleOrDefault(); return SingleOrDefault(s => s.CleanTitle.Equals(cleanTitle));
} }
public Series FindByTvdbId(int tvdbId) public Series FindByTvdbId(int tvdbId)
{ {
return Database.Select<Series>(s => s.TvDbId.Equals(tvdbId)).SingleOrDefault(); return SingleOrDefault(s => s.TvDbId.Equals(tvdbId));
} }
public void SetSeriesType(int seriesId, SeriesTypes seriesType) 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);
} }
} }
} }
Loading…
Cancel
Save