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.
Radarr/NzbDrone.Core/Datastore/BasicRepository.cs

204 lines
5.3 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;
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();
}
public class BasicRepository<TModel> : IBasicRepository<TModel> where TModel : ModelBase, new()
{
12 years ago
private readonly IDatabase _database;
private readonly IMessageAggregator _messageAggregator;
12 years ago
//TODO: add assertion to make sure model properly mapped
12 years ago
private IDataMapper DataMapper
{
get { return _database.DataMapper; }
}
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)
{
12 years ago
return DataMapper.Query<TModel>().Single(c => c.Id == id);
}
public IEnumerable<TModel> Get(IEnumerable<int> ids)
{
var idList = ids.ToList();
var result = Query.Where(String.Format("Id IN ({0})", String.Join(",", idList))).ToList();
var resultCount = result.Count;
if (resultCount != idList.Count || result.Select(r => r.Id).Distinct().Count() != resultCount)
throw new InvalidOperationException("Unexpected result from query");
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)
{
throw new InvalidOperationException("Can't insert model with existing ID");
}
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
}
protected virtual void PublishModelEvent(TModel model, RepositoryAction action)
{
_messageAggregator.PublishEvent(new ModelEvent<TModel>(model, action));
}
}
}