1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-09 12:32:31 +01:00
Radarr/NzbDrone.Core/Datastore/BasicRepository.cs

194 lines
5.1 KiB
C#
Raw Normal View History

2013-03-24 05:16:00 +01:00
using System;
using System.Collections.Generic;
2013-02-05 05:07:07 +01:00
using System.Linq;
2013-03-24 05:16:00 +01:00
using System.Linq.Expressions;
2013-03-25 04:51:32 +01:00
using Marr.Data;
using Marr.Data.QGen;
2013-05-05 23:24:33 +02:00
using NzbDrone.Common.Messaging;
using NzbDrone.Core.Datastore.Events;
2013-03-25 04:51:32 +01:00
2013-02-05 05:07:07 +01:00
namespace NzbDrone.Core.Datastore
{
2013-03-02 19:25:39 +01:00
public interface IBasicRepository<TModel> where TModel : ModelBase, new()
2013-02-05 05:07:07 +01:00
{
2013-02-19 07:56:02 +01:00
IEnumerable<TModel> All();
2013-02-23 20:38:25 +01:00
int Count();
TModel Get(int id);
2013-04-18 02:33:38 +02:00
IEnumerable<TModel> Get(IEnumerable<int> ids);
TModel SingleOrDefault();
2013-02-19 07:56:02 +01:00
TModel Insert(TModel model);
TModel Update(TModel model);
2013-03-24 05:16:00 +01:00
TModel Upsert(TModel model);
void Delete(int id);
void Delete(TModel model);
2013-03-24 05:16:00 +01:00
void InsertMany(IList<TModel> model);
void UpdateMany(IList<TModel> model);
void DeleteMany(List<TModel> model);
2013-02-23 20:38:25 +01:00
void Purge();
2013-03-02 19:25:39 +01:00
bool HasItems();
2013-03-24 05:16:00 +01:00
void DeleteMany(IEnumerable<int> ids);
void SetFields(TModel model, params Expression<Func<TModel, object>>[] properties);
2013-04-25 06:27:49 +02:00
TModel Single();
2013-02-05 05:07:07 +01:00
}
public class BasicRepository<TModel> : IBasicRepository<TModel> where TModel : ModelBase, new()
2013-02-05 05:07:07 +01:00
{
2013-05-05 23:24:33 +02:00
private readonly IMessageAggregator _messageAggregator;
2013-04-25 06:27:49 +02:00
//TODO: add assertion to make sure model properly mapped
2013-03-25 04:51:32 +01:00
private readonly IDataMapper _dataMapper;
2013-05-05 23:24:33 +02:00
public BasicRepository(IDatabase database, IMessageAggregator messageAggregator)
2013-02-05 05:07:07 +01:00
{
2013-05-05 23:24:33 +02:00
_messageAggregator = messageAggregator;
2013-03-25 04:51:32 +01:00
_dataMapper = database.DataMapper;
2013-02-05 05:07:07 +01:00
}
2013-03-27 04:44:52 +01:00
protected QueryBuilder<TModel> Query
2013-02-05 05:07:07 +01:00
{
2013-03-27 04:44:52 +01:00
get { return _dataMapper.Query<TModel>(); }
}
protected void Delete(Expression<Func<TModel, bool>> filter)
{
_dataMapper.Delete(filter);
2013-02-05 05:07:07 +01:00
}
2013-03-25 04:51:32 +01:00
public IEnumerable<TModel> All()
2013-02-23 20:38:25 +01:00
{
2013-03-25 04:51:32 +01:00
return _dataMapper.Query<TModel>().ToList();
}
2013-03-25 04:51:32 +01:00
public int Count()
{
2013-05-11 08:16:10 +02:00
return _dataMapper.Query<TModel>().GetRowCount();
2013-02-23 20:38:25 +01:00
}
public TModel Get(int id)
2013-02-05 05:07:07 +01:00
{
2013-03-25 08:36:22 +01:00
return _dataMapper.Query<TModel>().Single(c => c.Id == id);
2013-02-05 05:07:07 +01:00
}
2013-04-18 02:33:38 +02:00
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()
2013-04-25 06:27:49 +02:00
{
return All().SingleOrDefault();
}
public TModel Single()
{
2013-03-24 05:16:00 +01:00
return All().Single();
}
2013-02-19 07:56:02 +01:00
public TModel Insert(TModel model)
2013-02-05 05:07:07 +01:00
{
2013-03-24 20:56:51 +01:00
if (model.Id != 0)
{
throw new InvalidOperationException("Can't insert model with existing ID");
}
2013-05-05 23:24:33 +02:00
_dataMapper.Insert(model);
_messageAggregator.PublishEvent(new ModelEvent<TModel>(model, ModelEvent<TModel>.RepositoryAction.Created));
2013-03-24 05:16:00 +01:00
return model;
2013-02-05 05:07:07 +01:00
}
public TModel Update(TModel model)
{
2013-03-24 20:56:51 +01:00
if (model.Id == 0)
{
throw new InvalidOperationException("Can't update model with ID 0");
}
2013-03-25 04:51:32 +01:00
_dataMapper.Update(model, c => c.Id == model.Id);
2013-03-24 05:16:00 +01:00
return model;
}
public void Delete(TModel model)
{
2013-03-25 04:51:32 +01:00
_dataMapper.Delete<TModel>(c => c.Id == model.Id);
}
2013-03-24 05:16:00 +01:00
public void InsertMany(IList<TModel> models)
{
2013-03-25 04:51:32 +01:00
foreach (var model in models)
{
Insert(model);
}
}
2013-03-24 05:16:00 +01:00
public void UpdateMany(IList<TModel> models)
{
2013-03-25 04:51:32 +01:00
foreach (var model in models)
{
Update(model);
}
}
2013-03-24 05:16:00 +01:00
public void DeleteMany(List<TModel> models)
{
2013-03-25 04:51:32 +01:00
models.ForEach(Delete);
}
2013-03-24 05:16:00 +01:00
public TModel Upsert(TModel model)
{
if (model.Id == 0)
2013-02-23 20:38:25 +01:00
{
2013-03-25 04:51:32 +01:00
Insert(model);
2013-03-24 05:16:00 +01:00
return model;
2013-02-23 20:38:25 +01:00
}
2013-03-25 04:51:32 +01:00
Update(model);
2013-03-24 05:16:00 +01:00
return model;
}
public void Delete(int id)
2013-02-05 05:07:07 +01:00
{
2013-03-25 04:51:32 +01:00
_dataMapper.Delete<TModel>(c => c.Id == id);
2013-02-05 05:07:07 +01:00
}
2013-02-23 20:38:25 +01:00
public void DeleteMany(IEnumerable<int> ids)
{
2013-03-25 04:51:32 +01:00
ids.ToList().ForEach(Delete);
2013-02-23 20:38:25 +01:00
}
public void Purge()
{
2013-03-25 04:51:32 +01:00
_dataMapper.Delete<TModel>(c => c.Id > -1);
2013-02-23 20:38:25 +01:00
}
2013-03-02 19:25:39 +01:00
public bool HasItems()
{
2013-03-24 05:16:00 +01:00
return Count() > 0;
}
public void SetFields(TModel model, params Expression<Func<TModel, object>>[] properties)
2013-03-24 05:16:00 +01:00
{
if (model.Id == 0)
{
throw new InvalidOperationException("Attempted to updated model without ID");
}
_dataMapper.Update<TModel>()
.Where(c => c.Id == model.Id)
.ColumnsIncluding(properties)
.Entity(model)
.Execute();
2013-03-02 19:25:39 +01:00
}
2013-05-11 01:53:50 +02:00
2013-02-05 05:07:07 +01:00
}
}