Made the search page all async goodness #278

This commit is contained in:
tidusjar 2016-05-27 22:04:25 +01:00
commit 03ce361183
13 changed files with 432 additions and 183 deletions

View file

@ -61,6 +61,7 @@
<Compile Include="Entity.cs" />
<Compile Include="Models\ScheduledJobs.cs" />
<Compile Include="Models\UsersToNotify.cs" />
<Compile Include="Repository\BaseGenericRepository.cs" />
<Compile Include="Repository\IRequestRepository.cs" />
<Compile Include="Repository\ISettingsRepository.cs" />
<Compile Include="ISqliteConfiguration.cs" />

View file

@ -0,0 +1,179 @@
#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.Linq;
using System.Threading.Tasks;
using Dapper.Contrib.Extensions;
using NLog;
using PlexRequests.Helpers;
namespace PlexRequests.Store.Repository
{
public abstract class BaseGenericRepository<T> where T : class
{
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);
public long Insert(T entity)
{
ResetCache();
using (var cnn = Config.DbConnection())
{
cnn.Open();
return cnn.Insert(entity);
}
}
public void Delete(T entity)
{
ResetCache();
using (var db = Config.DbConnection())
{
db.Open();
db.Delete(entity);
}
}
public async Task DeleteAsync(T entity)
{
ResetCache();
using (var db = Config.DbConnection())
{
db.Open();
await db.DeleteAsync(entity);
}
}
public bool Update(T entity)
{
ResetCache();
Log.Trace("Updating entity");
Log.Trace(entity.DumpJson());
using (var db = Config.DbConnection())
{
db.Open();
return db.Update(entity);
}
}
public async Task<bool> UpdateAsync(T entity)
{
ResetCache();
Log.Trace("Updating entity");
Log.Trace(entity.DumpJson());
using (var db = Config.DbConnection())
{
db.Open();
return await db.UpdateAsync(entity);
}
}
public bool UpdateAll(IEnumerable<T> entity)
{
ResetCache();
Log.Trace("Updating all entities");
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);
}
public async Task<bool> UpdateAllAsync(IEnumerable<T> entity)
{
ResetCache();
Log.Trace("Updating all entities");
var result = new HashSet<bool>();
using (var db = Config.DbConnection())
{
db.Open();
foreach (var e in entity)
{
result.Add(await db.UpdateAsync(e));
}
}
return result.All(x => true);
}
public async Task<int> InsertAsync(T entity)
{
ResetCache();
using (var cnn = Config.DbConnection())
{
cnn.Open();
return await cnn.InsertAsync(entity);
}
}
private void ResetCache()
{
Cache.Remove("Get");
Cache.Remove("GetAll");
}
public IEnumerable<T> GetAll()
{
using (var db = Config.DbConnection())
{
db.Open();
var result = db.GetAll<T>();
return result;
}
}
public async Task<IEnumerable<T>> GetAllAsync()
{
using (var db = Config.DbConnection())
{
db.Open();
var result = await db.GetAllAsync<T>();
return result;
}
}
}
}

View file

@ -1,4 +1,5 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: GenericRepository.cs
@ -23,59 +24,38 @@
// 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.Linq;
using System.Threading.Tasks;
using Dapper.Contrib.Extensions;
using NLog;
using PlexRequests.Helpers;
namespace PlexRequests.Store.Repository
{
public class GenericRepository<T> : IRepository<T> where T : Entity
public class GenericRepository<T> : BaseGenericRepository<T>, IRepository<T> where T : Entity
{
private ICacheProvider Cache { get; }
public GenericRepository(ISqliteConfiguration config, ICacheProvider cache)
public GenericRepository(ISqliteConfiguration config, ICacheProvider cache) : base(config, cache)
{
Config = config;
Cache = cache;
}
private static Logger Log = LogManager.GetCurrentClassLogger();
private ISqliteConfiguration Config { get; }
public long Insert(T entity)
{
ResetCache();
using (var cnn = Config.DbConnection())
{
cnn.Open();
return cnn.Insert(entity);
}
}
public IEnumerable<T> GetAll()
{
using (var db = Config.DbConnection())
{
db.Open();
var result = db.GetAll<T>();
return result;
}
}
public T Get(string id)
public override T Get(string id)
{
throw new NotSupportedException("Get(string) is not supported. Use Get(int)");
}
public T Get(int id)
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(
@ -91,49 +71,22 @@ namespace PlexRequests.Store.Repository
return item;
}
public void Delete(T entity)
public override async Task<T> GetAsync(int id)
{
ResetCache();
using (var db = Config.DbConnection())
{
db.Open();
db.Delete(entity);
}
}
public bool Update(T entity)
{
ResetCache();
Log.Trace("Updating entity");
Log.Trace(entity.DumpJson());
using (var db = Config.DbConnection())
{
db.Open();
return db.Update(entity);
}
}
public bool UpdateAll(IEnumerable<T> entity)
{
ResetCache();
Log.Trace("Updating all entities");
var result = new HashSet<bool>();
using (var db = Config.DbConnection())
{
db.Open();
foreach (var e in entity)
var key = "Get" + id;
var item = await Cache.GetOrSetAsync(
key,
async () =>
{
result.Add(db.Update(e));
}
}
return result.All(x => true);
using (var db = Config.DbConnection())
{
db.Open();
return await db.GetAsync<T>(id);
}
});
return item;
}
private void ResetCache()
{
Cache.Remove("Get");
Cache.Remove("GetAll");
}
}
}
}

