diff options
author | crupest <crupest@outlook.com> | 2021-05-05 15:58:40 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-05-05 15:58:40 +0800 |
commit | 0accc9f09d0aaf2292cb94e3c4e438c3f76f89e5 (patch) | |
tree | 6877c4815b54eddda828d3d89752ab964d17d1bf /BackEnd/Timeline/Controllers/MyControllerBase.cs | |
parent | 1c4be4a43c4acd2ee46e37685e9153d9c5b34233 (diff) | |
download | timeline-0accc9f09d0aaf2292cb94e3c4e438c3f76f89e5.tar.gz timeline-0accc9f09d0aaf2292cb94e3c4e438c3f76f89e5.tar.bz2 timeline-0accc9f09d0aaf2292cb94e3c4e438c3f76f89e5.zip |
refactor: ...
Diffstat (limited to 'BackEnd/Timeline/Controllers/MyControllerBase.cs')
-rw-r--r-- | BackEnd/Timeline/Controllers/MyControllerBase.cs | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/BackEnd/Timeline/Controllers/MyControllerBase.cs b/BackEnd/Timeline/Controllers/MyControllerBase.cs new file mode 100644 index 00000000..d4ee9d3e --- /dev/null +++ b/BackEnd/Timeline/Controllers/MyControllerBase.cs @@ -0,0 +1,76 @@ +using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+using System;
+using Timeline.Auth;
+using Timeline.Models.Http;
+using Timeline.Services.User;
+
+namespace Timeline.Controllers
+{
+ public class MyControllerBase : ControllerBase
+ {
+ #region auth
+ protected bool UserHasPermission(UserPermission permission)
+ {
+ return User.HasPermission(permission);
+ }
+
+ protected string? GetOptionalUsername()
+ {
+ return User.GetOptionalName();
+ }
+
+ protected string GetUsername()
+ {
+ return GetOptionalUsername() ?? throw new InvalidOperationException(Resource.ExceptionNoUsername);
+ }
+
+ protected long? GetOptionalUserId()
+ {
+ return User.GetOptionalUserId();
+ }
+
+ protected long GetUserId()
+ {
+ return GetOptionalUserId() ?? throw new InvalidOperationException(Resource.ExceptionNoUserId);
+ }
+ #endregion auth
+
+ #region action result
+ protected ObjectResult StatusCodeWithCommonResponse(int statusCode, int code, string message)
+ {
+ return StatusCode(statusCode, new CommonResponse(code, message));
+ }
+
+ protected ObjectResult OkWithCommonResponse(int statusCode = 0, string? message = null)
+ {
+ return Ok(new CommonResponse(statusCode, message ?? Resource.MessageOperationSucceeded));
+ }
+
+ protected ObjectResult OkWithCommonResponse(string? message)
+ {
+ return OkWithCommonResponse(message: message);
+ }
+
+ protected ObjectResult ForbidWithCommonResponse(string? message = null)
+ {
+ return StatusCode(StatusCodes.Status403Forbidden, new CommonResponse(ErrorCodes.Common.Forbid, message ?? Resource.MessageForbid));
+ }
+
+ protected ObjectResult ForbidWithCommonResponse(int code, string? message = null)
+ {
+ return StatusCode(StatusCodes.Status403Forbidden, new CommonResponse(code, message ?? Resource.MessageForbid));
+ }
+
+ protected ObjectResult DeleteWithCommonDeleteResponse(bool delete = true)
+ {
+ return StatusCode(StatusCodes.Status200OK, CommonDeleteResponse.Create(delete));
+ }
+
+ protected BadRequestObjectResult BadRequestWithCommonResponse(int code, string message)
+ {
+ return BadRequest(new CommonResponse(code, message));
+ }
+ #endregion action result
+ }
+}
|