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 ++++++++++++++++++++++++++++ Timeline/Services/UserDetailService.cs | 9 ++++ Timeline/Startup.cs | 1 + 3 files changed, 85 insertions(+) create mode 100644 Timeline/Controllers/UserDetailController.cs (limited to 'Timeline') 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.")); + } + } + } +} diff --git a/Timeline/Services/UserDetailService.cs b/Timeline/Services/UserDetailService.cs index d1fdc040..0bb745f3 100644 --- a/Timeline/Services/UserDetailService.cs +++ b/Timeline/Services/UserDetailService.cs @@ -1,4 +1,5 @@ using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System; using System.Linq; @@ -89,4 +90,12 @@ namespace Timeline.Services _logger.LogInformation("An entity is updated in user_details."); } } + + public static class UserDetailServiceCollectionExtensions + { + public static void AddUserDetailService(this IServiceCollection services) + { + services.AddScoped(); + } + } } diff --git a/Timeline/Startup.cs b/Timeline/Startup.cs index 66f648c3..b5a5106b 100644 --- a/Timeline/Startup.cs +++ b/Timeline/Startup.cs @@ -46,6 +46,7 @@ namespace Timeline services.AddTransient(); services.AddUserAvatarService(); + services.AddUserDetailService(); var databaseConfig = Configuration.GetSection(nameof(DatabaseConfig)).Get(); -- cgit v1.2.3