flower-story/App/Data/FlowerDatabase.cs
2023-05-23 08:37:20 +08:00

45 lines
990 B
C#

using Blahblah.FlowerStory.Data.Model;
using Microsoft.Extensions.Logging;
using SQLite;
namespace Blahblah.FlowerStory.Data;
public class FlowerDatabase
{
private SQLiteAsyncConnection database;
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.Flags);
#if DEBUG
var result =
#endif
await database.CreateTablesAsync<FlowerItem, RecordItem>();
#if DEBUG
foreach (var item in result.Results)
{
logger.LogDebug("create table {table}, result: {result}", item.Key, item.Value);
}
#endif
}
public async Task<List<FlowerItem>> GetFlowers()
{
await Init();
return await database.Table<FlowerItem>().ToListAsync();
}
}