aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Controllers
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2019-08-17 20:43:24 +0800
committerGitHub <noreply@github.com>2019-08-17 20:43:24 +0800
commit0630fd020ec11e343b787a18d70f1f13fdb350b3 (patch)
treececc5b93150f7b1e3be792de5c058433b5e40982 /Timeline/Controllers
parent2d33ed8dc93a99fd5f24a9d4b2c609ddf078d367 (diff)
parent1fcd4c0daace2532757f797be2e33768b9d2cccd (diff)
downloadtimeline-0630fd020ec11e343b787a18d70f1f13fdb350b3.tar.gz
timeline-0630fd020ec11e343b787a18d70f1f13fdb350b3.tar.bz2
timeline-0630fd020ec11e343b787a18d70f1f13fdb350b3.zip
Merge pull request #42 from crupest/change-username
Add change username api.
Diffstat (limited to 'Timeline/Controllers')
-rw-r--r--Timeline/Controllers/UserController.cs30
1 files changed, 29 insertions, 1 deletions
diff --git a/Timeline/Controllers/UserController.cs b/Timeline/Controllers/UserController.cs
index d38f96e1..bd13f0a3 100644
--- a/Timeline/Controllers/UserController.cs
+++ b/Timeline/Controllers/UserController.cs
@@ -22,7 +22,10 @@ namespace Timeline.Controllers
public const int Patch_NotExist = -3001;
- public const int ChangePassword_BadOldPassword = -4001;
+ public const int ChangeUsername_NotExist = -4001;
+ public const int ChangeUsername_AlreadyExist = -4002;
+
+ public const int ChangePassword_BadOldPassword = -5001;
}
private readonly ILogger<UserController> _logger;
@@ -108,6 +111,31 @@ namespace Timeline.Controllers
}
}
+ [HttpPost("userop/changeusername"), AdminAuthorize]
+ public async Task<IActionResult> ChangeUsername([FromBody] ChangeUsernameRequest request)
+ {
+ try
+ {
+ await _userService.ChangeUsername(request.OldUsername, request.NewUsername);
+ _logger.LogInformation(FormatLogMessage("A user changed username.",
+ Pair("Old Username", request.OldUsername), Pair("New Username", request.NewUsername)));
+ return Ok();
+ }
+ catch (UserNotExistException e)
+ {
+ _logger.LogInformation(e, FormatLogMessage("Attempt to change a non-existent user's username failed.",
+ Pair("Old Username", request.OldUsername), Pair("New Username", request.NewUsername)));
+ return BadRequest(new CommonResponse(ErrorCodes.ChangeUsername_NotExist, $"The user {request.OldUsername} does not exist."));
+ }
+ catch (UserAlreadyExistException e)
+ {
+ _logger.LogInformation(e, FormatLogMessage("Attempt to change a user's username to a existent one failed.",
+ Pair("Old Username", request.OldUsername), Pair("New Username", request.NewUsername)));
+ return BadRequest(new CommonResponse(ErrorCodes.ChangeUsername_AlreadyExist, $"The user {request.NewUsername} already exists."));
+ }
+ // there is no need to catch bad format exception because it is already checked in model validation.
+ }
+
[HttpPost("userop/changepassword"), Authorize]
public async Task<IActionResult> ChangePassword([FromBody] ChangePasswordRequest request)
{