All Sln changes

This commit is contained in:
tidusjar 2016-12-19 20:14:31 +00:00
commit 796f0fc188
615 changed files with 68 additions and 747 deletions

View file

@ -0,0 +1,346 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: BaseGenericRepository.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Dapper;
using Dapper.Contrib.Extensions;
using Mono.Data.Sqlite;
using NLog;
using Ombi.Helpers;
namespace Ombi.Store.Repository
{
public abstract class BaseGenericRepository<T> where T : class
{
private const string CorruptMessage =
"The database is corrupt, this could be due to the application exiting unexpectedly. See here to fix it: http://www.dosomethinghere.com/2013/02/20/fixing-the-sqlite-error-the-database-disk-image-is-malformed/";
protected BaseGenericRepository(ISqliteConfiguration config, ICacheProvider cache)
{
Config = config;
Cache = cache;
}
protected ICacheProvider Cache { get; }
protected ISqliteConfiguration Config { get; }
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
public abstract T Get(string id);
public abstract Task<T> GetAsync(int id);
public abstract T Get(int id);
public abstract Task<T> GetAsync(string id);
private IDbConnection Connection => Config.DbConnection();
public IEnumerable<T> Custom(Func<IDbConnection, IEnumerable<T>> func)
{
using (var cnn = Connection)
{
return func(cnn);
}
}
public async Task<IEnumerable<T>> CustomAsync(Func<IDbConnection, Task<IEnumerable<T>>> func)
{
using (var cnn = Connection)
{
return await func(cnn);
}
}
public long Insert(T entity)
{
try
{
ResetCache();
using (var cnn = Config.DbConnection())
{
cnn.Open();
return cnn.Insert(entity);
}
}
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
Log.Fatal(CorruptMessage);
throw;
}
}
public void Delete(T entity)
{
try
{
ResetCache();
using (var db = Config.DbConnection())
{
db.Open();
db.Delete(entity);
}
}
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
Log.Fatal(CorruptMessage);
throw;
}
}
public async Task DeleteAsync(T entity)
{
try
{
ResetCache();
using (var db = Config.DbConnection())
{
db.Open();
await db.DeleteAsync(entity).ConfigureAwait(false);
}
}
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
Log.Fatal(CorruptMessage);
throw;
}
}
public bool Update(T entity)
{
try
{
ResetCache();
using (var db = Config.DbConnection())
{
db.Open();
return db.Update(entity);
}
}
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
Log.Fatal(CorruptMessage);
throw;
}
}
public async Task<bool> UpdateAsync(T entity)
{
try
{
ResetCache();
using (var db = Config.DbConnection())
{
db.Open();
return await db.UpdateAsync(entity).ConfigureAwait(false);
}
}
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
Log.Fatal(CorruptMessage);
throw;
}
}
public bool UpdateAll(IEnumerable<T> entity)
{
try
{
ResetCache();
var result = new HashSet<bool>();
using (var db = Config.DbConnection())
{
db.Open();
foreach (var e in entity)
{
result.Add(db.Update(e));
}
}
return result.All(x => true);
}
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
Log.Fatal(CorruptMessage);
throw;
}
}
public async Task<bool> UpdateAllAsync(IEnumerable<T> entity)
{
try
{
ResetCache();
var tasks = new List<Task<bool>>();
using (var db = Config.DbConnection())
{
db.Open();
foreach (var e in entity)
{
tasks.Add(db.UpdateAsync(e));
}
}
var result = await Task.WhenAll(tasks).ConfigureAwait(false);
return result.All(x => true);
}
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
Log.Fatal(CorruptMessage);
throw;
}
}
public async Task<int> InsertAsync(T entity)
{
try
{
ResetCache();
using (var cnn = Config.DbConnection())
{
cnn.Open();
return await cnn.InsertAsync(entity).ConfigureAwait(false);
}
}
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
Log.Fatal(CorruptMessage);
throw;
}
}
private void ResetCache()
{
Cache.Remove("Get");
Cache.Remove("GetAll");
}
public IEnumerable<T> GetAll()
{
try
{
using (var db = Config.DbConnection())
{
db.Open();
var result = db.GetAll<T>();
return result;
}
}
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
Log.Fatal(CorruptMessage);
throw;
}
}
public async Task<IEnumerable<T>> GetAllAsync()
{
try
{
using (var db = Config.DbConnection())
{
db.Open();
var result = await db.GetAllAsync<T>().ConfigureAwait(false);
return result;
}
}
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
Log.Fatal(CorruptMessage);
throw;
}
}
public bool BatchInsert(IEnumerable<T> entities, string tableName, params string[] values)
{
// If we have nothing to update, then it didn't fail...
var enumerable = entities as T[] ?? entities.ToArray();
if (!enumerable.Any())
{
return true;
}
try
{
ResetCache();
using (var db = Config.DbConnection())
{
db.Open();
using (var tran = db.BeginTransaction())
{
var result = enumerable.Sum(e => db.Insert(e));
if (result != 0)
{
tran.Commit();
return true;
}
tran.Rollback();
return false;
}
}
}
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
Log.Fatal(CorruptMessage);
throw;
}
}
public void DeleteAll(string tableName)
{
try
{
ResetCache();
using (var db = Config.DbConnection())
{
db.Open();
db.Execute($"delete from {tableName}");
}
}
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
Log.Fatal(CorruptMessage);
throw;
}
}
public async Task DeleteAllAsync(string tableName)
{
try
{
ResetCache();
using (var db = Config.DbConnection())
{
db.Open();
await db.ExecuteAsync($"delete from {tableName}");
}
}
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
Log.Fatal(CorruptMessage);
throw;
}
}
}
}

