From 39dce9a7a0b723bf9a4fd151c54c005ed90c74e5 Mon Sep 17 00:00:00 2001 From: real-zony Date: Fri, 25 Jul 2025 23:01:53 +0800 Subject: [PATCH] feat: Add a basic API framework. --- Directory.Packages.props | 5 +++ ZonyLrcTools.sln | 10 ++++++ .../Database/DatabaseServiceExtensions.cs | 19 ++++++++++++ .../Domain/Settings/Setting.cs | 30 ++++++++++++++++++ .../Settings/DTOs/GetAllSettingsResponse.cs | 6 ++++ .../Settings/GetAllSettingsEndpoint.cs | 31 +++++++++++++++++++ src/ZonyLrcTools.Api/Program.cs | 23 ++++++++++++++ src/ZonyLrcTools.Api/ZonyLrcTools.Api.csproj | 18 +++++++++++ 8 files changed, 142 insertions(+) create mode 100644 src/ZonyLrcTools.Api/Database/DatabaseServiceExtensions.cs create mode 100644 src/ZonyLrcTools.Api/Domain/Settings/Setting.cs create mode 100644 src/ZonyLrcTools.Api/Endpoints/Settings/DTOs/GetAllSettingsResponse.cs create mode 100644 src/ZonyLrcTools.Api/Endpoints/Settings/GetAllSettingsEndpoint.cs create mode 100644 src/ZonyLrcTools.Api/Program.cs create mode 100644 src/ZonyLrcTools.Api/ZonyLrcTools.Api.csproj diff --git a/Directory.Packages.props b/Directory.Packages.props index f461e91..496b1a0 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -3,8 +3,13 @@ true + + + + + diff --git a/ZonyLrcTools.sln b/ZonyLrcTools.sln index a82357d..5e712ae 100644 --- a/ZonyLrcTools.sln +++ b/ZonyLrcTools.sln @@ -13,6 +13,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZonyLrcTools.Tests", "tests EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZonyLrcTools.Common", "src\ZonyLrcTools.Common\ZonyLrcTools.Common.csproj", "{9B42E4CA-61AA-4798-8D2B-2D8A7035EB67}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "frontend", "frontend", "{07691E21-94C7-4C9F-970D-B0C77F02B878}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZonyLrcTools.Api", "src\ZonyLrcTools.Api\ZonyLrcTools.Api.csproj", "{698A1F4D-86F1-4251-B708-9B15648D7245}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -31,6 +35,10 @@ Global {9B42E4CA-61AA-4798-8D2B-2D8A7035EB67}.Debug|Any CPU.Build.0 = Debug|Any CPU {9B42E4CA-61AA-4798-8D2B-2D8A7035EB67}.Release|Any CPU.ActiveCfg = Release|Any CPU {9B42E4CA-61AA-4798-8D2B-2D8A7035EB67}.Release|Any CPU.Build.0 = Release|Any CPU + {698A1F4D-86F1-4251-B708-9B15648D7245}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {698A1F4D-86F1-4251-B708-9B15648D7245}.Debug|Any CPU.Build.0 = Debug|Any CPU + {698A1F4D-86F1-4251-B708-9B15648D7245}.Release|Any CPU.ActiveCfg = Release|Any CPU + {698A1F4D-86F1-4251-B708-9B15648D7245}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -42,5 +50,7 @@ Global {55D74323-ABFA-4A73-A3BF-F3E8D5DE6DE8} = {C29FB05C-54B1-4020-94D2-87E192EB2F98} {FFBD3200-568F-455B-8390-5E76C51D522C} = {AF8ADB2F-E46C-4DEE-8316-652D9FE1A69B} {9B42E4CA-61AA-4798-8D2B-2D8A7035EB67} = {C29FB05C-54B1-4020-94D2-87E192EB2F98} + {07691E21-94C7-4C9F-970D-B0C77F02B878} = {C29FB05C-54B1-4020-94D2-87E192EB2F98} + {698A1F4D-86F1-4251-B708-9B15648D7245} = {C29FB05C-54B1-4020-94D2-87E192EB2F98} EndGlobalSection EndGlobal diff --git a/src/ZonyLrcTools.Api/Database/DatabaseServiceExtensions.cs b/src/ZonyLrcTools.Api/Database/DatabaseServiceExtensions.cs new file mode 100644 index 0000000..e8fc844 --- /dev/null +++ b/src/ZonyLrcTools.Api/Database/DatabaseServiceExtensions.cs @@ -0,0 +1,19 @@ +using FreeSql; +using Microsoft.Extensions.DependencyInjection; + +namespace ZonyLrcTools.Api.Database; + +public static class DatabaseServiceExtensions +{ + public static IServiceCollection RegisterDatabaseServices(this IServiceCollection services) + { + var freeSqlBuilder = new FreeSqlBuilder() + .UseConnectionString(DataType.Sqlite, "Data Source=ZonyLrcTools.db;Pooling=true;Max Pool Size=10;") + .UseAutoSyncStructure(true); + + var freeSql = freeSqlBuilder.Build(); + services.AddSingleton(freeSql); + + return services; + } +} \ No newline at end of file diff --git a/src/ZonyLrcTools.Api/Domain/Settings/Setting.cs b/src/ZonyLrcTools.Api/Domain/Settings/Setting.cs new file mode 100644 index 0000000..e7c1177 --- /dev/null +++ b/src/ZonyLrcTools.Api/Domain/Settings/Setting.cs @@ -0,0 +1,30 @@ +using FreeSql.DataAnnotations; + +namespace ZonyLrcTools.Api.Domain.Settings; + +[Table(Name = "Settings")] +public class Setting +{ + [Column(IsPrimary = true, IsIdentity = true)] + public Guid Id { get; set; } + + public string Key { get; protected set; } = null!; + + public string? Value { get; set; } + + public SettingValueType ValueType { get; protected set; } + + public Setting(string key, SettingValueType valueType) + { + Key = key; + ValueType = valueType; + } +} + +public enum SettingValueType +{ + String = 1, + Int = 2, + Bool = 3, + Double = 4 +} \ No newline at end of file diff --git a/src/ZonyLrcTools.Api/Endpoints/Settings/DTOs/GetAllSettingsResponse.cs b/src/ZonyLrcTools.Api/Endpoints/Settings/DTOs/GetAllSettingsResponse.cs new file mode 100644 index 0000000..b702657 --- /dev/null +++ b/src/ZonyLrcTools.Api/Endpoints/Settings/DTOs/GetAllSettingsResponse.cs @@ -0,0 +1,6 @@ +namespace ZonyLrcTools.Api.Endpoints.Settings.DTOs; + +public class GetAllSettingsResponse +{ + public bool IsAutoCheckUpdate { get; set; } +} \ No newline at end of file diff --git a/src/ZonyLrcTools.Api/Endpoints/Settings/GetAllSettingsEndpoint.cs b/src/ZonyLrcTools.Api/Endpoints/Settings/GetAllSettingsEndpoint.cs new file mode 100644 index 0000000..930e420 --- /dev/null +++ b/src/ZonyLrcTools.Api/Endpoints/Settings/GetAllSettingsEndpoint.cs @@ -0,0 +1,31 @@ +using FastEndpoints; +using ZonyLrcTools.Api.Domain.Settings; +using ZonyLrcTools.Api.Endpoints.Settings.DTOs; + +namespace ZonyLrcTools.Api.Endpoints.Settings; + +public class GetAllSettingsEndpoint(IFreeSql freeSql) : EndpointWithoutRequest +{ + public override void Configure() + { + Get("/api/settings"); + AllowAnonymous(); + } + + public override async Task HandleAsync(CancellationToken ct) + { + var setting = await freeSql.Select() + .ToListAsync(ct); + + if (setting.Count == 0) + { + await Send.NotFoundAsync(ct); + return; + } + + await Send.OkAsync(new GetAllSettingsResponse + { + IsAutoCheckUpdate = true + }, ct); + } +} \ No newline at end of file diff --git a/src/ZonyLrcTools.Api/Program.cs b/src/ZonyLrcTools.Api/Program.cs new file mode 100644 index 0000000..d161b95 --- /dev/null +++ b/src/ZonyLrcTools.Api/Program.cs @@ -0,0 +1,23 @@ +using FastEndpoints; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using ZonyLrcTools.Api.Database; + +namespace ZonyLrcTools.Api; + +class Program +{ + static async Task Main(string[] args) + { + var builder = WebApplication.CreateBuilder(args); + + builder.WebHost.ConfigureKestrel(k => k.ListenAnyIP(10086)); + + builder.Services.AddFastEndpoints(); + builder.Services.RegisterDatabaseServices(); + + var app = builder.Build(); + app.UseFastEndpoints(); + await app.RunAsync(); + } +} \ No newline at end of file diff --git a/src/ZonyLrcTools.Api/ZonyLrcTools.Api.csproj b/src/ZonyLrcTools.Api/ZonyLrcTools.Api.csproj new file mode 100644 index 0000000..ee212f3 --- /dev/null +++ b/src/ZonyLrcTools.Api/ZonyLrcTools.Api.csproj @@ -0,0 +1,18 @@ + + + + Exe + net9.0 + enable + enable + + + + + + + + + + +