diff options
author | 杨宇千 <crupest@outlook.com> | 2019-08-22 15:29:03 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-22 15:29:03 +0800 |
commit | 11f01c56b4ea1dbb09d04258bec89f800c6ee2b6 (patch) | |
tree | af83f8596b4fa78713733c0db6b4b6d1695d0ff0 /Timeline/Controllers | |
parent | fd95f9abc017575b13a31dd16ac72ef663e984d6 (diff) | |
parent | 96c18fb2e17c94ff04094608c705db087400f510 (diff) | |
download | timeline-11f01c56b4ea1dbb09d04258bec89f800c6ee2b6.tar.gz timeline-11f01c56b4ea1dbb09d04258bec89f800c6ee2b6.tar.bz2 timeline-11f01c56b4ea1dbb09d04258bec89f800c6ee2b6.zip |
Merge pull request #48 from crupest/user-details
Add user details.
Diffstat (limited to 'Timeline/Controllers')
-rw-r--r-- | Timeline/Controllers/UserDetailController.cs | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/Timeline/Controllers/UserDetailController.cs b/Timeline/Controllers/UserDetailController.cs new file mode 100644 index 00000000..5e1183c1 --- /dev/null +++ b/Timeline/Controllers/UserDetailController.cs @@ -0,0 +1,96 @@ +using Microsoft.AspNetCore.Authorization;
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Logging;
+using System.Threading.Tasks;
+using Timeline.Authenticate;
+using Timeline.Models;
+using Timeline.Models.Http;
+using Timeline.Services;
+
+namespace Timeline.Controllers
+{
+ [Route("users/{username}")]
+ [ProducesErrorResponseType(typeof(CommonResponse))]
+ [ApiController]
+ public class UserDetailController : Controller
+ {
+ public static class ErrorCodes
+ {
+ public const int Get_UserNotExist = -1001;
+
+ public const int Patch_Forbid = -2001;
+ public const int Patch_UserNotExist = -2002;
+
+ public const int GetNickname_UserNotExist = -3001;
+ }
+
+ private readonly ILogger<UserDetailController> _logger;
+ private readonly IUserDetailService _service;
+
+ public UserDetailController(ILogger<UserDetailController> logger, IUserDetailService service)
+ {
+ _logger = logger;
+ _service = service;
+ }
+
+ [HttpGet("nickname")]
+ [UserAuthorize]
+ [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(UserDetail))]
+ [ProducesResponseType(StatusCodes.Status404NotFound)]
+ public async Task<IActionResult> GetNickname([FromRoute] string username)
+ {
+ try
+ {
+ var nickname = await _service.GetUserNickname(username);
+ return Ok(new UserDetail
+ {
+ Nickname = nickname
+ });
+ }
+ catch (UserNotExistException)
+ {
+ return NotFound(new CommonResponse(ErrorCodes.GetNickname_UserNotExist, "The user does not exist."));
+ }
+ }
+
+ [HttpGet("details")]
+ [UserAuthorize]
+ [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(UserDetail))]
+ [ProducesResponseType(StatusCodes.Status404NotFound)]
+ public async Task<IActionResult> Get([FromRoute] string username)
+ {
+ try
+ {
+ var detail = await _service.GetUserDetail(username);
+ return Ok(detail);
+ }
+ catch (UserNotExistException)
+ {
+ return NotFound(new CommonResponse(ErrorCodes.Get_UserNotExist, "The user does not exist."));
+ }
+ }
+
+ [HttpPatch("details")]
+ [Authorize]
+ [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(void))]
+ [ProducesResponseType(StatusCodes.Status400BadRequest)]
+ [ProducesResponseType(StatusCodes.Status403Forbidden)]
+ [ProducesResponseType(StatusCodes.Status404NotFound)]
+ public async Task<IActionResult> Patch([FromRoute] string username, [FromBody] UserDetail detail)
+ {
+ if (!User.IsAdmin() && User.Identity.Name != username)
+ return StatusCode(StatusCodes.Status403Forbidden, new CommonResponse(ErrorCodes.Patch_Forbid, "You can't change other's details unless you are admin."));
+
+ try
+ {
+ await _service.UpdateUserDetail(username, detail);
+ return Ok();
+ }
+ catch (UserNotExistException)
+ {
+ return NotFound(new CommonResponse(ErrorCodes.Patch_UserNotExist, "The user does not exist."));
+ }
+ }
+ }
+}
|