From de2a3ec163a52c69f38fcfd50795df23b129fc3f Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Tue, 7 Feb 2023 23:00:25 +0100 Subject: [PATCH] Add swagger, start work on user api controller --- AfRApay.Web/AfRApay.Web.csproj | 4 ++ AfRApay.Web/Backend/Startup.cs | 15 +++++--- .../Controllers/Schema/ErrorResponse.cs | 15 ++++++++ .../Controllers/Schema/UserResponse.cs | 15 ++++++++ AfRApay.Web/Controllers/UserController.cs | 37 +++++++++++++++++++ AfRApay.Web/Properties/launchSettings.json | 9 ----- 6 files changed, 80 insertions(+), 15 deletions(-) create mode 100644 AfRApay.Web/Controllers/Schema/ErrorResponse.cs create mode 100644 AfRApay.Web/Controllers/Schema/UserResponse.cs create mode 100644 AfRApay.Web/Controllers/UserController.cs diff --git a/AfRApay.Web/AfRApay.Web.csproj b/AfRApay.Web/AfRApay.Web.csproj index f388055..76f5773 100644 --- a/AfRApay.Web/AfRApay.Web.csproj +++ b/AfRApay.Web/AfRApay.Web.csproj @@ -4,11 +4,15 @@ net7.0 enable enable + true + $(NoWarn);1591 + + diff --git a/AfRApay.Web/Backend/Startup.cs b/AfRApay.Web/Backend/Startup.cs index ca559d9..e2f1616 100644 --- a/AfRApay.Web/Backend/Startup.cs +++ b/AfRApay.Web/Backend/Startup.cs @@ -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(); diff --git a/AfRApay.Web/Controllers/Schema/ErrorResponse.cs b/AfRApay.Web/Controllers/Schema/ErrorResponse.cs new file mode 100644 index 0000000..ae5dd44 --- /dev/null +++ b/AfRApay.Web/Controllers/Schema/ErrorResponse.cs @@ -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; + } +} diff --git a/AfRApay.Web/Controllers/Schema/UserResponse.cs b/AfRApay.Web/Controllers/Schema/UserResponse.cs new file mode 100644 index 0000000..51b3a15 --- /dev/null +++ b/AfRApay.Web/Controllers/Schema/UserResponse.cs @@ -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; + } +} diff --git a/AfRApay.Web/Controllers/UserController.cs b/AfRApay.Web/Controllers/UserController.cs new file mode 100644 index 0000000..b32615a --- /dev/null +++ b/AfRApay.Web/Controllers/UserController.cs @@ -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 { + /// + /// Renames the specified user. + /// + /// The ID of the user to be renamed + /// The new name of the user + /// Returns 200 if user was renamed successfully + /// Returns 400 if newName isn't unique + /// Returns 404 if no user matching the UID was found + [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 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")); + } +} diff --git a/AfRApay.Web/Properties/launchSettings.json b/AfRApay.Web/Properties/launchSettings.json index abf2a67..bf742fb 100644 --- a/AfRApay.Web/Properties/launchSettings.json +++ b/AfRApay.Web/Properties/launchSettings.json @@ -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" - } } } }