Skip to content

Commit

Permalink
Merge pull request #284 from tidusjar/dev
Browse files Browse the repository at this point in the history
1.7.5
  • Loading branch information
Jamie committed May 29, 2016
2 parents 570b450 + 247ef48 commit f4e75f2
Show file tree
Hide file tree
Showing 64 changed files with 1,811 additions and 8,321 deletions.
10 changes: 10 additions & 0 deletions PlexRequests.Core/IRequestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,30 @@
#endregion

using System.Collections.Generic;
using System.Threading.Tasks;

using PlexRequests.Store;

namespace PlexRequests.Core
{
public interface IRequestService
{
long AddRequest(RequestedModel model);
Task<int> AddRequestAsync(RequestedModel model);
RequestedModel CheckRequest(int providerId);
Task<RequestedModel> CheckRequestAsync(int providerId);
RequestedModel CheckRequest(string musicId);
Task<RequestedModel> CheckRequestAsync(string musicId);

void DeleteRequest(RequestedModel request);
Task DeleteRequestAsync(RequestedModel request);
bool UpdateRequest(RequestedModel model);
Task<bool> UpdateRequestAsync(RequestedModel model);
RequestedModel Get(int id);
Task<RequestedModel> GetAsync(int id);
IEnumerable<RequestedModel> GetAll();
Task<IEnumerable<RequestedModel>> GetAllAsync();
bool BatchUpdate(List<RequestedModel> model);
bool BatchDelete(List<RequestedModel> model);
}
}
6 changes: 6 additions & 0 deletions PlexRequests.Core/ISettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion

using System.Threading.Tasks;

namespace PlexRequests.Core
{
public interface ISettingsService<T>
{
T GetSettings();
Task<T> GetSettingsAsync();
bool SaveSettings(T model);
Task<bool> SaveSettingsAsync(T model);
bool Delete(T model);
Task<bool> DeleteAsync(T model);
}
}
65 changes: 65 additions & 0 deletions PlexRequests.Core/JsonRequestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Newtonsoft.Json;

Expand Down Expand Up @@ -57,32 +58,71 @@ public long AddRequest(RequestedModel model)
return result ? id : -1;
}

public async Task<int> AddRequestAsync(RequestedModel model)
{
var entity = new RequestBlobs { Type = model.Type, Content = ByteConverterHelper.ReturnBytes(model), ProviderId = model.ProviderId };
var id = await Repo.InsertAsync(entity);

model.Id = id;

entity = new RequestBlobs { Type = model.Type, Content = ByteConverterHelper.ReturnBytes(model), ProviderId = model.ProviderId, Id = id, MusicId = model.MusicBrainzId };
var result = await Repo.UpdateAsync(entity);

return result ? id : -1;
}

public RequestedModel CheckRequest(int providerId)
{
var blobs = Repo.GetAll();
var blob = blobs.FirstOrDefault(x => x.ProviderId == providerId);
return blob != null ? ByteConverterHelper.ReturnObject<RequestedModel>(blob.Content) : null;
}

public async Task<RequestedModel> CheckRequestAsync(int providerId)
{
var blobs = await Repo.GetAllAsync();
var blob = blobs.FirstOrDefault(x => x.ProviderId == providerId);
return blob != null ? ByteConverterHelper.ReturnObject<RequestedModel>(blob.Content) : null;
}

public RequestedModel CheckRequest(string musicId)
{
var blobs = Repo.GetAll();
var blob = blobs.FirstOrDefault(x => x.MusicId == musicId);
return blob != null ? ByteConverterHelper.ReturnObject<RequestedModel>(blob.Content) : null;
}

public async Task<RequestedModel> CheckRequestAsync(string musicId)
{
var blobs = await Repo.GetAllAsync();
var blob = blobs.FirstOrDefault(x => x.MusicId == musicId);
return blob != null ? ByteConverterHelper.ReturnObject<RequestedModel>(blob.Content) : null;
}

public void DeleteRequest(RequestedModel request)
{
var blob = Repo.Get(request.Id);
Repo.Delete(blob);
}

public async Task DeleteRequestAsync(RequestedModel request)
{
var blob = await Repo.GetAsync(request.Id);
await Repo.DeleteAsync(blob);
}

public bool UpdateRequest(RequestedModel model)
{
var entity = new RequestBlobs { Type = model.Type, Content = ByteConverterHelper.ReturnBytes(model), ProviderId = model.ProviderId, Id = model.Id };
return Repo.Update(entity);
}

public async Task<bool> UpdateRequestAsync(RequestedModel model)
{
var entity = new RequestBlobs { Type = model.Type, Content = ByteConverterHelper.ReturnBytes(model), ProviderId = model.ProviderId, Id = model.Id };
return await Repo.UpdateAsync(entity);
}

public RequestedModel Get(int id)
{
var blob = Repo.Get(id);
Expand All @@ -94,6 +134,17 @@ public RequestedModel Get(int id)
return model;
}

public async Task<RequestedModel> GetAsync(int id)
{
var blob = await Repo.GetAsync(id);
if (blob == null)
{
return new RequestedModel();
}
var model = ByteConverterHelper.ReturnObject<RequestedModel>(blob.Content);
return model;
}

public IEnumerable<RequestedModel> GetAll()
{
var blobs = Repo.GetAll();
Expand All @@ -102,10 +153,24 @@ public IEnumerable<RequestedModel> GetAll()
.ToList();
}

