bitwarden-android/src/Core/Services/LiteDbStorageService.cs

62 lines
1.7 KiB
C#
Raw Normal View History

2019-03-28 06:44:54 +03:00
using Bit.Core.Abstractions;
using LiteDB;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
2019-03-28 06:44:54 +03:00
using System.Linq;
using System.Threading.Tasks;
namespace Bit.Core.Services
{
public class LiteDbStorageService : IStorageService
{
private readonly LiteCollection<JsonItem> _collection;
private readonly JsonSerializerSettings _jsonSettings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
2019-03-28 06:44:54 +03:00
public LiteDbStorageService(string dbPath)
{
2019-03-28 06:47:24 +03:00
var db = new LiteDatabase($"Filename={dbPath};");
2019-03-28 06:44:54 +03:00
_collection = db.GetCollection<JsonItem>("json_items");
}
public Task<T> GetAsync<T>(string key)
{
var item = _collection.Find(i => i.Id == key).FirstOrDefault();
if(item == null)
{
return Task.FromResult(default(T));
}
return Task.FromResult(JsonConvert.DeserializeObject<T>(item.Value, _jsonSettings));
2019-03-28 06:44:54 +03:00
}
public Task SaveAsync<T>(string key, T obj)
{
var data = JsonConvert.SerializeObject(obj, _jsonSettings);
2019-03-28 06:44:54 +03:00
_collection.Upsert(new JsonItem(key, data));
return Task.FromResult(0);
}
public Task RemoveAsync(string key)
{
_collection.Delete(i => i.Id == key);
return Task.FromResult(0);
}
private class JsonItem
{
public JsonItem() { }
public JsonItem(string key, string value)
{
Id = key;
Value = value;
}
public string Id { get; set; }
public string Value { get; set; }
}
}
}