aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Controllers
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-01-30 23:49:02 +0800
committercrupest <crupest@outlook.com>2020-01-30 23:49:02 +0800
commit49bd47fb0eb81a88cce135f7ff7c25637790e63b (patch)
tree70ddc26cf29bdf4b19e9a746cac6017845e009fc /Timeline/Controllers
parenta1a2eede6942aef7d8c20f9e7fb25f53b2b63d86 (diff)
downloadtimeline-49bd47fb0eb81a88cce135f7ff7c25637790e63b.tar.gz
timeline-49bd47fb0eb81a88cce135f7ff7c25637790e63b.tar.bz2
timeline-49bd47fb0eb81a88cce135f7ff7c25637790e63b.zip
Finish reafctor, TODO: Database migration.
Diffstat (limited to 'Timeline/Controllers')
-rw-r--r--Timeline/Controllers/ControllerAuthExtensions.cs2
-rw-r--r--Timeline/Controllers/UserAvatarController.cs2
-rw-r--r--Timeline/Controllers/UserController.cs18
3 files changed, 12 insertions, 10 deletions
diff --git a/Timeline/Controllers/ControllerAuthExtensions.cs b/Timeline/Controllers/ControllerAuthExtensions.cs
index 90da8a93..34fd4d99 100644
--- a/Timeline/Controllers/ControllerAuthExtensions.cs
+++ b/Timeline/Controllers/ControllerAuthExtensions.cs
@@ -34,7 +34,7 @@ namespace Timeline.Controllers
var claim = controller.User.FindFirst(ClaimTypes.NameIdentifier);
if (claim == null)
- throw new InvalidOperationException("Failed to get user id because User has no NameIdentifier claim.");
+ return null;
if (long.TryParse(claim.Value, out var value))
return value;
diff --git a/Timeline/Controllers/UserAvatarController.cs b/Timeline/Controllers/UserAvatarController.cs
index ab0ad8e7..2dd279a8 100644
--- a/Timeline/Controllers/UserAvatarController.cs
+++ b/Timeline/Controllers/UserAvatarController.cs
@@ -78,7 +78,7 @@ namespace Timeline.Controllers
[HttpPut("users/{username}/avatar")]
[Authorize]
- [RequireContentLength]
+ [RequireContentType, RequireContentLength]
[Consumes("image/png", "image/jpeg", "image/gif", "image/webp")]
public async Task<IActionResult> Put([FromRoute][Username] string username)
{
diff --git a/Timeline/Controllers/UserController.cs b/Timeline/Controllers/UserController.cs
index 400a518c..fa73c6f9 100644
--- a/Timeline/Controllers/UserController.cs
+++ b/Timeline/Controllers/UserController.cs
@@ -42,7 +42,13 @@ namespace Timeline.Controllers
{
var users = await _userService.GetUsers();
var administrator = this.IsAdministrator();
- return Ok(users.Select(u => ConvertToUserInfo(u, administrator)).ToArray());
+ // Note: the (object) explicit conversion. If not convert,
+ // then result is a IUserInfo array and JsonSerializer will
+ // treat all element as IUserInfo and deserialize only properties
+ // in IUserInfo. So we convert it to object to make an object
+ // array so that JsonSerializer use the runtime type.
+ var result = users.Select(u => (object)ConvertToUserInfo(u, administrator)).ToArray();
+ return Ok(result);
}
[HttpGet("users/{username}")]
@@ -106,15 +112,11 @@ namespace Timeline.Controllers
[HttpDelete("users/{username}"), AdminAuthorize]
public async Task<ActionResult<CommonDeleteResponse>> Delete([FromRoute][Username] string username)
{
- try
- {
- await _userService.DeleteUser(username);
+ var delete = await _userService.DeleteUser(username);
+ if (delete)
return Ok(CommonDeleteResponse.Delete());
- }
- catch (UserNotExistException)
- {
+ else
return Ok(CommonDeleteResponse.NotExist());
- }
}
[HttpPost("userop/createuser"), AdminAuthorize]