public async Task<IEnumerable<RequestedModel>> GetAllAsync()
{
var blobs = await Repo.GetAllAsync();
return blobs.Select(b => Encoding.UTF8.GetString(b.Content))
.Select(JsonConvert.DeserializeObject<RequestedModel>)
.ToList();
}

public bool BatchUpdate(List<RequestedModel> model)
{
var entities = model.Select(m => new RequestBlobs { Type = m.Type, Content = ByteConverterHelper.ReturnBytes(m), ProviderId = m.ProviderId, Id = m.Id }).ToList();
return Repo.UpdateAll(entities);
}

public bool BatchDelete(List<RequestedModel> model)
{
var entities = model.Select(m => new RequestBlobs { Type = m.Type, Content = ByteConverterHelper.ReturnBytes(m), ProviderId = m.ProviderId, Id = m.Id }).ToList();
return Repo.DeleteAll(entities);
}
}
}
53 changes: 53 additions & 0 deletions PlexRequests.Core/SettingsServiceV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion

using System.Threading.Tasks;
using Newtonsoft.Json;

using PlexRequests.Core.SettingModels;
Expand Down Expand Up @@ -62,6 +64,21 @@ public T GetSettings()
return model;
}

public async Task<T> GetSettingsAsync()
{
var result = await Repo.GetAsync(EntityName);
if (result == null)
{
return new T();
}
result.Content = DecryptSettings(result);
var obj = string.IsNullOrEmpty(result.Content) ? null : JsonConvert.DeserializeObject<T>(result.Content, SerializerSettings.Settings);

var model = obj;

return model;
}

public bool SaveSettings(T model)
{
var entity = Repo.Get(EntityName);
Expand All @@ -88,6 +105,31 @@ public bool SaveSettings(T model)
return result;
}

public async Task<bool> SaveSettingsAsync(T model)
{
var entity = await Repo.GetAsync(EntityName);

if (entity == null)
{
var newEntity = model;

var settings = new GlobalSettings { SettingsName = EntityName, Content = JsonConvert.SerializeObject(newEntity, SerializerSettings.Settings) };
settings.Content = EncryptSettings(settings);
var insertResult = await Repo.InsertAsync(settings);

return insertResult != int.MinValue;
}

var modified = model;
modified.Id = entity.Id;

var globalSettings = new GlobalSettings { SettingsName = EntityName, Content = JsonConvert.SerializeObject(modified, SerializerSettings.Settings), Id = entity.Id };
globalSettings.Content = EncryptSettings(globalSettings);
var result = await Repo.UpdateAsync(globalSettings);

return result;
}

public bool Delete(T model)
{
var entity = Repo.Get(EntityName);
Expand All @@ -100,6 +142,17 @@ public bool Delete(T model)
return true;
}

public async Task<bool> DeleteAsync(T model)
{
var entity = Repo.Get(EntityName);
if (entity != null)
{
return await Repo.DeleteAsync(entity);
}

return true;
}

private string EncryptSettings(GlobalSettings settings)
{
return StringCipher.Encrypt(settings.Content, settings.SettingsName);
Expand Down
53 changes: 51 additions & 2 deletions PlexRequests.Core/Setup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,18 @@ public string SetupDb(string urlBase)
{
CreateDefaultSettingsPage(urlBase);
}

var version = CheckSchema();
if (version > 0)
{
if (version > 1700 && version <= 1799)
{
MigrateToVersion1700();
}
if (version > 1800 && version <= 1899)
{
MigrateToVersion1800();
}
}

return Db.DbConnection().ConnectionString;
Expand All @@ -73,7 +77,7 @@ private int CheckSchema()
{
var productVersion = AssemblyHelper.GetProductVersion();
var trimStatus = new Regex("[^0-9]", RegexOptions.Compiled).Replace(productVersion, string.Empty).PadRight(4, '0');
var version = int.Parse(trimStatus);
var version = int.Parse(trimStatus);

var connection = Db.DbConnection();
var schema = connection.GetSchemaVersion();
Expand Down Expand Up @@ -173,5 +177,50 @@ public void MigrateToVersion1700()
TableCreation.DropTable(Db.DbConnection(), "User");
TableCreation.DropTable(Db.DbConnection(), "Log");
}

/// <summary>
/// Migrates to version 1.8.
/// <para>This includes updating the admin account to have all roles.</para>
/// <para>Set the log level to info</para>
/// </summary>
private void MigrateToVersion1800()
{
try
{
var userMapper = new UserMapper(new UserRepository<UsersModel>(Db, new MemoryCacheProvider()));
var users = userMapper.GetUsers();

foreach (var u in users)
{
var claims = new[] { UserClaims.User, UserClaims.Admin, UserClaims.PowerUser };
u.Claims = ByteConverterHelper.ReturnBytes(claims);

userMapper.EditUser(u);
}
}
catch (Exception e)
{
Log.Error(e);
throw;
}

try
{
var settingsService = new SettingsServiceV2<LogSettings>(new SettingsJsonRepository(Db, new MemoryCacheProvider()));
var logSettings = settingsService.GetSettings();
logSettings.Level = LogLevel.Info.Ordinal;
settingsService.SaveSettings(logSettings);

LoggingHelper.ReconfigureLogLevel(LogLevel.FromOrdinal(logSettings.Level));

}
catch (Exception e)
{
Log.Error(e);
throw;
}


}
}
}
Loading

0 comments on commit f4e75f2

Please sign in to comment.