View file

@ -0,0 +1,89 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: GenericRepository.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using System;
using System.Threading.Tasks;
using Dapper.Contrib.Extensions;
using Ombi.Helpers;
namespace Ombi.Store.Repository
{
public class GenericRepository<T> : BaseGenericRepository<T>, IRepository<T> where T : Entity
{
public GenericRepository(ISqliteConfiguration config, ICacheProvider cache) : base(config, cache)
{
}
public override T Get(string id)
{
throw new NotSupportedException("Get(string) is not supported. Use Get(int)");
}
public override Task<T> GetAsync(string id)
{
throw new NotSupportedException("GetAsync(string) is not supported. Use GetAsync(int)");
}
public override T Get(int id)
{
var key = "Get" + id;
var item = Cache.GetOrSet(
key,
() =>
{
using (var db = Config.DbConnection())
{
db.Open();
return db.Get<T>(id);
}
});
return item;
}
public override async Task<T> GetAsync(int id)
{
var key = "Get" + id;
var item = await Cache.GetOrSetAsync(
key,
async () =>
{
using (var db = Config.DbConnection())
{
db.Open();
return await db.GetAsync<T>(id).ConfigureAwait(false);
}
});
return item;
}
}
}

View file

@ -0,0 +1,91 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: IRepository.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
namespace Ombi.Store.Repository
{
public interface IRepository<T>
{
/// <summary>
/// Inserts the specified entity.
/// </summary>
/// <param name="entity">The entity.</param>
long Insert(T entity);
Task<int> InsertAsync(T entity);
/// <summary>
/// Gets all.
/// </summary>
/// <returns></returns>
IEnumerable<T> GetAll();
Task<IEnumerable<T>> GetAllAsync();
/// <summary>
/// Gets the specified identifier.
/// </summary>
/// <param name="id">The identifier.</param>
/// <returns></returns>
T Get(string id);
Task<T> GetAsync(string id);
T Get(int id);
Task<T> GetAsync(int id);
/// <summary>
/// Deletes the specified entity.
/// </summary>
/// <param name="entity">The entity.</param>
void Delete(T entity);
Task DeleteAsync(T entity);
/// <summary>
/// Updates the specified entity.
/// </summary>
/// <param name="entity">The entity.</param>
/// <returns></returns>
bool Update(T entity);
Task<bool> UpdateAsync(T entity);
/// <summary>
/// Updates all.
/// </summary>
/// <param name="entity">The entity.</param>
/// <returns></returns>
bool UpdateAll(IEnumerable<T> entity);
Task<bool> UpdateAllAsync(IEnumerable<T> entity);
bool BatchInsert(IEnumerable<T> entities, string tableName, params string[] values);
IEnumerable<T> Custom(Func<IDbConnection, IEnumerable<T>> func);
Task<IEnumerable<T>> CustomAsync(Func<IDbConnection, Task<IEnumerable<T>>> func);
void DeleteAll(string tableName);
Task DeleteAllAsync(string tableName);
}
}

