From b67a26248d5dde4c3909c29b92b8a182248bdcc1 Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Wed, 23 Oct 2019 20:41:19 +0800 Subject: ... --- Timeline/Controllers/UserController.cs | 44 +++++++++++++--------------------- 1 file changed, 16 insertions(+), 28 deletions(-) (limited to 'Timeline/Controllers/UserController.cs') diff --git a/Timeline/Controllers/UserController.cs b/Timeline/Controllers/UserController.cs index b8d1d659..1771dc85 100644 --- a/Timeline/Controllers/UserController.cs +++ b/Timeline/Controllers/UserController.cs @@ -3,10 +3,11 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Localization; using Microsoft.Extensions.Logging; using System.Threading.Tasks; -using Timeline.Authenticate; +using Timeline.Authentication; using Timeline.Helpers; using Timeline.Models; using Timeline.Models.Http; +using Timeline.Models.Validation; using Timeline.Services; using static Timeline.Resources.Controllers.UserController; @@ -23,11 +24,6 @@ namespace Timeline public const int NotExist = 10020101; // dd = 01 } - public static class Put // cc = 02 - { - public const int BadUsername = 10020201; // dd = 01 - } - public static class Patch // cc = 03 { public const int NotExist = 10020301; // dd = 01 @@ -78,7 +74,7 @@ namespace Timeline.Controllers } [HttpGet("users/{username}"), AdminAuthorize] - public async Task> Get([FromRoute] string username) + public async Task> Get([FromRoute][Username] string username) { var user = await _userService.GetUser(username); if (user == null) @@ -90,32 +86,24 @@ namespace Timeline.Controllers } [HttpPut("users/{username}"), AdminAuthorize] - public async Task> Put([FromBody] UserPutRequest request, [FromRoute] string username) + public async Task> Put([FromBody] UserPutRequest request, [FromRoute][Username] string username) { - try - { - var result = await _userService.PutUser(username, request.Password, request.Administrator!.Value); - switch (result) - { - case PutResult.Create: - _logger.LogInformation(Log.Format(LogPutCreate, ("Username", username))); - return CreatedAtAction("Get", new { username }, CommonPutResponse.Create(_localizerFactory)); - case PutResult.Modify: - _logger.LogInformation(Log.Format(LogPutModify, ("Username", username))); - return Ok(CommonPutResponse.Modify(_localizerFactory)); - default: - throw new InvalidBranchException(); - } - } - catch (UsernameBadFormatException e) + var result = await _userService.PutUser(username, request.Password, request.Administrator!.Value); + switch (result) { - _logger.LogInformation(e, Log.Format(LogPutBadUsername, ("Username", username))); - return BadRequest(new CommonResponse(ErrorCodes.Http.User.Put.BadUsername, _localizer["ErrorPutBadUsername"])); + case PutResult.Create: + _logger.LogInformation(Log.Format(LogPutCreate, ("Username", username))); + return CreatedAtAction("Get", new { username }, CommonPutResponse.Create(_localizerFactory)); + case PutResult.Modify: + _logger.LogInformation(Log.Format(LogPutModify, ("Username", username))); + return Ok(CommonPutResponse.Modify(_localizerFactory)); + default: + throw new InvalidBranchException(); } } [HttpPatch("users/{username}"), AdminAuthorize] - public async Task Patch([FromBody] UserPatchRequest request, [FromRoute] string username) + public async Task Patch([FromBody] UserPatchRequest request, [FromRoute][Username] string username) { try { @@ -130,7 +118,7 @@ namespace Timeline.Controllers } [HttpDelete("users/{username}"), AdminAuthorize] - public async Task> Delete([FromRoute] string username) + public async Task> Delete([FromRoute][Username] string username) { try { -- cgit v1.2.3