using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Linq.Expressions; using Marr.Data; using Marr.Data.QGen; using NzbDrone.Core.Tv; namespace NzbDrone.Core.Datastore { public interface IBasicRepository where TModel : ModelBase, new() { IEnumerable All(); int Count(); TModel Get(int id); TModel SingleOrDefault(); TModel Insert(TModel model); TModel Update(TModel model); TModel Upsert(TModel model); void Delete(int id); void Delete(TModel model); void InsertMany(IList model); void UpdateMany(IList model); void DeleteMany(List model); void Purge(); bool HasItems(); void DeleteMany(IEnumerable ids); void SetFields(TModel model, params Expression>[] properties); } public class BasicRepository : IBasicRepository where TModel : ModelBase, new() { private readonly IDataMapper _dataMapper; public BasicRepository(IDatabase database) { _dataMapper = database.DataMapper; } protected QueryBuilder Query { get { return _dataMapper.Query(); } } protected void Delete(Expression> filter) { _dataMapper.Delete(filter); } public IEnumerable All() { return _dataMapper.Query().ToList(); } public int Count() { return _dataMapper.Query().Count(); } public TModel Get(int id) { return _dataMapper.Query().Single(c => c.Id == id); } public TModel SingleOrDefault() { return All().Single(); } public TModel Insert(TModel model) { if (model.Id != 0) { throw new InvalidOperationException("Can't insert model with existing ID"); } var id = _dataMapper.Insert(model); return model; } public TModel Update(TModel model) { if (model.Id == 0) { throw new InvalidOperationException("Can't update model with ID 0"); } _dataMapper.Update(model, c => c.Id == model.Id); return model; } public void Delete(TModel model) { _dataMapper.Delete(c => c.Id == model.Id); } public void InsertMany(IList models) { foreach (var model in models) { Insert(model); } } public void UpdateMany(IList models) { foreach (var model in models) { Update(model); } } public void DeleteMany(List models) { models.ForEach(Delete); } public TModel Upsert(TModel model) { if (model.Id == 0) { Insert(model); return model; } Update(model); return model; } public void Delete(int id) { _dataMapper.Delete(c => c.Id == id); } public void DeleteMany(IEnumerable ids) { ids.ToList().ForEach(Delete); } public void Purge() { _dataMapper.Delete(c => c.Id > -1); } public bool HasItems() { return Count() > 0; } public void SetFields(TModel model, params Expression>[] properties) { if (model.Id == 0) { throw new InvalidOperationException("Attempted to updated model without ID"); } _dataMapper.Update() .Where(c => c.Id == model.Id) .ColumnsIncluding(properties) .Entity(model) .Execute(); } } }