View file

@ -0,0 +1,77 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: ISettingsRepository.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using System.Collections.Generic;
using System.Threading.Tasks;
using Ombi.Store.Models;
namespace Ombi.Store.Repository
{
public interface IRequestRepository
{
/// <summary>
/// Inserts the specified entity.
/// </summary>
/// <param name="entity">The entity.</param>
long Insert(RequestBlobs entity);
Task<int> InsertAsync(RequestBlobs entity);
/// <summary>
/// Gets all.
/// </summary>
/// <returns></returns>
IEnumerable<RequestBlobs> GetAll();
Task<IEnumerable<RequestBlobs>> GetAllAsync();
RequestBlobs Get(int id);
Task<RequestBlobs> GetAsync(int id);
/// <summary>
/// Deletes the specified entity.
/// </summary>
/// <param name="entity">The entity.</param>
/// <returns></returns>
bool Delete(RequestBlobs entity);
Task<bool> DeleteAsync(RequestBlobs entity);
bool DeleteAll(IEnumerable<RequestBlobs> entity);
Task<bool> DeleteAllAsync(IEnumerable<RequestBlobs> entity);
/// <summary>
/// Updates the specified entity.
/// </summary>
/// <param name="entity">The entity.</param>
/// <returns></returns>
bool Update(RequestBlobs entity);
Task<bool> UpdateAsync(RequestBlobs entity);
bool UpdateAll(IEnumerable<RequestBlobs> entity);
Task<bool> UpdateAllAsync(IEnumerable<RequestBlobs> entity);
}
}

View file

@ -0,0 +1,76 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: ISettingsRepository.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using System.Collections.Generic;
using System.Threading.Tasks;
using Ombi.Store.Models;
namespace Ombi.Store.Repository
{
public interface ISettingsRepository
{
/// <summary>
/// Inserts the specified entity.
/// </summary>
/// <param name="entity">The entity.</param>
long Insert(GlobalSettings entity);
Task<int> InsertAsync(GlobalSettings entity);
/// <summary>
/// Gets all.
/// </summary>
/// <returns></returns>
IEnumerable<GlobalSettings> GetAll();
Task<IEnumerable<GlobalSettings>> GetAllAsync();
/// <summary>
/// Gets the specified identifier.
/// </summary>
/// <param name="settingsName">Name of the settings.</param>
/// <returns></returns>
GlobalSettings Get(string settingsName);
Task<GlobalSettings> GetAsync(string settingsName);
/// <summary>
/// Deletes the specified entity.
/// </summary>
/// <param name="entity">The entity.</param>
/// <returns></returns>
Task<bool> DeleteAsync(GlobalSettings entity);
bool Delete(GlobalSettings entity);
/// <summary>
/// Updates the specified entity.
/// </summary>
/// <param name="entity">The entity.</param>
/// <returns></returns>
Task<bool> UpdateAsync(GlobalSettings entity);
bool Update(GlobalSettings entity);
}
}

View file

