From e90357d153fcf86c6bf1bb7f6def8f1ba074ca28 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Wed, 9 Apr 2025 00:11:45 +0800 Subject: [PATCH] :sparkles: Swagger UI --- DysonNetwork.Sphere/Account/Account.cs | 5 +++-- .../Account/AccountController.cs | 9 ++++---- .../DysonNetwork.Sphere.csproj | 2 ++ DysonNetwork.Sphere/Program.cs | 21 +++++++++++++++++++ DysonNetwork.sln.DotSettings.user | 4 +++- 5 files changed, 34 insertions(+), 7 deletions(-) diff --git a/DysonNetwork.Sphere/Account/Account.cs b/DysonNetwork.Sphere/Account/Account.cs index 4b8da1f..71623a0 100644 --- a/DysonNetwork.Sphere/Account/Account.cs +++ b/DysonNetwork.Sphere/Account/Account.cs @@ -1,5 +1,6 @@ using System.ComponentModel.DataAnnotations; using System.Text; +using System.Text.Json.Serialization; using NodaTime; using Org.BouncyCastle.Crypto.Generators; using Org.BouncyCastle.Security; @@ -23,7 +24,7 @@ public class AccountContact : BaseModel public Instant? VerifiedAt { get; set; } [MaxLength(1024)] public string Content { get; set; } = string.Empty; - public Account Account { get; set; } = null!; + [JsonIgnore] public Account Account { get; set; } = null!; } public enum AccountContactType @@ -37,7 +38,7 @@ public class AccountAuthFactor : BaseModel public AccountAuthFactorType Type { get; set; } public string? Secret { get; set; } = null; - public Account Account { get; set; } = null!; + [JsonIgnore] public Account Account { get; set; } = null!; public AccountAuthFactor HashSecret(int cost = 12) { diff --git a/DysonNetwork.Sphere/Account/AccountController.cs b/DysonNetwork.Sphere/Account/AccountController.cs index 267f96c..1277dad 100644 --- a/DysonNetwork.Sphere/Account/AccountController.cs +++ b/DysonNetwork.Sphere/Account/AccountController.cs @@ -1,5 +1,6 @@ using System.ComponentModel.DataAnnotations; using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; namespace DysonNetwork.Sphere.Account; @@ -12,8 +13,8 @@ public class AccountController(AppDatabase db) [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task> GetByName(string name) { - var account = await db.Accounts.FindAsync(name); - return account; + var account = await db.Accounts.Where(a => a.Name == name).FirstOrDefaultAsync(); + return account is null ? new NotFoundResult() : account; } public class AccountCreateRequest @@ -32,9 +33,9 @@ public class AccountController(AppDatabase db) { Name = request.Name, Nick = request.Nick, - Contacts = new List() + Contacts = new List { - new AccountContact + new() { Type = AccountContactType.Email, Content = request.Email diff --git a/DysonNetwork.Sphere/DysonNetwork.Sphere.csproj b/DysonNetwork.Sphere/DysonNetwork.Sphere.csproj index dbd07fd..7e32a5c 100644 --- a/DysonNetwork.Sphere/DysonNetwork.Sphere.csproj +++ b/DysonNetwork.Sphere/DysonNetwork.Sphere.csproj @@ -21,6 +21,8 @@ + + diff --git a/DysonNetwork.Sphere/Program.cs b/DysonNetwork.Sphere/Program.cs index db507e5..4c090f3 100644 --- a/DysonNetwork.Sphere/Program.cs +++ b/DysonNetwork.Sphere/Program.cs @@ -1,6 +1,7 @@ using System.Text.Json; using DysonNetwork.Sphere; using Microsoft.EntityFrameworkCore; +using Microsoft.OpenApi.Models; using NodaTime; using NodaTime.Serialization.SystemTextJson; @@ -22,12 +23,32 @@ builder.Services.AddControllers().AddJsonOptions(options => options.JsonSerializerOptions.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb); }); + +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(options => +{ + options.SwaggerDoc("v1", new OpenApiInfo + { + Version = "v1", + Title = "Solar Network API", + Description = "An open-source social network", + TermsOfService = new Uri("https://solsynth.dev/terms"), + License = new OpenApiLicense + { + Name = "APGLv3", + Url = new Uri("https://www.gnu.org/licenses/agpl-3.0.html") + } + }); +}); builder.Services.AddOpenApi(); var app = builder.Build(); if (app.Environment.IsDevelopment()) app.MapOpenApi(); +app.UseSwagger(); +app.UseSwaggerUI(); + using (var scope = app.Services.CreateScope()) { var db = scope.ServiceProvider.GetRequiredService(); diff --git a/DysonNetwork.sln.DotSettings.user b/DysonNetwork.sln.DotSettings.user index 8c0e8ef..c38d425 100644 --- a/DysonNetwork.sln.DotSettings.user +++ b/DysonNetwork.sln.DotSettings.user @@ -1,2 +1,4 @@  - ForceIncluded \ No newline at end of file + ForceIncluded + ForceIncluded + ForceIncluded \ No newline at end of file