diff options
author | crupest <crupest@outlook.com> | 2020-01-18 00:50:31 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2020-01-18 00:50:31 +0800 |
commit | c5f3c69b3a008ab87542e523e2a59f37801bd65a (patch) | |
tree | 8479cc9af91a7e7b1c0c60a0abc778244359bdbd /Timeline/Controllers | |
parent | 289c7e1fada1f4dae6ce5e421e997ebddd55c2df (diff) | |
download | timeline-c5f3c69b3a008ab87542e523e2a59f37801bd65a.tar.gz timeline-c5f3c69b3a008ab87542e523e2a59f37801bd65a.tar.bz2 timeline-c5f3c69b3a008ab87542e523e2a59f37801bd65a.zip |
...
Diffstat (limited to 'Timeline/Controllers')
-rw-r--r-- | Timeline/Controllers/PersonalTimelineController.cs | 40 | ||||
-rw-r--r-- | Timeline/Controllers/TokenController.cs | 50 | ||||
-rw-r--r-- | Timeline/Controllers/UserAvatarController.cs | 74 | ||||
-rw-r--r-- | Timeline/Controllers/UserController.cs | 64 |
4 files changed, 43 insertions, 185 deletions
diff --git a/Timeline/Controllers/PersonalTimelineController.cs b/Timeline/Controllers/PersonalTimelineController.cs index c864ed39..e1e3aba0 100644 --- a/Timeline/Controllers/PersonalTimelineController.cs +++ b/Timeline/Controllers/PersonalTimelineController.cs @@ -12,24 +12,7 @@ using Timeline.Models.Http; using Timeline.Models.Validation;
using Timeline.Services;
using static Timeline.Resources.Controllers.TimelineController;
-
-namespace Timeline
-{
- public static partial class ErrorCodes
- {
- public static partial class Http
- {
- public static class Timeline // ccc = 004
- {
- public const int PostListGetForbid = 10040101;
- public const int PostOperationCreateForbid = 10040102;
- public const int PostOperationDeleteForbid = 10040103;
- public const int PostOperationDeleteNotExist = 10040201;
- public const int ChangeMemberUserNotExist = 10040301;
- }
- }
- }
-}
+using static Timeline.Resources.Messages;
namespace Timeline.Controllers
{
@@ -80,8 +63,7 @@ namespace Timeline.Controllers {
if (!IsAdmin() && !await _service.HasReadPermission(username, GetAuthUsername()))
{
- return StatusCode(StatusCodes.Status403Forbidden,
- new CommonResponse(ErrorCodes.Http.Timeline.PostListGetForbid, MessagePostListGetForbid));
+ return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid());
}
return await _service.GetPosts(username);
@@ -94,8 +76,7 @@ namespace Timeline.Controllers {
if (!IsAdmin() && !await _service.IsMemberOf(username, GetAuthUsername()!))
{
- return StatusCode(StatusCodes.Status403Forbidden,
- new CommonResponse(ErrorCodes.Http.Timeline.PostOperationCreateForbid, MessagePostOperationCreateForbid));
+ return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid());
}
var res = await _service.CreatePost(username, User.Identity.Name!, body.Content, body.Time);
@@ -112,16 +93,13 @@ namespace Timeline.Controllers var postId = body.Id!.Value;
if (!IsAdmin() && !await _service.HasPostModifyPermission(username, postId, GetAuthUsername()!))
{
- return StatusCode(StatusCodes.Status403Forbidden,
- new CommonResponse(ErrorCodes.Http.Timeline.PostOperationDeleteForbid, MessagePostOperationCreateForbid));
+ return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid());
}
await _service.DeletePost(username, postId);
}
catch (TimelinePostNotExistException)
{
- return BadRequest(new CommonResponse(
- ErrorCodes.Http.Timeline.PostOperationDeleteNotExist,
- MessagePostOperationDeleteNotExist));
+ return BadRequest(ErrorResponse.TimelineController.PostOperationDelete_NotExist());
}
return Ok();
}
@@ -151,13 +129,13 @@ namespace Timeline.Controllers {
if (e.InnerException is UsernameBadFormatException)
{
- return BadRequest(CommonResponse.InvalidModel(
- string.Format(CultureInfo.CurrentCulture, MessageMemberUsernameBadFormat, e.Index, e.Operation)));
+ return BadRequest(ErrorResponse.Common.CustomMessage_InvalidModel(
+ TimelineController_ChangeMember_UsernameBadFormat, e.Index, e.Operation));
}
else if (e.InnerException is UserNotExistException)
{
- return BadRequest(new CommonResponse(ErrorCodes.Http.Timeline.ChangeMemberUserNotExist,
- string.Format(CultureInfo.CurrentCulture, MessageMemberUserNotExist, e.Index, e.Operation)));
+ return BadRequest(ErrorResponse.Common.CustomMessage_InvalidModel(
+ TimelineController_ChangeMember_UserNotExist, e.Index, e.Operation));
}
_logger.LogError(e, LogUnknownTimelineMemberOperationUserException);
diff --git a/Timeline/Controllers/TokenController.cs b/Timeline/Controllers/TokenController.cs index 01f4778f..c360a109 100644 --- a/Timeline/Controllers/TokenController.cs +++ b/Timeline/Controllers/TokenController.cs @@ -10,31 +10,6 @@ using Timeline.Models.Http; using Timeline.Services;
using static Timeline.Resources.Controllers.TokenController;
-namespace Timeline
-{
- public static partial class ErrorCodes
- {
- public static partial class Http
- {
- public static class Token // bbb = 001
- {
- public static class Create // cc = 01
- {
- public const int BadCredential = 10010101;
- }
-
- public static class Verify // cc = 02
- {
- public const int BadFormat = 10010201;
- public const int UserNotExist = 10010202;
- public const int OldVersion = 10010203;
- public const int Expired = 10010204;
- }
- }
- }
- }
-}
-
namespace Timeline.Controllers
{
[Route("token")]
@@ -87,16 +62,12 @@ namespace Timeline.Controllers catch (UserNotExistException e)
{
LogFailure(LogUserNotExist, e);
- return BadRequest(new CommonResponse(
- ErrorCodes.Http.Token.Create.BadCredential,
- ErrorBadCredential));
+ return BadRequest(ErrorResponse.TokenController.Create_BadCredential());
}
catch (BadPasswordException e)
{
LogFailure(LogBadPassword, e);
- return BadRequest(new CommonResponse(
- ErrorCodes.Http.Token.Create.BadCredential,
- ErrorBadCredential));
+ return BadRequest(ErrorResponse.TokenController.Create_BadCredential());
}
}
@@ -128,31 +99,28 @@ namespace Timeline.Controllers if (e.ErrorCode == JwtVerifyException.ErrorCodes.Expired)
{
var innerException = e.InnerException as SecurityTokenExpiredException;
- LogFailure(LogVerifyExpire, e, ("Expires", innerException?.Expires),
+ LogFailure(LogVerifyExpire, e, ("Expires", innerException.Expires),
("Current Time", _clock.GetCurrentTime()));
- return BadRequest(new CommonResponse(
- ErrorCodes.Http.Token.Verify.Expired, ErrorVerifyExpire));
+ return BadRequest(ErrorResponse.TokenController.Verify_TimeExpired());
}
else if (e.ErrorCode == JwtVerifyException.ErrorCodes.OldVersion)
{
var innerException = e.InnerException as JwtBadVersionException;
LogFailure(LogVerifyOldVersion, e,
- ("Token Version", innerException?.TokenVersion), ("Required Version", innerException?.RequiredVersion));
- return BadRequest(new CommonResponse(
- ErrorCodes.Http.Token.Verify.OldVersion, ErrorVerifyOldVersion));
+ ("Token Version", innerException.TokenVersion),
+ ("Required Version", innerException?.RequiredVersion));
+ return BadRequest(ErrorResponse.TokenController.Verify_OldVersion());
}
else
{
LogFailure(LogVerifyBadFormat, e);
- return BadRequest(new CommonResponse(
- ErrorCodes.Http.Token.Verify.BadFormat, ErrorVerifyBadFormat));
+ return BadRequest(ErrorResponse.TokenController.Verify_BadFormat());
}
}
catch (UserNotExistException e)
{
LogFailure(LogVerifyUserNotExist, e);
- return BadRequest(new CommonResponse(
- ErrorCodes.Http.Token.Verify.UserNotExist, ErrorVerifyUserNotExist));
+ return BadRequest(ErrorResponse.TokenController.Verify_UserNotExist());
}
}
}
diff --git a/Timeline/Controllers/UserAvatarController.cs b/Timeline/Controllers/UserAvatarController.cs index 7625f962..b4a6d8fd 100644 --- a/Timeline/Controllers/UserAvatarController.cs +++ b/Timeline/Controllers/UserAvatarController.cs @@ -14,39 +14,6 @@ using Timeline.Models.Validation; using Timeline.Services;
using static Timeline.Resources.Controllers.UserAvatarController;
-namespace Timeline
-{
- public static partial class ErrorCodes
- {
- public static partial class Http
- {
- public static class UserAvatar // bbb = 003
- {
- public static class Get // cc = 01
- {
- public const int UserNotExist = 10030101;
- }
-
- public static class Put // cc = 02
- {
- public const int UserNotExist = 10030201;
- public const int Forbid = 10030202;
- public const int BadFormat_CantDecode = 10030203;
- public const int BadFormat_UnmatchedFormat = 10030204;
- public const int BadFormat_BadSize = 10030205;
-
- }
-
- public static class Delete // cc = 03
- {
- public const int UserNotExist = 10030301;
- public const int Forbid = 10030302;
- }
- }
- }
- }
-}
-
namespace Timeline.Controllers
{
[ApiController]
@@ -79,7 +46,7 @@ namespace Timeline.Controllers {
_logger.LogInformation(Log.Format(LogGetBadIfNoneMatch,
("Username", username), ("If-None-Match", value)));
- return BadRequest(HeaderErrorResponse.BadIfNonMatch());
+ return BadRequest(ErrorResponse.Common.Header.IfNonMatch_BadFormat());
}
if (eTagList.FirstOrDefault(e => e.Equals(eTag)) != null)
@@ -99,7 +66,7 @@ namespace Timeline.Controllers catch (UserNotExistException e)
{
_logger.LogInformation(e, Log.Format(LogGetUserNotExist, ("Username", username)));
- return NotFound(new CommonResponse(ErrorCodes.Http.UserAvatar.Get.UserNotExist, ErrorGetUserNotExist));
+ return NotFound(ErrorResponse.UserController.ChangePassword_BadOldPassword());
}
}
@@ -111,14 +78,13 @@ namespace Timeline.Controllers {
var contentLength = Request.ContentLength!.Value;
if (contentLength > 1000 * 1000 * 10)
- return BadRequest(ContentErrorResponse.TooBig("10MB"));
+ return BadRequest(ErrorResponse.Common.Content.TooBig("10MB"));
if (!User.IsAdministrator() && User.Identity.Name != username)
{
_logger.LogInformation(Log.Format(LogPutForbid,
("Operator Username", User.Identity.Name), ("Username To Put Avatar", username)));
- return StatusCode(StatusCodes.Status403Forbidden,
- new CommonResponse(ErrorCodes.Http.UserAvatar.Put.Forbid, ErrorPutForbid));
+ return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid());
}
try
@@ -127,11 +93,11 @@ namespace Timeline.Controllers var bytesRead = await Request.Body.ReadAsync(data);
if (bytesRead != contentLength)
- return BadRequest(ContentErrorResponse.UnmatchedLength_Smaller());
+ return BadRequest(ErrorResponse.Common.Content.UnmatchedLength_Smaller());
var extraByte = new byte[1];
if (await Request.Body.ReadAsync(extraByte) != 0)
- return BadRequest(ContentErrorResponse.UnmatchedLength_Bigger());
+ return BadRequest(ErrorResponse.Common.Content.UnmatchedLength_Bigger());
await _service.SetAvatar(username, new Avatar
{
@@ -146,24 +112,19 @@ namespace Timeline.Controllers catch (UserNotExistException e)
{
_logger.LogInformation(e, Log.Format(LogPutUserNotExist, ("Username", username)));
- return BadRequest(new CommonResponse(ErrorCodes.Http.UserAvatar.Put.UserNotExist, ErrorPutUserNotExist));
+ return BadRequest(ErrorResponse.UserController.ChangePassword_BadOldPassword());
}
catch (AvatarFormatException e)
{
- var (code, message) = e.Error switch
+ _logger.LogInformation(e, Log.Format(LogPutUserBadFormat, ("Username", username)));
+ return BadRequest(e.Error switch
{
- AvatarFormatException.ErrorReason.CantDecode =>
- (ErrorCodes.Http.UserAvatar.Put.BadFormat_CantDecode, ErrorPutBadFormatCantDecode),
- AvatarFormatException.ErrorReason.UnmatchedFormat =>
- (ErrorCodes.Http.UserAvatar.Put.BadFormat_UnmatchedFormat, ErrorPutBadFormatUnmatchedFormat),
- AvatarFormatException.ErrorReason.BadSize =>
- (ErrorCodes.Http.UserAvatar.Put.BadFormat_BadSize, ErrorPutBadFormatBadSize),
+ AvatarFormatException.ErrorReason.CantDecode => ErrorResponse.UserAvatar.BadFormat_CantDecode(),
+ AvatarFormatException.ErrorReason.UnmatchedFormat => ErrorResponse.UserAvatar.BadFormat_UnmatchedFormat(),
+ AvatarFormatException.ErrorReason.BadSize => ErrorResponse.UserAvatar.BadFormat_BadSize(),
_ =>
throw new Exception(ExceptionUnknownAvatarFormatError)
- };
-
- _logger.LogInformation(e, Log.Format(LogPutUserBadFormat, ("Username", username)));
- return BadRequest(new CommonResponse(code, message));
+ });
}
}
@@ -173,23 +134,20 @@ namespace Timeline.Controllers {
if (!User.IsAdministrator() && User.Identity.Name != username)
{
- _logger.LogInformation(Log.Format(LogPutUserBadFormat,
+ _logger.LogInformation(Log.Format(LogDeleteForbid,
("Operator Username", User.Identity.Name), ("Username To Delete Avatar", username)));
- return StatusCode(StatusCodes.Status403Forbidden,
- new CommonResponse(ErrorCodes.Http.UserAvatar.Delete.Forbid, ErrorDeleteForbid));
+ return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid());
}
try
{
await _service.SetAvatar(username, null);
-
- _logger.LogInformation(Log.Format(LogDeleteSuccess, ("Username", username)));
return Ok();
}
catch (UserNotExistException e)
{
_logger.LogInformation(e, Log.Format(LogDeleteNotExist, ("Username", username)));
- return BadRequest(new CommonResponse(ErrorCodes.Http.UserAvatar.Delete.UserNotExist, ErrorDeleteUserNotExist));
+ return BadRequest(ErrorResponse.UserCommon.NotExist());
}
}
}
diff --git a/Timeline/Controllers/UserController.cs b/Timeline/Controllers/UserController.cs index 0d950cd7..956865dc 100644 --- a/Timeline/Controllers/UserController.cs +++ b/Timeline/Controllers/UserController.cs @@ -1,7 +1,7 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
-using System.Globalization;
+using System;
using System.Threading.Tasks;
using Timeline.Auth;
using Timeline.Helpers;
@@ -11,43 +11,6 @@ using Timeline.Models.Validation; using Timeline.Services;
using static Timeline.Resources.Controllers.UserController;
-namespace Timeline
-{
- public static partial class ErrorCodes
- {
- public static partial class Http
- {
- public static class User // bbb = 002
- {
- public static class Get // cc = 01
- {
- public const int NotExist = 10020101; // dd = 01
- }
-
- public static class Patch // cc = 03
- {
- public const int NotExist = 10020301; // dd = 01
- }
-
- public static class Op // cc = 1x
- {
- public static class ChangeUsername // cc = 11
- {
- public const int NotExist = 10021101; // dd = 01
- public const int AlreadyExist = 10021102; // dd = 02
- }
-
- public static class ChangePassword // cc = 12
- {
- public const int BadOldPassword = 10021201; // dd = 01
- }
- }
-
- }
- }
- }
-}
-
namespace Timeline.Controllers
{
[ApiController]
@@ -76,7 +39,7 @@ namespace Timeline.Controllers if (user == null)
{
_logger.LogInformation(Log.Format(LogGetUserNotExist, ("Username", username)));
- return NotFound(new CommonResponse(ErrorCodes.Http.User.Get.NotExist, ErrorGetUserNotExist));
+ return NotFound(ErrorResponse.UserCommon.NotExist());
}
return Ok(user);
}
@@ -88,13 +51,11 @@ namespace Timeline.Controllers switch (result)
{
case PutResult.Create:
- _logger.LogInformation(Log.Format(LogPutCreate, ("Username", username)));
return CreatedAtAction("Get", new { username }, CommonPutResponse.Create());
case PutResult.Modify:
- _logger.LogInformation(Log.Format(LogPutModify, ("Username", username)));
return Ok(CommonPutResponse.Modify());
default:
- throw new InvalidBranchException();
+ throw new Exception(ExceptionUnknownPutResult);
}
}
@@ -109,7 +70,7 @@ namespace Timeline.Controllers catch (UserNotExistException e)
{
_logger.LogInformation(e, Log.Format(LogPatchUserNotExist, ("Username", username)));
- return NotFound(new CommonResponse(ErrorCodes.Http.User.Patch.NotExist, ErrorPatchUserNotExist));
+ return NotFound(ErrorResponse.UserCommon.NotExist());
}
}
@@ -119,12 +80,10 @@ namespace Timeline.Controllers try
{
await _userService.DeleteUser(username);
- _logger.LogInformation(Log.Format(LogDeleteDelete, ("Username", username)));
return Ok(CommonDeleteResponse.Delete());
}
- catch (UserNotExistException e)
+ catch (UserNotExistException)
{
- _logger.LogInformation(e, Log.Format(LogDeleteNotExist, ("Username", username)));
return Ok(CommonDeleteResponse.NotExist());
}
}
@@ -135,22 +94,19 @@ namespace Timeline.Controllers try
{
await _userService.ChangeUsername(request.OldUsername, request.NewUsername);
- _logger.LogInformation(Log.Format(LogChangeUsernameSuccess,
- ("Old Username", request.OldUsername), ("New Username", request.NewUsername)));
return Ok();
}
catch (UserNotExistException e)
{
_logger.LogInformation(e, Log.Format(LogChangeUsernameNotExist,
("Old Username", request.OldUsername), ("New Username", request.NewUsername)));
- return BadRequest(new CommonResponse(ErrorCodes.Http.User.Op.ChangeUsername.NotExist,
- string.Format(CultureInfo.CurrentCulture, ErrorChangeUsernameNotExist, request.OldUsername)));
+ return BadRequest(ErrorResponse.UserCommon.NotExist());
}
catch (UsernameConfictException e)
{
- _logger.LogInformation(e, Log.Format(LogChangeUsernameAlreadyExist,
+ _logger.LogInformation(e, Log.Format(LogChangeUsernameConflict,
("Old Username", request.OldUsername), ("New Username", request.NewUsername)));
- return BadRequest(new CommonResponse(ErrorCodes.Http.User.Op.ChangeUsername.AlreadyExist, ErrorChangeUsernameAlreadyExist));
+ return BadRequest(ErrorResponse.UserController.ChangeUsername_Conflict());
}
// there is no need to catch bad format exception because it is already checked in model validation.
}
@@ -161,15 +117,13 @@ namespace Timeline.Controllers try
{
await _userService.ChangePassword(User.Identity.Name!, request.OldPassword, request.NewPassword);
- _logger.LogInformation(Log.Format(LogChangePasswordSuccess, ("Username", User.Identity.Name)));
return Ok();
}
catch (BadPasswordException e)
{
_logger.LogInformation(e, Log.Format(LogChangePasswordBadPassword,
("Username", User.Identity.Name), ("Old Password", request.OldPassword)));
- return BadRequest(new CommonResponse(ErrorCodes.Http.User.Op.ChangePassword.BadOldPassword,
- ErrorChangePasswordBadPassword));
+ return BadRequest(ErrorResponse.UserController.ChangePassword_BadOldPassword());
}
// User can't be non-existent or the token is bad.
}
|