mirror of https://github.com/Ombi-app/Ombi
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
115 lines
4.0 KiB
115 lines
4.0 KiB
#region Copyright
|
|
// /************************************************************************
|
|
// Copyright (c) 2016 Jamie Rees
|
|
// File: SettingsServiceV2.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 Newtonsoft.Json;
|
|
|
|
using Omu.ValueInjecter;
|
|
|
|
using RequestPlex.Core.SettingModels;
|
|
using RequestPlex.Helpers;
|
|
using RequestPlex.Store;
|
|
using RequestPlex.Store.Models;
|
|
|
|
namespace RequestPlex.Core
|
|
{
|
|
public class SettingsServiceV2<T> : ISettingsService<T>
|
|
where T : Settings, new()
|
|
{
|
|
|
|
public SettingsServiceV2(ISettingsRepository repo)
|
|
{
|
|
Repo = repo;
|
|
EntityName = typeof(T).Name;
|
|
}
|
|
|
|
private ISettingsRepository Repo { get; set; }
|
|
private string EntityName { get; set; }
|
|
|
|
public T GetSettings()
|
|
{
|
|
var result = Repo.Get(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);
|
|
|
|
if (entity == null)
|
|
{
|
|
var newEntity = model;
|
|
|
|
var settings = new GlobalSettings { SettingsName = EntityName, Content = JsonConvert.SerializeObject(newEntity, SerializerSettings.Settings) };
|
|
settings.Content = EncryptSettings(settings);
|
|
var insertResult = Repo.Insert(settings);
|
|
|
|
return insertResult != long.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 = Repo.Update(globalSettings);
|
|
|
|
return result;
|
|
}
|
|
|
|
public bool Delete(T model)
|
|
{
|
|
var entity = Repo.Get(EntityName);
|
|
if (entity != null)
|
|
{
|
|
return Repo.Delete(entity);
|
|
}
|
|
|
|
// Entity does not exist so nothing to delete
|
|
return true;
|
|
}
|
|
|
|
private string EncryptSettings(GlobalSettings settings)
|
|
{
|
|
return StringCipher.Encrypt(settings.Content, settings.SettingsName);
|
|
}
|
|
|
|
private string DecryptSettings(GlobalSettings settings)
|
|
{
|
|
return StringCipher.Decrypt(settings.Content, settings.SettingsName);
|
|
}
|
|
}
|
|
}
|