diff options
| author | crupest <crupest@outlook.com> | 2022-12-02 11:04:34 +0800 | 
|---|---|---|
| committer | crupest <crupest@outlook.com> | 2022-12-02 13:35:35 +0800 | 
| commit | 300842e81a99db6da103f146d33eb47f43efc683 (patch) | |
| tree | 2f80b57945408b8af83556a4efe5a92dea98e025 /docker/crupest-api/CrupestApi/CrupestApi.Commons | |
| parent | 49ada0b891a93cf0f1c0dfc2cb5b1d103019b7c8 (diff) | |
| download | crupest-300842e81a99db6da103f146d33eb47f43efc683.tar.gz crupest-300842e81a99db6da103f146d33eb47f43efc683.tar.bz2 crupest-300842e81a99db6da103f146d33eb47f43efc683.zip  | |
...
Diffstat (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Commons')
4 files changed, 71 insertions, 0 deletions
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/CrupestApi.Commons.csproj b/docker/crupest-api/CrupestApi/CrupestApi.Commons/CrupestApi.Commons.csproj new file mode 100644 index 0000000..72a1294 --- /dev/null +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/CrupestApi.Commons.csproj @@ -0,0 +1,9 @@ +<Project Sdk="Microsoft.NET.Sdk.Web">
 +
 +  <PropertyGroup>
 +    <TargetFramework>net7.0</TargetFramework>
 +    <Nullable>enable</Nullable>
 +    <ImplicitUsings>enable</ImplicitUsings>
 +  </PropertyGroup>
 +
 +</Project>
 diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Error.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Error.cs new file mode 100644 index 0000000..b298f7a --- /dev/null +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Error.cs @@ -0,0 +1,19 @@ +namespace CrupestApi.Commons; + +public class ErrorBody +{ +    public ErrorBody(string message) +    { +        Message = message; +    } + +    public string Message { get; set; } +} + +public static class CrupestApiErrorExtensions +{ +    public static async Task WriteErrorMessageAsync(this HttpResponse response, string message, int statusCode = 400, HttpResponseAction? beforeWriteBody = null, CancellationToken cancellationToken = default) +    { +        await response.WriteJsonAsync(new ErrorBody(message), statusCode: statusCode, beforeWriteBody, cancellationToken); +    } +} diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/HttpResponseAction.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/HttpResponseAction.cs new file mode 100644 index 0000000..4b76066 --- /dev/null +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/HttpResponseAction.cs @@ -0,0 +1,3 @@ +namespace CrupestApi.Commons; + +public delegate Task HttpResponseAction(HttpResponse response); diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Json.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Json.cs new file mode 100644 index 0000000..0ec3ff0 --- /dev/null +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Json.cs @@ -0,0 +1,40 @@ +using System.Text; +using System.Text.Json; + +namespace CrupestApi.Commons; + + +public static class CrupestApiJsonExtensions +{ +    public static IServiceCollection AddJsonOptions(this IServiceCollection services) +    { +        services.AddOptions<JsonSerializerOptions>(); +        services.Configure<JsonSerializerOptions>(config => +        { +            config.AllowTrailingCommas = true; +            config.PropertyNameCaseInsensitive = true; +            config.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; +        }); + +        return services; +    } + + +    public static async Task WriteJsonAsync<T>(this HttpResponse response, T bodyObject, int statusCode = 200, HttpResponseAction? beforeWriteBody = null, CancellationToken cancellationToken = default) +    { +        var jsonOptions = response.HttpContext.RequestServices.GetRequiredService<JsonSerializerOptions>(); +        byte[] json = JsonSerializer.SerializeToUtf8Bytes<T>(bodyObject, jsonOptions); + +        var byteCount = json.Length; +        response.StatusCode = statusCode; +        response.Headers.ContentType = "application/json; charset=utf-8"; +        response.Headers.ContentLength = byteCount; + +        if (beforeWriteBody is not null) +        { +            await beforeWriteBody(response); +        } + +        await response.Body.WriteAsync(json, cancellationToken); +    } +}  | 
