From ee419812021f8b2e8e35997750662e56c9db613a Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 19 Jan 2021 15:38:03 +0800 Subject: feat: Deprecate userop/createuser api and add users post api. --- .../Timeline.Tests/IntegratedTests/SearchTest.cs | 6 ++-- BackEnd/Timeline.Tests/IntegratedTests/UserTest.cs | 2 +- BackEnd/Timeline/Controllers/UserController.cs | 34 ++++++++++++++++------ 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/BackEnd/Timeline.Tests/IntegratedTests/SearchTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/SearchTest.cs index f96acfea..4979224f 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/SearchTest.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/SearchTest.cs @@ -41,10 +41,10 @@ namespace Timeline.Tests.IntegratedTests var client = await CreateClientAsAdministrator(); { - await client.TestPostAsync("userop/createuser", new HttpCreateUserRequest { Username = "hahaha", Password = "p" }); - await client.TestPostAsync("userop/createuser", new HttpCreateUserRequest { Username = "bababa", Password = "p" }); + await client.TestPostAsync("users", new HttpCreateUserRequest { Username = "hahaha", Password = "p" }); + await client.TestPostAsync("users", new HttpCreateUserRequest { Username = "bababa", Password = "p" }); await client.TestPatchAsync("users/bababa", new HttpUserPatchRequest { Nickname = "hahaha" }); - await client.TestPostAsync("userop/createuser", new HttpCreateUserRequest { Username = "gagaga", Password = "p" }); + await client.TestPostAsync("users", new HttpCreateUserRequest { Username = "gagaga", Password = "p" }); } { diff --git a/BackEnd/Timeline.Tests/IntegratedTests/UserTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/UserTest.cs index 56dbf92a..664a0604 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/UserTest.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/UserTest.cs @@ -207,7 +207,7 @@ namespace Timeline.Tests.IntegratedTests await client.TestDeleteAssertForbiddenAsync("users/aaa!a"); } - private const string createUserUrl = "userop/createuser"; + private const string createUserUrl = "users"; [Fact] public async Task Op_CreateUser() diff --git a/BackEnd/Timeline/Controllers/UserController.cs b/BackEnd/Timeline/Controllers/UserController.cs index e1a9d454..4091174c 100644 --- a/BackEnd/Timeline/Controllers/UserController.cs +++ b/BackEnd/Timeline/Controllers/UserController.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; +using System; using System.Collections.Generic; using System.Threading.Tasks; using Timeline.Auth; @@ -59,6 +60,28 @@ namespace Timeline.Controllers return result; } + /// + /// Create a new user. You have to be administrator. + /// + /// The new user's info. + [HttpPost("users"), PermissionAuthorize(UserPermission.UserManagement)] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(StatusCodes.Status403Forbidden)] + public async Task> Post([FromBody] HttpCreateUserRequest body) + { + try + { + var user = await _userService.CreateUser(body.Username, body.Password); + return await _userMapper.MapToHttp(user, Url); + } + catch (EntityAlreadyExistException e) when (e.EntityName == EntityNames.User) + { + return BadRequest(ErrorResponse.UserController.UsernameConflict()); + } + } + /// /// Get a user's info. /// @@ -168,17 +191,10 @@ namespace Timeline.Controllers [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status403Forbidden)] + [Obsolete("Use post instead.")] public async Task> CreateUser([FromBody] HttpCreateUserRequest body) { - try - { - var user = await _userService.CreateUser(body.Username, body.Password); - return await _userMapper.MapToHttp(user, Url); - } - catch (EntityAlreadyExistException e) when (e.EntityName == EntityNames.User) - { - return BadRequest(ErrorResponse.UserController.UsernameConflict()); - } + return await Post(body); } /// -- cgit v1.2.3