46 lines
1.0 KiB
C#
46 lines
1.0 KiB
C#
using Blahblah.FlowerApp.Data.Model;
|
|
using Microsoft.Extensions.Logging;
|
|
using SQLite;
|
|
|
|
namespace Blahblah.FlowerApp.Data;
|
|
|
|
public class FlowerDatabase
|
|
{
|
|
private SQLiteAsyncConnection database = null!;
|
|
|
|
private readonly ILogger logger;
|
|
|
|
public FlowerDatabase(ILogger<FlowerDatabase> logger)
|
|
{
|
|
this.logger = logger;
|
|
}
|
|
|
|
private async Task Init()
|
|
{
|
|
if (database is not null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
database = new SQLiteAsyncConnection(Constants.DatabasePath, Constants.SQLiteFlags);
|
|
|
|
#if DEBUG
|
|
var result =
|
|
#endif
|
|
await database.CreateTablesAsync<FlowerItem, RecordItem, PhotoItem, UserItem>();
|
|
|
|
#if DEBUG
|
|
foreach (var item in result.Results)
|
|
{
|
|
logger.LogInformation("create table {table}, result: {result}", item.Key, item.Value);
|
|
}
|
|
#endif
|
|
}
|
|
|
|
public async Task<FlowerItem[]> GetFlowers()
|
|
{
|
|
await Init();
|
|
return await database.Table<FlowerItem>().ToArrayAsync();
|
|
}
|
|
}
|