View file

@ -25,6 +25,7 @@
// ************************************************************************/
#endregion
using System.Collections.Generic;
using System.Threading.Tasks;
namespace PlexRequests.Store.Repository
{
@ -35,12 +36,15 @@ namespace PlexRequests.Store.Repository
/// </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.
@ -48,12 +52,15 @@ namespace PlexRequests.Store.Repository
/// <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.
@ -61,6 +68,7 @@ namespace PlexRequests.Store.Repository
/// <param name="entity">The entity.</param>
/// <returns></returns>
bool Update(T entity);
Task<bool> UpdateAsync(T entity);
/// <summary>
/// Updates all.
@ -68,5 +76,6 @@ namespace PlexRequests.Store.Repository
/// <param name="entity">The entity.</param>
/// <returns></returns>
bool UpdateAll(IEnumerable<T> entity);
Task<bool> UpdateAllAsync(IEnumerable<T> entity);
}
}

View file

@ -25,7 +25,7 @@
// ************************************************************************/
#endregion
using System.Collections.Generic;
using System.Threading.Tasks;
using PlexRequests.Store.Models;
namespace PlexRequests.Store.Repository
@ -37,12 +37,14 @@ namespace PlexRequests.Store.Repository
/// </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.
@ -50,12 +52,14 @@ namespace PlexRequests.Store.Repository
/// <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>
@ -63,6 +67,7 @@ namespace PlexRequests.Store.Repository
/// </summary>
/// <param name="entity">The entity.</param>
/// <returns></returns>
Task<bool> UpdateAsync(GlobalSettings entity);
bool Update(GlobalSettings entity);

View file

@ -26,7 +26,7 @@
#endregion
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Dapper.Contrib.Extensions;
using PlexRequests.Helpers;
@ -57,6 +57,15 @@ namespace PlexRequests.Store.Repository
}
}
public async Task<int> InsertAsync(GlobalSettings entity)
{
ResetCache();
using (var con = Db.DbConnection())
{
return await con.InsertAsync(entity);
}
}
public IEnumerable<GlobalSettings> GetAll()
{
var key = TypeName + "GetAll";
@ -71,6 +80,20 @@ namespace PlexRequests.Store.Repository
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>();
return page;
}
}, 5);
return item;
}
public GlobalSettings Get(string pageName)
{
var key = pageName + "Get";
@ -85,6 +108,38 @@ namespace PlexRequests.Store.Repository
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>();
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);
}
}
public async Task<bool> UpdateAsync(GlobalSettings entity)
{
ResetCache();
using (var con = Db.DbConnection())
{
return await con.UpdateAsync(entity);
}
}
public bool Delete(GlobalSettings entity)
{
ResetCache();

View file

@ -27,41 +27,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Dapper.Contrib.Extensions;
using PlexRequests.Helpers;
using PlexRequests.Store.Repository;
namespace PlexRequests.Store
{
public class UserRepository<T> : IRepository<T> where T : UserEntity
public class UserRepository<T> : BaseGenericRepository<T>, IRepository<T> where T : UserEntity
{
public UserRepository(ISqliteConfiguration config)
public UserRepository(ISqliteConfiguration config, ICacheProvider cache) : base(config, cache)
{
Config = config;
}
private ISqliteConfiguration Config { get; }
public long Insert(T entity)
public override T Get(int id)
{
using (var cnn = Config.DbConnection())
{
cnn.Open();
return cnn.Insert(entity);
}
throw new NotSupportedException("Get(int) is not supported. Use Get(string)");
}
public IEnumerable<T> GetAll()
public override Task<T> GetAsync(int id)
{
using (var db = Config.DbConnection())
{
db.Open();
var result = db.GetAll<T>();
return result;
}
throw new NotSupportedException("GetAsync(int) is not supported. Use GetAsync(string)");
}
public T Get(string id)
public override T Get(string id)
{
using (var db = Config.DbConnection())
{
@ -72,33 +62,18 @@ namespace PlexRequests.Store
}
}
public T Get(int id)
{
throw new NotSupportedException("Get(int) is not supported. Use Get(string)");
}
public void Delete(T entity)
public override async Task<T> GetAsync(string id)
{
using (var db = Config.DbConnection())
{
db.Open();
db.Delete(entity);
var result = await db.GetAllAsync<T>();
var selected = result.FirstOrDefault(x => x.UserGuid == id);
return selected;
}
}
public bool Update(T entity)
{
using (var db = Config.DbConnection())
{
db.Open();
return db.Update(entity);
}
}
public bool UpdateAll(IEnumerable<T> entity)
{
throw new NotSupportedException();
}
}
}