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.
|
|
|
|
using LocalDataStorageWithSQLite.Models;
|
|
|
|
|
using SQLite;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace LocalDataStorageWithSQLite.Repositories
|
|
|
|
|
{
|
|
|
|
|
public class PersonRepository
|
|
|
|
|
{
|
|
|
|
|
string _dbPath;
|
|
|
|
|
|
|
|
|
|
public string StatusMessage { get; set; }
|
|
|
|
|
|
|
|
|
|
// TODO: Add variable for the SQLite connection
|
|
|
|
|
|
|
|
|
|
private SQLiteAsyncConnection _conn;
|
|
|
|
|
private async Task Init()
|
|
|
|
|
{
|
|
|
|
|
if (_conn != null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
_conn = new SQLiteAsyncConnection(_dbPath);
|
|
|
|
|
await _conn.CreateTableAsync<Person>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PersonRepository(string dbPath)
|
|
|
|
|
{
|
|
|
|
|
_dbPath = dbPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task AddNewPerson(string name)
|
|
|
|
|
{
|
|
|
|
|
int result = 0;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await Init();
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(name))
|
|
|
|
|
throw new Exception("Valid name required");
|
|
|
|
|
|
|
|
|
|
result = await _conn.InsertAsync(new Person { Name = name });
|
|
|
|
|
|
|
|
|
|
StatusMessage = string.Format("{0} record(s) added (Name: {1})", result, name);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
StatusMessage = string.Format("Failed to add {0}. Error: {1}", name, ex.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<List<Person>> GetAllPeople()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await Init();
|
|
|
|
|
return await _conn.Table<Person>().ToListAsync();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
StatusMessage = string.Format("Failed to retrieve data. {0}", ex.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new List<Person>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|