-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
378 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Text; | ||
using Masa.Blazor.Pro.Components.Models; | ||
using SQLite; | ||
|
||
namespace Masa.Blazor.Pro.Components; | ||
|
||
public class ProDatabase | ||
{ | ||
public const string DatabaseFilename = "proapp.db"; | ||
|
||
public const SQLiteOpenFlags Flags = | ||
// open the database in read/write mode | ||
SQLiteOpenFlags.ReadWrite | | ||
// create the database if it doesn't exist | ||
SQLiteOpenFlags.Create | | ||
// enable multi-threaded database access | ||
SQLiteOpenFlags.SharedCache; | ||
|
||
// public static string DatabasePath => | ||
// Path.Combine(FileSystem.AppDataDirectory, DatabaseFilename); | ||
|
||
private SQLiteAsyncConnection? Database { get; set; } | ||
|
||
[MemberNotNull(nameof(Database))] | ||
async Task InitAsync() | ||
{ | ||
if (Database is not null) | ||
{ | ||
return; | ||
} | ||
|
||
Database = new SQLiteAsyncConnection(DatabaseFilename, Flags); | ||
await Database.CreateTableAsync<TodoTask>(); | ||
await Database.CreateTableAsync<TodoTag>(); | ||
} | ||
|
||
#region Todo task | ||
|
||
public async Task<int> CreateTaskAsync(TodoTask task) | ||
{ | ||
await InitAsync(); | ||
return await Database.InsertAsync(task); | ||
} | ||
|
||
public async Task UpdateTaskAsync(TodoTask task) | ||
{ | ||
await InitAsync(); | ||
await Database.UpdateAsync(task); | ||
} | ||
|
||
public async Task<List<TodoTask>> GetTasksAsync( | ||
int page, | ||
int pageSize, | ||
DateTime dateTime = default, | ||
int tag = 0, | ||
TodoTaskPriority? priority = null) | ||
{ | ||
await InitAsync(); | ||
|
||
var sqlBuilder = new StringBuilder(); | ||
sqlBuilder.Append("SELECT * FROM [TodoTask]"); | ||
|
||
var hasWhere = false; | ||
|
||
if (dateTime != default) | ||
{ | ||
hasWhere = true; | ||
|
||
var tick = dateTime.Ticks; | ||
var nextDay = tick + TimeSpan.TicksPerDay; | ||
sqlBuilder.Append(" WHERE [DueAt] >= ").Append(tick).Append(" AND [DueAt] < ").Append(nextDay); | ||
} | ||
|
||
if (tag != 0) | ||
{ | ||
sqlBuilder.Append(hasWhere ? " AND" : " WHERE"); | ||
sqlBuilder.Append(" [Tags] LIKE '%").Append(tag).Append(";%'"); | ||
} | ||
|
||
if (priority is not null) | ||
{ | ||
sqlBuilder.Append(hasWhere ? " AND" : " WHERE"); | ||
sqlBuilder.Append(" [Priority] = ").Append((int)priority); | ||
} | ||
|
||
sqlBuilder.Append(" ORDER BY [DueAt] DESC"); | ||
sqlBuilder.Append(" LIMIT ").Append(pageSize).Append(" OFFSET ").Append((page - 1) * pageSize); | ||
|
||
return await Database.QueryAsync<TodoTask>(sqlBuilder.ToString()); | ||
} | ||
|
||
#endregion | ||
|
||
#region Todo tag | ||
|
||
public async Task<int> CreateTagAsync(TodoTag tag) | ||
{ | ||
await InitAsync(); | ||
|
||
return await this.Database.InsertAsync(tag); | ||
} | ||
|
||
public async Task<List<TodoTag>> GetTagsAsync() | ||
{ | ||
await InitAsync(); | ||
|
||
return await Database.Table<TodoTag>().ToListAsync(); | ||
} | ||
|
||
#endregion | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using SQLite; | ||
|
||
namespace Masa.Blazor.Pro.Components.Models; | ||
|
||
public class TodoTag | ||
{ | ||
[PrimaryKey] [AutoIncrement] public int Id { get; set; } | ||
|
||
public string? Name { get; set; } | ||
|
||
public string? Color { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using BlazorComponent; | ||
using Masa.Blazor.Pro.Components.Models; | ||
using Microsoft.AspNetCore.Components; | ||
|
||
namespace Masa.Blazor.Pro.Components; | ||
|
||
public static class RenderFragments | ||
{ | ||
public static RenderFragment GenTagItem(TodoTag tag) => builder => | ||
{ | ||
builder.OpenComponent(0, typeof(MListItemIcon)); | ||
builder.AddAttribute(1, "Class", "mr-4"); | ||
builder.AddAttribute(2, "ChildContent", (RenderFragment)(sub => | ||
{ | ||
sub.OpenComponent(0, typeof(MIcon)); | ||
sub.AddAttribute(1, "Icon", (Icon)"mdi-circle"); | ||
sub.AddAttribute(2, "Color", tag.Color); | ||
sub.CloseComponent(); | ||
})); | ||
builder.CloseComponent(); | ||
|
||
builder.OpenComponent<MListItemContent>(3); | ||
builder.AddAttribute(4, "ChildContent", (RenderFragment)(sub => | ||
{ | ||
sub.OpenComponent<MListItemTitle>(0); | ||
sub.AddAttribute(1, "ChildContent", (RenderFragment)(c => c.AddContent(0, tag.Name))); | ||
sub.CloseComponent(); | ||
})); | ||
builder.CloseComponent(); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.