tiny server.
This commit is contained in:
44
App/Data/FlowerDatabase.cs
Normal file
44
App/Data/FlowerDatabase.cs
Normal file
@ -0,0 +1,44 @@
|
||||
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();
|
||||
}
|
||||
}
|
45
App/Data/Model/FlowerItem.cs
Normal file
45
App/Data/Model/FlowerItem.cs
Normal file
@ -0,0 +1,45 @@
|
||||
using SQLite;
|
||||
|
||||
namespace Blahblah.FlowerStory.Data.Model;
|
||||
|
||||
[Table("flowers")]
|
||||
public class FlowerItem
|
||||
{
|
||||
[Column("fid"), PrimaryKey, AutoIncrement]
|
||||
public int Id { get; set; }
|
||||
|
||||
[Column("categoryid")]
|
||||
public int CategoryId { get; set; }
|
||||
|
||||
private string categoryName;
|
||||
public string CategoryName
|
||||
{
|
||||
get
|
||||
{
|
||||
if (categoryName == null)
|
||||
{
|
||||
if (!Constants.Categories.TryGetValue(CategoryId, out categoryName))
|
||||
{
|
||||
categoryName = Constants.CategoryOther;
|
||||
}
|
||||
// TODO: i18n
|
||||
}
|
||||
return categoryName;
|
||||
}
|
||||
}
|
||||
|
||||
[Column("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
[Column("datebuy")]
|
||||
public DateTimeOffset DateBuy { get; set; }
|
||||
|
||||
[Column("cost")]
|
||||
public decimal? Cost { get; set; }
|
||||
|
||||
[Column("purchase")]
|
||||
public string Purchase { get; set; }
|
||||
|
||||
[Column("photo")]
|
||||
public byte[] Photo { get; set; }
|
||||
}
|
46
App/Data/Model/RecordItem.cs
Normal file
46
App/Data/Model/RecordItem.cs
Normal file
@ -0,0 +1,46 @@
|
||||
using SQLite;
|
||||
|
||||
namespace Blahblah.FlowerStory.Data.Model;
|
||||
|
||||
[Table("records")]
|
||||
public class RecordItem
|
||||
{
|
||||
[Column("rid"), PrimaryKey, AutoIncrement]
|
||||
public int Id { get; set; }
|
||||
|
||||
[Column("eid")]
|
||||
public int EventId { get; set; }
|
||||
|
||||
private string eventName;
|
||||
public string EventName
|
||||
{
|
||||
get
|
||||
{
|
||||
if (eventName == null)
|
||||
{
|
||||
if (Constants.Events.TryGetValue(EventId, out var @event))
|
||||
{
|
||||
eventName = @event.Name;
|
||||
}
|
||||
else
|
||||
{
|
||||
eventName = Constants.EventUnknown;
|
||||
}
|
||||
// TODO: i18n
|
||||
}
|
||||
return eventName;
|
||||
}
|
||||
}
|
||||
|
||||
[Column("date")]
|
||||
public DateTimeOffset Date { get; set; }
|
||||
|
||||
[Column("byuid")]
|
||||
public int? ByUserId { get; set; }
|
||||
|
||||
[Column("byname")]
|
||||
public string ByUserName { get; set; }
|
||||
|
||||
[Column("photo")]
|
||||
public byte[] Photo { get; set; }
|
||||
}
|
11
App/Data/Model/UserItem.cs
Normal file
11
App/Data/Model/UserItem.cs
Normal file
@ -0,0 +1,11 @@
|
||||
namespace Blahblah.FlowerStory.Data.Model;
|
||||
|
||||
public class UserItem
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public int Level { get; set; }
|
||||
public DateTimeOffset RegisterDate { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Email { get; set; }
|
||||
public string Mobile { get; set; }
|
||||
}
|
Reference in New Issue
Block a user