From 1934e2d6ade6115bdb8f7f90f590b557ec96323d Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Thu, 22 Aug 2019 14:32:37 +0800 Subject: Add user detail controller. --- Timeline/Controllers/UserDetailController.cs | 75 ++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 Timeline/Controllers/UserDetailController.cs (limited to 'Timeline/Controllers') diff --git a/Timeline/Controllers/UserDetailController.cs b/Timeline/Controllers/UserDetailController.cs new file mode 100644 index 00000000..9e1d5483 --- /dev/null +++ b/Timeline/Controllers/UserDetailController.cs @@ -0,0 +1,75 @@ +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}/details")] + [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; + + } + + private readonly ILogger _logger; + private readonly IUserDetailService _service; + + public UserDetailController(ILogger logger, IUserDetailService service) + { + _logger = logger; + _service = service; + } + + [HttpGet()] + [UserAuthorize] + [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(UserDetail))] + [ProducesResponseType(StatusCodes.Status404NotFound)] + public async Task 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()] + [Authorize] + [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(void))] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status403Forbidden)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + public async Task 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.")); + } + } + } +} -- cgit v1.2.3 From 0f4b5672fd0bc10d52a6ea98fc8dae209852f7c0 Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Thu, 22 Aug 2019 15:06:15 +0800 Subject: Add nickname in detail controller. --- Timeline.Tests/IntegratedTests/UserDetailTest.cs | 33 ++++++++++++++++++++---- Timeline/Controllers/UserDetailController.cs | 27 ++++++++++++++++--- 2 files changed, 52 insertions(+), 8 deletions(-) (limited to 'Timeline/Controllers') diff --git a/Timeline.Tests/IntegratedTests/UserDetailTest.cs b/Timeline.Tests/IntegratedTests/UserDetailTest.cs index 571f200f..4923cd06 100644 --- a/Timeline.Tests/IntegratedTests/UserDetailTest.cs +++ b/Timeline.Tests/IntegratedTests/UserDetailTest.cs @@ -1,8 +1,6 @@ using FluentAssertions; using Microsoft.AspNetCore.Mvc.Testing; using System; -using System.Collections.Generic; -using System.Linq; using System.Net; using System.Threading.Tasks; using Timeline.Controllers; @@ -36,6 +34,12 @@ namespace Timeline.Tests.IntegratedTests { using (var client = await _factory.CreateClientAsUser()) { + { + var res = await client.GetAsync($"users/usernotexist/nickname"); + res.Should().HaveStatusCodeNotFound() + .And.Should().HaveBodyAsCommonResponseWithCode(UserDetailController.ErrorCodes.GetNickname_UserNotExist); + } + { var res = await client.GetAsync($"users/usernotexist/details"); res.Should().HaveStatusCodeNotFound() @@ -61,15 +65,24 @@ namespace Timeline.Tests.IntegratedTests { var res = await client.PatchAsJsonAsync($"users/{MockUsers.UserUsername}/details", new UserDetail { + Nickname = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", QQ = "aaaaaaa", - EMail = "aaaaaa" + EMail = "aaaaaa", + PhoneNumber = "aaaaaaaa" }); - res.Should().HaveStatusCode(HttpStatusCode.BadRequest) - .And.Should().HaveBodyAsCommonResponseWithCode(CommonResponse.ErrorCodes.InvalidModel); + var body = res.Should().HaveStatusCode(HttpStatusCode.BadRequest) + .And.Should().HaveBodyAsCommonResponse().Which; + body.Code.Should().Be(CommonResponse.ErrorCodes.InvalidModel); + foreach (var key in new string[] { "nickname", "qq", "email", "phonenumber" }) + { + body.Message.Should().ContainEquivalentOf(key); + } } + var detail = new UserDetail { + Nickname = "aaa", QQ = "1234567", EMail = "aaaa@aaa.net", Description = "aaaaaaaaa" @@ -81,6 +94,15 @@ namespace Timeline.Tests.IntegratedTests await GetAndTest(detail); } + { + var res = await client.GetAsync($"users/{MockUsers.UserUsername}/nickname"); + res.Should().HaveStatusCodeOk().And.Should().HaveBodyAsJson() + .Which.Should().BeEquivalentTo(new UserDetail + { + Nickname = detail.Nickname + }); + } + var detail2 = new UserDetail { QQ = "", @@ -93,6 +115,7 @@ namespace Timeline.Tests.IntegratedTests res.Should().HaveStatusCodeOk(); await GetAndTest(new UserDetail { + Nickname = detail.Nickname, QQ = null, EMail = detail.EMail, PhoneNumber = detail2.PhoneNumber, diff --git a/Timeline/Controllers/UserDetailController.cs b/Timeline/Controllers/UserDetailController.cs index 9e1d5483..5e1183c1 100644 --- a/Timeline/Controllers/UserDetailController.cs +++ b/Timeline/Controllers/UserDetailController.cs @@ -10,7 +10,7 @@ using Timeline.Services; namespace Timeline.Controllers { - [Route("users/{username}/details")] + [Route("users/{username}")] [ProducesErrorResponseType(typeof(CommonResponse))] [ApiController] public class UserDetailController : Controller @@ -22,6 +22,7 @@ namespace Timeline.Controllers public const int Patch_Forbid = -2001; public const int Patch_UserNotExist = -2002; + public const int GetNickname_UserNotExist = -3001; } private readonly ILogger _logger; @@ -33,7 +34,27 @@ namespace Timeline.Controllers _service = service; } - [HttpGet()] + [HttpGet("nickname")] + [UserAuthorize] + [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(UserDetail))] + [ProducesResponseType(StatusCodes.Status404NotFound)] + public async Task 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)] @@ -50,7 +71,7 @@ namespace Timeline.Controllers } } - [HttpPatch()] + [HttpPatch("details")] [Authorize] [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(void))] [ProducesResponseType(StatusCodes.Status400BadRequest)] -- cgit v1.2.3