Add swagger, start work on user api controller
This commit is contained in:
parent
40286250dc
commit
de2a3ec163
|
@ -4,11 +4,15 @@
|
|||
<TargetFramework>net7.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<NoWarn>$(NoWarn);1591</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="linq2db" Version="4.4.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="7.0.2" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="7.0.6" />
|
||||
<PackageReference Include="System.Data.SQLite" Version="1.0.115" />
|
||||
<PackageReference Include="System.Data.SQLite.Core.osx.arm64" Version="1.0.117" />
|
||||
</ItemGroup>
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
using System.Reflection;
|
||||
using AfRApay.Web.Backend;
|
||||
using LinqToDB.Data;
|
||||
|
||||
|
@ -6,8 +7,11 @@ Migrations.RunMigrations();
|
|||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
builder.Services.AddRazorPages();
|
||||
builder.Services.AddSwaggerGen(options => {
|
||||
var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
||||
options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
|
||||
});
|
||||
|
||||
#if (DEBUG)
|
||||
builder.Services.AddControllers().AddRazorRuntimeCompilation();
|
||||
|
@ -18,14 +22,13 @@ builder.Services.AddControllers();
|
|||
|
||||
var app = builder.Build();
|
||||
|
||||
if (!app.Environment.IsDevelopment()) {
|
||||
app.UseExceptionHandler("/Error");
|
||||
app.UseHsts();
|
||||
if (app.Environment.IsDevelopment()) {
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseStatusCodePagesWithReExecute("/error");
|
||||
app.UseStatusCodePagesWithReExecute("/Error");
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
app.UseStaticFiles();
|
||||
|
||||
app.UseRouting();
|
||||
|
|
15
AfRApay.Web/Controllers/Schema/ErrorResponse.cs
Normal file
15
AfRApay.Web/Controllers/Schema/ErrorResponse.cs
Normal file
|
@ -0,0 +1,15 @@
|
|||
using System.ComponentModel;
|
||||
using AfRApay.Web.Backend.Tables;
|
||||
using J = System.Text.Json.Serialization.JsonPropertyNameAttribute;
|
||||
|
||||
namespace AfRApay.Web.Controllers.Schema;
|
||||
|
||||
public class ErrorResponse {
|
||||
[J("status")] [DefaultValue("error")] public string Status { get; set; }
|
||||
[J("message")] public string Message { get; set; }
|
||||
|
||||
public ErrorResponse(string message) {
|
||||
Status = "error";
|
||||
Message = message;
|
||||
}
|
||||
}
|
15
AfRApay.Web/Controllers/Schema/UserResponse.cs
Normal file
15
AfRApay.Web/Controllers/Schema/UserResponse.cs
Normal file
|
@ -0,0 +1,15 @@
|
|||
using System.ComponentModel;
|
||||
using AfRApay.Web.Backend.Tables;
|
||||
using J = System.Text.Json.Serialization.JsonPropertyNameAttribute;
|
||||
|
||||
namespace AfRApay.Web.Controllers.Schema;
|
||||
|
||||
public class UserResponse {
|
||||
[J("data")] public User? Data { get; set; }
|
||||
[J("status")] [DefaultValue("success")] public string Status { get; set; } = "success";
|
||||
[J("message")] [DefaultValue("")] public string Message { get; set; } = "";
|
||||
|
||||
public UserResponse(User data) {
|
||||
Data = data;
|
||||
}
|
||||
}
|
37
AfRApay.Web/Controllers/UserController.cs
Normal file
37
AfRApay.Web/Controllers/UserController.cs
Normal file
|
@ -0,0 +1,37 @@
|
|||
using AfRApay.Web.Backend;
|
||||
using AfRApay.Web.Controllers.Schema;
|
||||
using LinqToDB;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Swashbuckle.AspNetCore.Filters;
|
||||
|
||||
namespace AfRApay.Web.Controllers;
|
||||
|
||||
[ApiController]
|
||||
public class UserController : Controller {
|
||||
/// <summary>
|
||||
/// Renames the specified user.
|
||||
/// </summary>
|
||||
/// <param name="uid">The ID of the user to be renamed</param>
|
||||
/// <param name="newName">The new name of the user</param>
|
||||
/// <response code="200">Returns 200 if user was renamed successfully</response>
|
||||
/// <response code="400">Returns 400 if newName isn't unique</response>
|
||||
/// <response code="404">Returns 404 if no user matching the UID was found</response>
|
||||
[HttpPut]
|
||||
[Produces("application/json")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(UserResponse))]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest, Type = typeof(ErrorResponse))]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound, Type = typeof(ErrorResponse))]
|
||||
[SwaggerResponseExample(400, typeof(new )]
|
||||
[Route("/api/user/{uid:int}/name")]
|
||||
public async Task<IActionResult> Rename(int uid, [FromQuery] string newName) {
|
||||
var db = new Database.DbConn();
|
||||
if (db.Users.Any(p => p.Id == uid)) {
|
||||
var user = db.Users.First(p => p.Id == uid);
|
||||
user.Nickname = newName;
|
||||
await db.UpdateAsync(user);
|
||||
return new OkObjectResult(new UserResponse(user));
|
||||
}
|
||||
|
||||
return NotFound(new ErrorResponse("Unknown user"));
|
||||
}
|
||||
}
|
|
@ -8,15 +8,6 @@
|
|||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": false,
|
||||
"applicationUrl": "https://0.0.0.0:7115;http://localhost:5296",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue