updated seriesrepo to use the new syntax.

This commit is contained in:
kay.one 2013-03-23 21:25:16 -07:00
parent 1a4f3fad25
commit b04fb32f77
4 changed files with 42 additions and 34 deletions

View File

@ -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<BaiscType>().Single().Address.Should().Be("Address");
Db.All<BaiscType>().Single().Name.Should().Be("B");

View File

@ -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<TModel> All();
int Count();
bool Any(Expression<Func<TModel, bool>> predicate);
TModel Get(int id);
TModel Single(Expression<Func<TModel, bool>> predicate);
TModel SingleOrDefault();
@ -28,36 +28,41 @@ namespace NzbDrone.Core.Datastore
void Purge();
bool HasItems();
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()
{
private readonly IDbConnection _database;
public BasicRepository(IDbConnection database)
{
Database = database;
_database = database;
}
public IDbConnection Database { get; private set; }
public IEnumerable<TModel> All()
{
return Database.Select<TModel>();
return _database.Select<TModel>();
}
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)
{
return Database.GetById<TModel>(id);
return _database.GetById<TModel>(id);
}
public TModel Single(Expression<Func<TModel, bool>> 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<Func<TModel, bool>> predicate)
{
return Database.Select(predicate).SingleOrDefault();
return _database.Select(predicate).SingleOrDefault();
}
public List<TModel> Where(Expression<Func<TModel, bool>> 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<TModel> models)
{
Database.InsertAll(models);
_database.InsertAll(models);
}
public void UpdateMany(IList<TModel> models)
{
Database.UpdateAll(models);
_database.UpdateAll(models);
}
public void DeleteMany(List<TModel> 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<TModel>(id);
_database.DeleteById<TModel>(id);
}
public void DeleteMany(IEnumerable<int> ids)
{
Database.DeleteByIds<TModel>(ids);
_database.DeleteByIds<TModel>(ids);
}
public void Purge()
{
Database.DeleteAll<TModel>();
_database.DeleteAll<TModel>();
}
public bool HasItems()
@ -146,9 +151,14 @@ namespace NzbDrone.Core.Datastore
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);
}
}
}

View File

@ -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<Series>("WHERE Path = {0}", path);
return Any(c => c.Path == path);
}
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)
{
return Database.Select<Series>(s => s.CleanTitle.Equals(cleanTitle)).SingleOrDefault();
return SingleOrDefault(s => s.CleanTitle.Equals(cleanTitle));
}
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)
{
Database.UpdateOnly(new Series { SeriesType = seriesType }, s => s.SeriesType, s => s.Id == seriesId);
UpdateFields(new Series { Id = seriesId, SeriesType = seriesType }, s => s.SeriesType);
}
}
}