You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Lidarr/NzbDrone.Core/Datastore/BasicRepository.cs

245 lines
6.4 KiB

12 years ago
using System;
using System.Collections.Generic;
using System.Linq;
12 years ago
using System.Linq.Expressions;
using Marr.Data;
using Marr.Data.QGen;
using NzbDrone.Common.Messaging;
using NzbDrone.Core.Datastore.Events;
using NzbDrone.Common;
namespace NzbDrone.Core.Datastore
{
12 years ago
public interface IBasicRepository<TModel> where TModel : ModelBase, new()
{
12 years ago
IEnumerable<TModel> All();
int Count();
TModel Get(int id);
IEnumerable<TModel> Get(IEnumerable<int> ids);
TModel SingleOrDefault();
12 years ago
TModel Insert(TModel model);
TModel Update(TModel model);
12 years ago
TModel Upsert(TModel model);
void Delete(int id);
void Delete(TModel model);
12 years ago
void InsertMany(IList<TModel> model);
void UpdateMany(IList<TModel> model);
void DeleteMany(List<TModel> model);
void Purge();
12 years ago
bool HasItems();
12 years ago
void DeleteMany(IEnumerable<int> ids);
void SetFields(TModel model, params Expression<Func<TModel, object>>[] properties);
12 years ago
TModel Single();
12 years ago
PagingSpec<TModel> GetPaged(PagingSpec<TModel> pagingSpec);
}
public class BasicRepository<TModel> : IBasicRepository<TModel> where TModel : ModelBase, new()
{
12 years ago
private readonly IDatabase _database;
private readonly IMessageAggregator _messageAggregator;
12 years ago
12 years ago
private IDataMapper DataMapper
{
get { return _database.GetDataMapper(); }
12 years ago
}
public BasicRepository(IDatabase database, IMessageAggregator messageAggregator)
{
12 years ago
_database = database;
_messageAggregator = messageAggregator;
}
protected QueryBuilder<TModel> Query
{
12 years ago
get { return DataMapper.Query<TModel>(); }
}
protected void Delete(Expression<Func<TModel, bool>> filter)
{
12 years ago
DataMapper.Delete(filter);
}
public IEnumerable<TModel> All()
{
12 years ago
return DataMapper.Query<TModel>().ToList();
}
public int Count()
{
12 years ago
return DataMapper.Query<TModel>().GetRowCount();
}
public TModel Get(int id)
{
var model = DataMapper.Query<TModel>().SingleOrDefault(c => c.Id == id);
if (model == null)
{
throw new ModelNotFoundException(typeof(TModel), id);
}
return model;
}
public IEnumerable<TModel> Get(IEnumerable<int> ids)
{
var idList = ids.ToList();
var query = String.Format("Id IN ({0})", String.Join(",", idList));
var result = Query.Where(query).ToList();
if (result.Count != idList.Count())
{
throw new ApplicationException("Expected query to return {0} rows but returned {1}".Inject(idList.Count(), result.Count));
}
return result;
}
public TModel SingleOrDefault()
12 years ago
{
return All().SingleOrDefault();
}
public TModel Single()
{
12 years ago
return All().Single();
}
12 years ago
public TModel Insert(TModel model)
{
12 years ago
if (model.Id != 0)
{
12 years ago
throw new InvalidOperationException("Can't insert model with existing ID " + model.Id);
12 years ago
}
12 years ago
DataMapper.Insert(model);
PublishModelEvent(model, RepositoryAction.Created);
12 years ago
return model;
}
public TModel Update(TModel model)
{
12 years ago
if (model.Id == 0)
{
throw new InvalidOperationException("Can't update model with ID 0");
}
12 years ago
DataMapper.Update(model, c => c.Id == model.Id);
12 years ago
return model;
}
public void Delete(TModel model)
{
12 years ago
DataMapper.Delete<TModel>(c => c.Id == model.Id);
}
12 years ago
public void InsertMany(IList<TModel> models)
{
foreach (var model in models)
{
Insert(model);
}
}
12 years ago
public void UpdateMany(IList<TModel> models)
{
foreach (var model in models)
{
Update(model);
}
}
12 years ago
public void DeleteMany(List<TModel> models)
{
models.ForEach(Delete);
}
12 years ago
public TModel Upsert(TModel model)
{
if (model.Id == 0)
{
Insert(model);
12 years ago
return model;
}
Update(model);
12 years ago
return model;
}
public void Delete(int id)
{
12 years ago
DataMapper.Delete<TModel>(c => c.Id == id);
}
public void DeleteMany(IEnumerable<int> ids)
{
ids.ToList().ForEach(Delete);
}
public void Purge()
{
12 years ago
DataMapper.Delete<TModel>(c => c.Id > -1);
}
12 years ago
public bool HasItems()
{
12 years ago
return Count() > 0;
}
public void SetFields(TModel model, params Expression<Func<TModel, object>>[] properties)
12 years ago
{
if (model.Id == 0)
{
throw new InvalidOperationException("Attempted to updated model without ID");
}
12 years ago
DataMapper.Update<TModel>()
.Where(c => c.Id == model.Id)
.ColumnsIncluding(properties)
.Entity(model)
.Execute();
12 years ago
}
12 years ago
public virtual PagingSpec<TModel> GetPaged(PagingSpec<TModel> pagingSpec)
{
var pagingQuery = Query.OrderBy(pagingSpec.OrderByClause(), pagingSpec.ToSortDirection())
.Skip(pagingSpec.PagingOffset())
.Take(pagingSpec.PageSize);
pagingSpec.Records = pagingQuery.ToList();
//TODO: Use the same query for count and records
pagingSpec.TotalRecords = Count();
return pagingSpec;
}
private void PublishModelEvent(TModel model, RepositoryAction action)
{
if (PublishModelEvents)
{
_messageAggregator.PublishEvent(new ModelEvent<TModel>(model, action));
}
}
protected virtual void OnModelChanged(IEnumerable<TModel> models)
{
}
protected virtual void OnModelDeleted(IEnumerable<TModel> models)
{
}
protected virtual bool PublishModelEvents
{
get { return false; }
}
}
}