From bf4c48980f81e566065c07f3fe534ce7551ebdcc Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 29 Apr 2021 19:29:35 +0800 Subject: ... --- .../Controllers/BookmarkTimelineController.cs | 8 ++-- .../Controllers/HighlightTimelineController.cs | 8 ++-- BackEnd/Timeline/Controllers/SearchController.cs | 4 +- BackEnd/Timeline/Controllers/TimelineController.cs | 3 -- .../Timeline/Controllers/TimelinePostController.cs | 3 -- BackEnd/Timeline/Controllers/TokenController.cs | 52 +++------------------- .../Timeline/Controllers/UserAvatarController.cs | 36 +++------------ BackEnd/Timeline/Controllers/UserController.cs | 24 +++------- 8 files changed, 31 insertions(+), 107 deletions(-) (limited to 'BackEnd/Timeline/Controllers') diff --git a/BackEnd/Timeline/Controllers/BookmarkTimelineController.cs b/BackEnd/Timeline/Controllers/BookmarkTimelineController.cs index e7ffa5c5..cbc96fc6 100644 --- a/BackEnd/Timeline/Controllers/BookmarkTimelineController.cs +++ b/BackEnd/Timeline/Controllers/BookmarkTimelineController.cs @@ -44,7 +44,7 @@ namespace Timeline.Controllers [ProducesResponseType(401)] public async Task>> List() { - var ids = await _service.GetBookmarks(this.GetUserId()); + var ids = await _service.GetBookmarksAsync(this.GetUserId()); var timelines = await _timelineService.GetTimelineList(ids); return await Map(timelines); } @@ -63,7 +63,7 @@ namespace Timeline.Controllers try { var timelineId = await _timelineService.GetTimelineIdByNameAsync(timeline); - var create = await _service.AddBookmark(this.GetUserId(), timelineId); + var create = await _service.AddBookmarkAsync(this.GetUserId(), timelineId); return CommonPutResponse.Create(create); } catch (TimelineNotExistException) @@ -86,7 +86,7 @@ namespace Timeline.Controllers try { var timelineId = await _timelineService.GetTimelineIdByNameAsync(timeline); - var delete = await _service.RemoveBookmark(this.GetUserId(), timelineId); + var delete = await _service.RemoveBookmarkAsync(this.GetUserId(), timelineId); return CommonDeleteResponse.Create(delete); } catch (TimelineNotExistException) @@ -109,7 +109,7 @@ namespace Timeline.Controllers try { var timelineId = await _timelineService.GetTimelineIdByNameAsync(request.Timeline); - await _service.MoveBookmark(this.GetUserId(), timelineId, request.NewPosition!.Value); + await _service.MoveBookmarkAsync(this.GetUserId(), timelineId, request.NewPosition!.Value); return Ok(); } catch (TimelineNotExistException) diff --git a/BackEnd/Timeline/Controllers/HighlightTimelineController.cs b/BackEnd/Timeline/Controllers/HighlightTimelineController.cs index 4e739056..ffaa50c1 100644 --- a/BackEnd/Timeline/Controllers/HighlightTimelineController.cs +++ b/BackEnd/Timeline/Controllers/HighlightTimelineController.cs @@ -43,7 +43,7 @@ namespace Timeline.Controllers [ProducesResponseType(200)] public async Task>> List() { - var ids = await _service.GetHighlightTimelines(); + var ids = await _service.GetHighlightTimelinesAsync(); var timelines = await _timelineService.GetTimelineList(ids); return await Map(timelines); } @@ -63,7 +63,7 @@ namespace Timeline.Controllers try { var timelineId = await _timelineService.GetTimelineIdByNameAsync(timeline); - var create = await _service.AddHighlightTimeline(timelineId, this.GetUserId()); + var create = await _service.AddHighlightTimelineAsync(timelineId, this.GetUserId()); return CommonPutResponse.Create(create); } catch (TimelineNotExistException) @@ -87,7 +87,7 @@ namespace Timeline.Controllers try { var timelineId = await _timelineService.GetTimelineIdByNameAsync(timeline); - var delete = await _service.RemoveHighlightTimeline(timelineId, this.GetUserId()); + var delete = await _service.RemoveHighlightTimelineAsync(timelineId, this.GetUserId()); return CommonDeleteResponse.Create(delete); } catch (TimelineNotExistException) @@ -110,7 +110,7 @@ namespace Timeline.Controllers try { var timelineId = await _timelineService.GetTimelineIdByNameAsync(body.Timeline); - await _service.MoveHighlightTimeline(timelineId, body.NewPosition!.Value); + await _service.MoveHighlightTimelineAsync(timelineId, body.NewPosition!.Value); return Ok(); } catch (TimelineNotExistException) diff --git a/BackEnd/Timeline/Controllers/SearchController.cs b/BackEnd/Timeline/Controllers/SearchController.cs index 76f3d8f2..cd085e5b 100644 --- a/BackEnd/Timeline/Controllers/SearchController.cs +++ b/BackEnd/Timeline/Controllers/SearchController.cs @@ -42,7 +42,7 @@ namespace Timeline.Controllers [ProducesResponseType(400)] public async Task>> TimelineSearch([FromQuery(Name = "q"), Required(AllowEmptyStrings = false)] string query) { - var searchResult = await _service.SearchTimeline(query); + var searchResult = await _service.SearchTimelineAsync(query); var timelines = searchResult.Items.Select(i => i.Item).ToList(); return await Map(timelines); } @@ -57,7 +57,7 @@ namespace Timeline.Controllers [ProducesResponseType(400)] public async Task>> UserSearch([FromQuery(Name = "q"), Required(AllowEmptyStrings = false)] string query) { - var searchResult = await _service.SearchUser(query); + var searchResult = await _service.SearchUserAsync(query); var users = searchResult.Items.Select(i => i.Item).ToList(); return await _mapper.MapListAsync(users, Url, User); } diff --git a/BackEnd/Timeline/Controllers/TimelineController.cs b/BackEnd/Timeline/Controllers/TimelineController.cs index 497d7893..3fd0f2ac 100644 --- a/BackEnd/Timeline/Controllers/TimelineController.cs +++ b/BackEnd/Timeline/Controllers/TimelineController.cs @@ -29,9 +29,6 @@ namespace Timeline.Controllers private readonly ITimelineService _service; private readonly IGenericMapper _mapper; - /// - /// - /// public TimelineController(IUserService userService, ITimelineService service, IGenericMapper mapper) { _userService = userService; diff --git a/BackEnd/Timeline/Controllers/TimelinePostController.cs b/BackEnd/Timeline/Controllers/TimelinePostController.cs index 2e1ed3a9..2d3e5423 100644 --- a/BackEnd/Timeline/Controllers/TimelinePostController.cs +++ b/BackEnd/Timeline/Controllers/TimelinePostController.cs @@ -35,9 +35,6 @@ namespace Timeline.Controllers private readonly MarkdownProcessor _markdownProcessor; - /// - /// - /// public TimelinePostController(ITimelineService timelineService, ITimelinePostService timelinePostService, IGenericMapper mapper, MarkdownProcessor markdownProcessor) { _timelineService = timelineService; diff --git a/BackEnd/Timeline/Controllers/TokenController.cs b/BackEnd/Timeline/Controllers/TokenController.cs index e728ae6d..3d4e9444 100644 --- a/BackEnd/Timeline/Controllers/TokenController.cs +++ b/BackEnd/Timeline/Controllers/TokenController.cs @@ -1,17 +1,13 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Logging; using System; -using System.Globalization; using System.Threading.Tasks; -using Timeline.Helpers; using Timeline.Models.Http; using Timeline.Services; using Timeline.Services.Mapper; using Timeline.Services.Token; using Timeline.Services.User; -using static Timeline.Resources.Controllers.TokenController; namespace Timeline.Controllers { @@ -24,15 +20,12 @@ namespace Timeline.Controllers public class TokenController : Controller { private readonly IUserTokenManager _userTokenManager; - private readonly ILogger _logger; private readonly IGenericMapper _mapper; private readonly IClock _clock; - /// - public TokenController(IUserTokenManager userTokenManager, ILogger logger, IGenericMapper mapper, IClock clock) + public TokenController(IUserTokenManager userTokenManager, IGenericMapper mapper, IClock clock) { _userTokenManager = userTokenManager; - _logger = logger; _mapper = mapper; _clock = clock; } @@ -47,15 +40,6 @@ namespace Timeline.Controllers [ProducesResponseType(StatusCodes.Status400BadRequest)] public async Task> Create([FromBody] HttpCreateTokenRequest request) { - void LogFailure(string reason, Exception? e = null) - { - _logger.LogInformation(e, Log.Format(LogCreateFailure, - ("Reason", reason), - ("Username", request.Username), - ("Password", request.Password), - ("Expire (in days)", request.Expire) - )); - } try { @@ -65,24 +49,18 @@ namespace Timeline.Controllers var result = await _userTokenManager.CreateTokenAsync(request.Username, request.Password, expireTime); - _logger.LogInformation(Log.Format(LogCreateSuccess, - ("Username", request.Username), - ("Expire At", expireTime?.ToString(CultureInfo.CurrentCulture.DateTimeFormat) ?? "default") - )); return new HttpCreateTokenResponse { Token = result.Token, User = await _mapper.MapAsync(result.User, Url, User) }; } - catch (UserNotExistException e) + catch (UserNotExistException) { - LogFailure(LogUserNotExist, e); return BadRequest(ErrorResponse.TokenController.Create_BadCredential()); } - catch (BadPasswordException e) + catch (BadPasswordException) { - LogFailure(LogBadPassword, e); return BadRequest(ErrorResponse.TokenController.Create_BadCredential()); } } @@ -97,44 +75,28 @@ namespace Timeline.Controllers [ProducesResponseType(StatusCodes.Status400BadRequest)] public async Task> Verify([FromBody] HttpVerifyTokenRequest request) { - void LogFailure(string reason, Exception? e = null, params (string, object?)[] otherProperties) - { - var properties = new (string, object?)[2 + otherProperties.Length]; - properties[0] = ("Reason", reason); - properties[1] = ("Token", request.Token); - otherProperties.CopyTo(properties, 2); - _logger.LogInformation(e, Log.Format(LogVerifyFailure, properties)); - } - try { var result = await _userTokenManager.VerifyTokenAsync(request.Token); - _logger.LogInformation(Log.Format(LogVerifySuccess, - ("Username", result.Username), ("Token", request.Token))); return new HttpVerifyTokenResponse { User = await _mapper.MapAsync(result, Url, User) }; } - catch (UserTokenTimeExpiredException e) + catch (UserTokenTimeExpiredException) { - LogFailure(LogVerifyExpire, e, ("Expire Time", e.ExpireTime), ("Verify Time", e.VerifyTime)); return BadRequest(ErrorResponse.TokenController.Verify_TimeExpired()); } - catch (UserTokenVersionExpiredException e) + catch (UserTokenVersionExpiredException) { - LogFailure(LogVerifyOldVersion, e, ("Token Version", e.TokenVersion), ("Required Version", e.RequiredVersion)); return BadRequest(ErrorResponse.TokenController.Verify_OldVersion()); - } - catch (UserTokenBadFormatException e) + catch (UserTokenBadFormatException) { - LogFailure(LogVerifyBadFormat, e); return BadRequest(ErrorResponse.TokenController.Verify_BadFormat()); } - catch (UserTokenUserNotExistException e) + catch (UserTokenUserNotExistException) { - LogFailure(LogVerifyUserNotExist, e); return BadRequest(ErrorResponse.TokenController.Verify_UserNotExist()); } } diff --git a/BackEnd/Timeline/Controllers/UserAvatarController.cs b/BackEnd/Timeline/Controllers/UserAvatarController.cs index c280f033..b1129329 100644 --- a/BackEnd/Timeline/Controllers/UserAvatarController.cs +++ b/BackEnd/Timeline/Controllers/UserAvatarController.cs @@ -1,11 +1,9 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Logging; using System; using System.Threading.Tasks; using Timeline.Filters; -using Timeline.Helpers; using Timeline.Helpers.Cache; using Timeline.Models; using Timeline.Models.Http; @@ -13,7 +11,6 @@ using Timeline.Models.Validation; using Timeline.Services.Imaging; using Timeline.Services.User; using Timeline.Services.User.Avatar; -using static Timeline.Resources.Controllers.UserAvatarController; namespace Timeline.Controllers { @@ -24,17 +21,11 @@ namespace Timeline.Controllers [ProducesErrorResponseType(typeof(CommonResponse))] public class UserAvatarController : Controller { - private readonly ILogger _logger; - private readonly IUserService _userService; private readonly IUserAvatarService _service; - /// - /// - /// - public UserAvatarController(ILogger logger, IUserService userService, IUserAvatarService service) + public UserAvatarController(IUserService userService, IUserAvatarService service) { - _logger = logger; _userService = userService; _service = service; } @@ -53,18 +44,16 @@ namespace Timeline.Controllers public async Task Get([FromRoute][Username] string username, [FromHeader(Name = "If-None-Match")] string? ifNoneMatch) { _ = ifNoneMatch; - long id; try { - id = await _userService.GetUserIdByUsernameAsync(username); + long userId = await _userService.GetUserIdByUsernameAsync(username); + return await DataCacheHelper.GenerateActionResult(this, () => _service.GetAvatarDigestAsync(userId), () => _service.GetAvatarAsync(userId)); } - catch (UserNotExistException e) + catch (UserNotExistException) { - _logger.LogInformation(e, Log.Format(LogGetUserNotExist, ("Username", username))); return NotFound(ErrorResponse.UserCommon.NotExist()); } - return await DataCacheHelper.GenerateActionResult(this, () => _service.GetAvatarDigestAsync(id), () => _service.GetAvatarAsync(id)); } /// @@ -84,8 +73,6 @@ namespace Timeline.Controllers { if (!this.UserHasPermission(UserPermission.UserManagement) && User.Identity!.Name != username) { - _logger.LogInformation(Log.Format(LogPutForbid, - ("Operator Username", User.Identity.Name), ("Username To Put Avatar", username))); return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); } @@ -94,9 +81,8 @@ namespace Timeline.Controllers { id = await _userService.GetUserIdByUsernameAsync(username); } - catch (UserNotExistException e) + catch (UserNotExistException) { - _logger.LogInformation(e, Log.Format(LogPutUserNotExist, ("Username", username))); return BadRequest(ErrorResponse.UserCommon.NotExist()); } @@ -104,23 +90,18 @@ namespace Timeline.Controllers { var digest = await _service.SetAvatarAsync(id, body); - _logger.LogInformation(Log.Format(LogPutSuccess, - ("Username", username), ("Mime Type", Request.ContentType))); - Response.Headers.Append("ETag", $"\"{digest.ETag}\""); return Ok(); } catch (ImageException e) { - _logger.LogInformation(e, Log.Format(LogPutUserBadFormat, ("Username", username))); return BadRequest(e.Error switch { ImageException.ErrorReason.CantDecode => ErrorResponse.UserAvatar.BadFormat_CantDecode(), ImageException.ErrorReason.UnmatchedFormat => ErrorResponse.UserAvatar.BadFormat_UnmatchedFormat(), ImageException.ErrorReason.BadSize => ErrorResponse.UserAvatar.BadFormat_BadSize(), - _ => - throw new Exception(ExceptionUnknownAvatarFormatError) + _ => throw new Exception() }); } } @@ -143,8 +124,6 @@ namespace Timeline.Controllers { if (!this.UserHasPermission(UserPermission.UserManagement) && User.Identity!.Name != username) { - _logger.LogInformation(Log.Format(LogDeleteForbid, - ("Operator Username", User.Identity!.Name), ("Username To Delete Avatar", username))); return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); } @@ -153,9 +132,8 @@ namespace Timeline.Controllers { id = await _userService.GetUserIdByUsernameAsync(username); } - catch (UserNotExistException e) + catch (UserNotExistException) { - _logger.LogInformation(e, Log.Format(LogDeleteNotExist, ("Username", username))); return BadRequest(ErrorResponse.UserCommon.NotExist()); } diff --git a/BackEnd/Timeline/Controllers/UserController.cs b/BackEnd/Timeline/Controllers/UserController.cs index 615eac2d..c0ae6a09 100644 --- a/BackEnd/Timeline/Controllers/UserController.cs +++ b/BackEnd/Timeline/Controllers/UserController.cs @@ -1,18 +1,14 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Logging; using System.Collections.Generic; using System.Threading.Tasks; using Timeline.Auth; -using Timeline.Helpers; using Timeline.Models.Http; using Timeline.Models.Validation; using Timeline.Services; using Timeline.Services.Mapper; using Timeline.Services.User; -using static Timeline.Resources.Controllers.UserController; -using static Timeline.Resources.Messages; namespace Timeline.Controllers { @@ -23,16 +19,14 @@ namespace Timeline.Controllers [ProducesErrorResponseType(typeof(CommonResponse))] public class UserController : Controller { - private readonly ILogger _logger; private readonly IUserService _userService; private readonly IUserPermissionService _userPermissionService; private readonly IUserDeleteService _userDeleteService; private readonly IGenericMapper _mapper; /// - public UserController(ILogger logger, IUserService userService, IUserPermissionService userPermissionService, IUserDeleteService userDeleteService, IGenericMapper mapper) + public UserController(IUserService userService, IUserPermissionService userPermissionService, IUserDeleteService userDeleteService, IGenericMapper mapper) { - _logger = logger; _userService = userService; _userPermissionService = userPermissionService; _userDeleteService = userDeleteService; @@ -93,9 +87,8 @@ namespace Timeline.Controllers var user = await _userService.GetUserAsync(id); return await _mapper.MapAsync(user, Url, User); } - catch (UserNotExistException e) + catch (UserNotExistException) { - _logger.LogInformation(e, Log.Format(LogGetUserNotExist, ("Username", username))); return NotFound(ErrorResponse.UserCommon.NotExist()); } } @@ -122,9 +115,8 @@ namespace Timeline.Controllers var user = await _userService.ModifyUserAsync(id, _mapper.AutoMapperMap(body)); return await _mapper.MapAsync(user, Url, User); } - catch (UserNotExistException e) + catch (UserNotExistException) { - _logger.LogInformation(e, Log.Format(LogPatchUserNotExist, ("Username", username))); return NotFound(ErrorResponse.UserCommon.NotExist()); } catch (EntityAlreadyExistException e) when (e.EntityName == EntityNames.User) @@ -136,15 +128,15 @@ namespace Timeline.Controllers { if (User.Identity!.Name != username) return StatusCode(StatusCodes.Status403Forbidden, - ErrorResponse.Common.CustomMessage_Forbid(Common_Forbid_NotSelf)); + ErrorResponse.Common.CustomMessage_Forbid(Resources.Messages.Common_Forbid_NotSelf)); if (body.Username != null) return StatusCode(StatusCodes.Status403Forbidden, - ErrorResponse.Common.CustomMessage_Forbid(UserController_Patch_Forbid_Username)); + ErrorResponse.Common.CustomMessage_Forbid(Resources.Messages.UserController_Patch_Forbid_Username)); if (body.Password != null) return StatusCode(StatusCodes.Status403Forbidden, - ErrorResponse.Common.CustomMessage_Forbid(UserController_Patch_Forbid_Password)); + ErrorResponse.Common.CustomMessage_Forbid(Resources.Messages.UserController_Patch_Forbid_Password)); var user = await _userService.ModifyUserAsync(this.GetUserId(), _mapper.AutoMapperMap(body)); return await _mapper.MapAsync(user, Url, User); @@ -191,10 +183,8 @@ namespace Timeline.Controllers await _userService.ChangePassword(this.GetUserId(), request.OldPassword, request.NewPassword); return Ok(); } - catch (BadPasswordException e) + catch (BadPasswordException) { - _logger.LogInformation(e, Log.Format(LogChangePasswordBadPassword, - ("Username", User.Identity!.Name), ("Old Password", request.OldPassword))); return BadRequest(ErrorResponse.UserController.ChangePassword_BadOldPassword()); } // User can't be non-existent or the token is bad. -- cgit v1.2.3