|
|
|
|
using SQLite;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Justice.Helpers
|
|
|
|
|
{
|
|
|
|
|
public class DatabaseHelper
|
|
|
|
|
{
|
|
|
|
|
private readonly SQLiteAsyncConnection _database;
|
|
|
|
|
|
|
|
|
|
public DatabaseHelper(string databaseName = "JusticeAppDatabase.db")
|
|
|
|
|
{
|
|
|
|
|
var dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), databaseName);
|
|
|
|
|
_database = new SQLiteAsyncConnection(dbPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task InitializeAsync<T>() where T : class, new()
|
|
|
|
|
{
|
|
|
|
|
await _database.CreateTableAsync<T>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<int> InsertAsync<T>(T item) where T : class, new()
|
|
|
|
|
{
|
|
|
|
|
return await _database.InsertAsync(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<List<T>> GetAllAsync<T>() where T : class, new()
|
|
|
|
|
{
|
|
|
|
|
return await _database.Table<T>().ToListAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<int> DeleteAsync<T>(T item) where T : class, new()
|
|
|
|
|
{
|
|
|
|
|
return await _database.DeleteAsync(item);
|
|
|
|
|
}
|
|
|
|
|
public async Task<T> FindAsync<T>(string query, params object[] args) where T : new()
|
|
|
|
|
{
|
|
|
|
|
return await _database.FindWithQueryAsync<T>(query, args);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|