@ -0,0 +1,117 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: UserRepository.cs
// Created By:
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using Dapper;
using Ombi.Helpers;
using Ombi.Store.Models;
namespace Ombi.Store.Repository
{
public class PlexUserRepository : BaseGenericRepository<PlexUsers>, IPlexUserRepository
{
public PlexUserRepository(ISqliteConfiguration config, ICacheProvider cache) : base(config,cache)
{
DbConfig = config;
}
private ISqliteConfiguration DbConfig { get; }
private IDbConnection Db => DbConfig.DbConnection();
public PlexUsers GetUser(string userGuid)
{
var sql = @"SELECT * FROM PlexUsers
WHERE PlexUserId = @UserGuid";
return Db.QueryFirstOrDefault<PlexUsers>(sql, new {UserGuid = userGuid});
}
public PlexUsers GetUserByUsername(string username)
{
var sql = @"SELECT * FROM PlexUsers
WHERE Username = @UserName";
return Db.QueryFirstOrDefault<PlexUsers>(sql, new {UserName = username});
}
public async Task<PlexUsers> GetUserAsync(string userguid)
{
var sql = @"SELECT * FROM PlexUsers
WHERE PlexUserId = @UserGuid";
return await Db.QueryFirstOrDefaultAsync<PlexUsers>(sql, new {UserGuid = userguid});
}
#region abstract implementation
#pragma warning disable CS0809 // Obsolete member overrides non-obsolete member
[Obsolete]
public override PlexUsers Get(string id)
{
throw new System.NotImplementedException();
}
[Obsolete]
public override Task<PlexUsers> GetAsync(int id)
{
throw new System.NotImplementedException();
}
[Obsolete]
public override PlexUsers Get(int id)
{
throw new System.NotImplementedException();
}
[Obsolete]
public override Task<PlexUsers> GetAsync(string id)
{
throw new System.NotImplementedException();
}
#pragma warning restore CS0809 // Obsolete member overrides non-obsolete member
#endregion
}
public interface IPlexUserRepository
{
PlexUsers GetUser(string userGuid);
PlexUsers GetUserByUsername(string username);
Task<PlexUsers> GetUserAsync(string userguid);
IEnumerable<PlexUsers> Custom(Func<IDbConnection, IEnumerable<PlexUsers>> func);
long Insert(PlexUsers entity);
void Delete(PlexUsers entity);
IEnumerable<PlexUsers> GetAll();
bool UpdateAll(IEnumerable<PlexUsers> entity);
bool Update(PlexUsers entity);
Task<IEnumerable<PlexUsers>> GetAllAsync();
Task<bool> UpdateAsync(PlexUsers users);
Task<int> InsertAsync(PlexUsers users);
}
}

View file

