diff options
author | crupest <crupest@outlook.com> | 2019-04-18 21:23:21 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2019-04-18 21:23:21 +0800 |
commit | b86c8cf5130d21ac56e733640cecd08945d30e6d (patch) | |
tree | 5b5a3b8d524cfc5f81b5af98de83d5070df4b68c /Timeline/Controllers | |
parent | 1cb92b8f2a98005b793c00e0191903c0792d540a (diff) | |
download | timeline-b86c8cf5130d21ac56e733640cecd08945d30e6d.tar.gz timeline-b86c8cf5130d21ac56e733640cecd08945d30e6d.tar.bz2 timeline-b86c8cf5130d21ac56e733640cecd08945d30e6d.zip |
Add user management REST api.
Diffstat (limited to 'Timeline/Controllers')
-rw-r--r-- | Timeline/Controllers/AdminUserController.cs | 83 | ||||
-rw-r--r-- | Timeline/Controllers/UserController.cs | 17 |
2 files changed, 83 insertions, 17 deletions
diff --git a/Timeline/Controllers/AdminUserController.cs b/Timeline/Controllers/AdminUserController.cs new file mode 100644 index 00000000..7cc8c150 --- /dev/null +++ b/Timeline/Controllers/AdminUserController.cs @@ -0,0 +1,83 @@ +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using System; +using System.Threading.Tasks; +using Timeline.Entities; +using Timeline.Services; + +namespace Timeline.Controllers +{ + [Route("admin")] + [Authorize(Roles = "admin")] + public class AdminUserController : Controller + { + private readonly IUserService _userService; + + public AdminUserController(IUserService userService) + { + _userService = userService; + } + + [HttpGet("users")] + public async Task<ActionResult<UserInfo[]>> List() + { + return Ok(await _userService.ListUsers()); + } + + [HttpGet("user/{username}")] + public async Task<IActionResult> Get([FromRoute] string username) + { + var user = await _userService.GetUser(username); + if (user == null) + { + return NotFound(); + } + return Ok(user); + } + + [HttpPut("user/{username}")] + public async Task<IActionResult> Put([FromBody] AdminUserEntityRequest request, [FromRoute] string username) + { + var result = await _userService.PutUser(username, request.Password, request.Roles); + switch (result) + { + case PutUserResult.Created: + return CreatedAtAction("Get", new { username }, AdminUserPutResponse.Created); + case PutUserResult.Modified: + return Ok(AdminUserPutResponse.Modified); + default: + throw new Exception("Unreachable code."); + } + } + + [HttpPatch("user/{username}")] + public async Task<IActionResult> Patch([FromBody] AdminUserEntityRequest request, [FromRoute] string username) + { + var result = await _userService.PatchUser(username, request.Password, request.Roles); + switch (result) + { + case PatchUserResult.Success: + return Ok(); + case PatchUserResult.NotExists: + return NotFound(); + default: + throw new Exception("Unreachable code."); + } + } + + [HttpDelete("user/{username}")] + public async Task<ActionResult<AdminUserDeleteResponse>> Delete([FromRoute] string username) + { + var result = await _userService.DeleteUser(username); + switch (result) + { + case DeleteUserResult.Success: + return Ok(AdminUserDeleteResponse.Success); + case DeleteUserResult.NotExists: + return Ok(AdminUserDeleteResponse.NotExists); + default: + throw new Exception("Uncreachable code."); + } + } + } +} diff --git a/Timeline/Controllers/UserController.cs b/Timeline/Controllers/UserController.cs index 147724c1..285e0146 100644 --- a/Timeline/Controllers/UserController.cs +++ b/Timeline/Controllers/UserController.cs @@ -1,7 +1,6 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; -using System; using System.Threading.Tasks; using Timeline.Entities; using Timeline.Services; @@ -71,21 +70,5 @@ namespace Timeline.Controllers UserInfo = result }); } - - [HttpPost("[action]")] - [Authorize(Roles = "admin")] - public async Task<ActionResult<CreateUserResponse>> CreateUser([FromBody] CreateUserRequest request) - { - var result = await _userService.CreateUser(request.Username, request.Password, request.Roles); - switch (result) - { - case CreateUserResult.Success: - return Ok(new CreateUserResponse { ReturnCode = CreateUserResponse.SuccessCode }); - case CreateUserResult.AlreadyExists: - return Ok(new CreateUserResponse { ReturnCode = CreateUserResponse.AlreadyExistsCode }); - default: - throw new Exception("Unreachable code."); - } - } } } |