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.

71 lines
1.8 KiB

1 year ago
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
1 year ago
private SQLiteAsyncConnection _conn;
private async Task Init()
1 year ago
{
if (_conn != null)
return;
1 year ago
_conn = new SQLiteAsyncConnection(_dbPath);
await _conn.CreateTableAsync<Person>();
1 year ago
}
public PersonRepository(string dbPath)
{
_dbPath = dbPath;
}
1 year ago
public async Task AddNewPerson(string name)
1 year ago
{
int result = 0;
try
{
1 year ago
await Init();
1 year ago
if (string.IsNullOrEmpty(name))
throw new Exception("Valid name required");
1 year ago
result = await _conn.InsertAsync(new Person { Name = name });
1 year ago
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);
}
}
1 year ago
public async Task<List<Person>> GetAllPeople()
1 year ago
{
try
{
1 year ago
await Init();
return await _conn.Table<Person>().ToListAsync();
1 year ago
}
catch (Exception ex)
{
StatusMessage = string.Format("Failed to retrieve data. {0}", ex.Message);
}
return new List<Person>();
}
}
}