@ -0,0 +1,230 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: SettingsJsonRepository.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Dapper.Contrib.Extensions;
using Ombi.Helpers;
using Ombi.Store.Models;
namespace Ombi.Store.Repository
{
public class RequestJsonRepository : IRequestRepository
{
private ICacheProvider Cache { get; }
public RequestJsonRepository(ISqliteConfiguration config, ICacheProvider cacheProvider)
{
Db = config;
Cache = cacheProvider;
}
private ISqliteConfiguration Db { get; }
public long Insert(RequestBlobs entity)
{
ResetCache();
using (var con = Db.DbConnection())
{
var id = con.Insert(entity);
return id;
}
}
public async Task<int> InsertAsync(RequestBlobs entity)
{
ResetCache();
using (var con = Db.DbConnection())
{
var id = await con.InsertAsync(entity).ConfigureAwait(false);
return id;
}
}
public IEnumerable<RequestBlobs> GetAll()
{
var key = "GetAll";
var item = Cache.GetOrSet(key, () =>
{
using (var con = Db.DbConnection())
{
var page = con.GetAll<RequestBlobs>();
return page;
}
}, 5);
return item;
}
public async Task<IEnumerable<RequestBlobs>> GetAllAsync()
{
var key = "GetAll";
var item = await Cache.GetOrSetAsync(key, async() =>
{
using (var con = Db.DbConnection())
{
var page = await con.GetAllAsync<RequestBlobs>().ConfigureAwait(false);
return page;
}
}, 5);
return item;
}
public RequestBlobs Get(int id)
{
var key = "Get" + id;
var item = Cache.GetOrSet(key, () =>
{
using (var con = Db.DbConnection())
{
var page = con.Get<RequestBlobs>(id);
return page;
}
}, 5);
return item;
}
public async Task<RequestBlobs> GetAsync(int id)
{
var key = "Get" + id;
var item = await Cache.GetOrSetAsync(key, async () =>
{
using (var con = Db.DbConnection())
{
var page = await con.GetAsync<RequestBlobs>(id).ConfigureAwait(false);
return page;
}
}, 5);
return item;
}
public bool Delete(RequestBlobs entity)
{
ResetCache();
using (var con = Db.DbConnection())
{
return con.Delete(entity);
}
}
public async Task<bool> DeleteAsync(RequestBlobs entity)
{
ResetCache();
using (var con = Db.DbConnection())
{
return await con.DeleteAsync(entity).ConfigureAwait(false);
}
}
public async Task<bool> DeleteAllAsync(IEnumerable<RequestBlobs> entity)
{
ResetCache();
var tasks = new List<Task<bool>>();
using (var db = Db.DbConnection())
{
db.Open();
foreach (var e in entity)
{
tasks.Add(db.DeleteAsync(e));
}
}
var result = await Task.WhenAll(tasks).ConfigureAwait(false);
return result.All(x => true);
}
public bool Update(RequestBlobs entity)
{
ResetCache();
using (var con = Db.DbConnection())
{
return con.Update(entity);
}
}
public async Task<bool> UpdateAsync(RequestBlobs entity)
{
ResetCache();
using (var con = Db.DbConnection())
{
return await con.UpdateAsync(entity).ConfigureAwait(false);
}
}
private void ResetCache()
{
Cache.Remove("Get");
Cache.Remove("GetAll");
}
public bool UpdateAll(IEnumerable<RequestBlobs> entity)
{
ResetCache();
var result = new HashSet<bool>();
using (var db = Db.DbConnection())
{
db.Open();
foreach (var e in entity)
{
result.Add(db.Update(e));
}
}
return result.All(x => true);
}
public async Task<bool> UpdateAllAsync(IEnumerable<RequestBlobs> entity)
{
ResetCache();
var tasks = new List<Task<bool>>();
using (var db = Db.DbConnection())
{
db.Open();
foreach (var e in entity)
{
tasks.Add(db.UpdateAsync(e));
}
}
var result = await Task.WhenAll(tasks).ConfigureAwait(false);
return result.All(x => true);
}
public bool DeleteAll(IEnumerable<RequestBlobs> entity)
{
ResetCache();
var result = new HashSet<bool>();
using (var db = Db.DbConnection())
{
db.Open();
foreach (var e in entity)
{
result.Add(db.Delete(e));
}
}
return result.All(x => true);
}
}
}

View file

@ -0,0 +1,167 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: SettingsJsonRepository.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Dapper.Contrib.Extensions;
using Ombi.Helpers;
using Ombi.Store.Models;
namespace Ombi.Store.Repository
{
public class SettingsJsonRepository : ISettingsRepository
{
private ICacheProvider Cache { get; }
private string TypeName { get; }
public SettingsJsonRepository(ISqliteConfiguration config, ICacheProvider cacheProvider)
{
Db = config;
Cache = cacheProvider;
TypeName = typeof(SettingsJsonRepository).Name;
}
private ISqliteConfiguration Db { get; }
public long Insert(GlobalSettings entity)
{
ResetCache();
using (var con = Db.DbConnection())
{
return con.Insert(entity);
}
}
public async Task<int> InsertAsync(GlobalSettings entity)
{
ResetCache();
using (var con = Db.DbConnection())
{
return await con.InsertAsync(entity).ConfigureAwait(false);
}
}
public IEnumerable<GlobalSettings> GetAll()
{
var key = TypeName + "GetAll";
var item = Cache.GetOrSet(key, () =>
{
using (var con = Db.DbConnection())
{
var page = con.GetAll<GlobalSettings>();
return page;
}
}, 5);
return item;
}
public async Task<IEnumerable<GlobalSettings>> GetAllAsync()
{
var key = TypeName + "GetAll";
var item = await Cache.GetOrSetAsync(key, async() =>
{
using (var con = Db.DbConnection())
{
var page = await con.GetAllAsync<GlobalSettings>().ConfigureAwait(false);
return page;
}
}, 5);
return item;
}
public GlobalSettings Get(string pageName)
{
var key = pageName + "Get";
var item = Cache.GetOrSet(key, () =>
{
using (var con = Db.DbConnection())
{
var page = con.GetAll<GlobalSettings>().SingleOrDefault(x => x.SettingsName == pageName);
return page;
}
}, 5);
return item;
}
public async Task<GlobalSettings> GetAsync(string settingsName)
{
var key = settingsName + "Get";
var item = await Cache.GetOrSetAsync(key, async() =>
{
using (var con = Db.DbConnection())
{
var page = await con.GetAllAsync<GlobalSettings>().ConfigureAwait(false);
return page.SingleOrDefault(x => x.SettingsName == settingsName);
}
}, 5);
return item;
}
public async Task<bool> DeleteAsync(GlobalSettings entity)
{
ResetCache();
using (var con = Db.DbConnection())
{
return await con.DeleteAsync(entity).ConfigureAwait(false);
}
}
public async Task<bool> UpdateAsync(GlobalSettings entity)
{
ResetCache();
using (var con = Db.DbConnection())
{
return await con.UpdateAsync(entity).ConfigureAwait(false);
}
}
public bool Delete(GlobalSettings entity)
{
ResetCache();
using (var con = Db.DbConnection())
{
return con.Delete(entity);
}
}
public bool Update(GlobalSettings entity)
{
ResetCache();
using (var con = Db.DbConnection())
{
return con.Update(entity);
}
}
private void ResetCache()
{
Cache.Remove("Get");
Cache.Remove(TypeName + "GetAll");
}
}
}

View file

@ -0,0 +1,113 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: UserRepository.cs
// Created By:
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using Dapper;
using Ombi.Helpers;
namespace Ombi.Store.Repository
{
public class UserRepository : BaseGenericRepository<UsersModel>, IUserRepository
{
public UserRepository(ISqliteConfiguration config, ICacheProvider cache) : base(config,cache)
{
DbConfig = config;
}
private ISqliteConfiguration DbConfig { get; }
private IDbConnection Db => DbConfig.DbConnection();
public UsersModel GetUser(string userGuid)
{
var sql = @"SELECT * FROM Users
WHERE Userguid = @UserGuid";
return Db.QueryFirstOrDefault<UsersModel>(sql, new {UserGuid = userGuid});
}
public UsersModel GetUserByUsername(string username)
{
var sql = @"SELECT * FROM Users
WHERE UserName = @UserName";
return Db.QueryFirstOrDefault<UsersModel>(sql, new {UserName = username});
}
public async Task<UsersModel> GetUserAsync(string userguid)
{
var sql = @"SELECT * FROM Users
WHERE UserGuid = @UserGuid";
return await Db.QueryFirstOrDefaultAsync<UsersModel>(sql, new {UserGuid = userguid});
}
#region abstract implementation
#pragma warning disable CS0809 // Obsolete member overrides non-obsolete member
[Obsolete]
public override UsersModel Get(string id)
{
throw new System.NotImplementedException();
}
[Obsolete]
public override Task<UsersModel> GetAsync(int id)
{
throw new System.NotImplementedException();
}
[Obsolete]
public override UsersModel Get(int id)
{
throw new System.NotImplementedException();
}
[Obsolete]
public override Task<UsersModel> GetAsync(string id)
{
throw new System.NotImplementedException();
}
#pragma warning restore CS0809 // Obsolete member overrides non-obsolete member
#endregion
}
public interface IUserRepository
{
UsersModel GetUser(string userGuid);
UsersModel GetUserByUsername(string username);
Task<UsersModel> GetUserAsync(string userguid);
IEnumerable<UsersModel> Custom(Func<IDbConnection, IEnumerable<UsersModel>> func);
long Insert(UsersModel entity);
void Delete(UsersModel entity);
IEnumerable<UsersModel> GetAll();
bool UpdateAll(IEnumerable<UsersModel> entity);
bool Update(UsersModel entity);
}
}