diff options
author | 杨宇千 <crupest@outlook.com> | 2019-08-09 21:48:12 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-09 21:48:12 +0800 |
commit | 38ff45fcc0b58a95ad52ba43a8be4ff466694269 (patch) | |
tree | f1cf455b758e8bf0265d4db0e42a404e9877b321 /Timeline/Controllers | |
parent | e283a3e745bad05a55c572646d7b20fbaaeb522d (diff) | |
parent | ea8397bafe24b5c9ab814891eb3a293a07ca217e (diff) | |
download | timeline-38ff45fcc0b58a95ad52ba43a8be4ff466694269.tar.gz timeline-38ff45fcc0b58a95ad52ba43a8be4ff466694269.tar.bz2 timeline-38ff45fcc0b58a95ad52ba43a8be4ff466694269.zip |
Merge pull request #38 from crupest/null-request-field
Do 3 things.
Diffstat (limited to 'Timeline/Controllers')
-rw-r--r-- | Timeline/Controllers/TokenController.cs | 18 | ||||
-rw-r--r-- | Timeline/Controllers/UserController.cs | 35 | ||||
-rw-r--r-- | Timeline/Controllers/UserTestController.cs | 1 |
3 files changed, 18 insertions, 36 deletions
diff --git a/Timeline/Controllers/TokenController.cs b/Timeline/Controllers/TokenController.cs index ff397518..3c166448 100644 --- a/Timeline/Controllers/TokenController.cs +++ b/Timeline/Controllers/TokenController.cs @@ -5,13 +5,14 @@ using Microsoft.IdentityModel.Tokens; using System;
using System.Collections.Generic;
using System.Threading.Tasks;
-using Timeline.Entities.Http;
+using Timeline.Models.Http;
using Timeline.Services;
using static Timeline.Helpers.MyLogHelper;
namespace Timeline.Controllers
{
[Route("token")]
+ [ApiController]
public class TokenController : Controller
{
private static class LoggingEventIds
@@ -60,22 +61,9 @@ namespace Timeline.Controllers Pair("Expire Offset (in days)", request.ExpireOffset)));
}
- TimeSpan? expireOffset = null;
- if (request.ExpireOffset != null)
- {
- if (request.ExpireOffset.Value <= 0.0)
- {
- const string message = "Expire time is not bigger than 0.";
- var code = ErrorCodes.Create_BadExpireOffset;
- LogFailure(message, code);
- return BadRequest(new CommonResponse(code, message));
- }
- expireOffset = TimeSpan.FromDays(request.ExpireOffset.Value);
- }
-
try
{
- var expiredTime = expireOffset == null ? null : (DateTime?)(_clock.GetCurrentTime() + expireOffset.Value);
+ var expiredTime = request.ExpireOffset == null ? null : (DateTime?)(_clock.GetCurrentTime().AddDays(request.ExpireOffset.Value));
var result = await _userService.CreateToken(request.Username, request.Password, expiredTime);
_logger.LogInformation(LoggingEventIds.CreateSucceeded, FormatLogMessage("Attemp to login succeeded.",
Pair("Username", request.Username),
diff --git a/Timeline/Controllers/UserController.cs b/Timeline/Controllers/UserController.cs index 8d338949..6f2fe77f 100644 --- a/Timeline/Controllers/UserController.cs +++ b/Timeline/Controllers/UserController.cs @@ -4,24 +4,23 @@ using Microsoft.Extensions.Logging; using System;
using System.Threading.Tasks;
using Timeline.Authenticate;
-using Timeline.Entities;
-using Timeline.Entities.Http;
+using Timeline.Models;
+using Timeline.Models.Http;
using Timeline.Services;
using static Timeline.Helpers.MyLogHelper;
namespace Timeline.Controllers
{
+ [ApiController]
public class UserController : Controller
{
- private static class ErrorCodes
+ public static class ErrorCodes
{
- public const int Get_NotExists = -1001;
+ public const int Get_NotExist = -1001;
- public const int Put_NoPassword = -2001;
+ public const int Patch_NotExist = -2001;
- public const int Patch_NotExists = -3001;
-
- public const int ChangePassword_BadOldPassword = -4001;
+ public const int ChangePassword_BadOldPassword = -3001;
}
private readonly ILogger<UserController> _logger;
@@ -39,28 +38,22 @@ namespace Timeline.Controllers return Ok(await _userService.ListUsers());
}
- [HttpGet("user/{username}"), AdminAuthorize]
+ [HttpGet("users/{username}"), AdminAuthorize]
public async Task<IActionResult> Get([FromRoute] string username)
{
var user = await _userService.GetUser(username);
if (user == null)
{
_logger.LogInformation(FormatLogMessage("Attempt to get a non-existent user.", Pair("Username", username)));
- return NotFound(new CommonResponse(ErrorCodes.Get_NotExists, "The user does not exist."));
+ return NotFound(new CommonResponse(ErrorCodes.Get_NotExist, "The user does not exist."));
}
return Ok(user);
}
- [HttpPut("user/{username}"), AdminAuthorize]
+ [HttpPut("users/{username}"), AdminAuthorize]
public async Task<IActionResult> Put([FromBody] UserPutRequest request, [FromRoute] string username)
{
- if (request.Password == null) // This place will be refactored.
- {
- _logger.LogInformation("Attempt to put a user without a password. Username: {} .", username);
- return BadRequest();
- }
-
- var result = await _userService.PutUser(username, request.Password, request.Administrator);
+ var result = await _userService.PutUser(username, request.Password, request.Administrator.Value);
switch (result)
{
case PutResult.Created:
@@ -74,7 +67,7 @@ namespace Timeline.Controllers }
}
- [HttpPatch("user/{username}"), AdminAuthorize]
+ [HttpPatch("users/{username}"), AdminAuthorize]
public async Task<IActionResult> Patch([FromBody] UserPatchRequest request, [FromRoute] string username)
{
try
@@ -85,11 +78,11 @@ namespace Timeline.Controllers catch (UserNotExistException e)
{
_logger.LogInformation(e, FormatLogMessage("Attempt to patch a non-existent user.", Pair("Username", username)));
- return BadRequest(new CommonResponse(ErrorCodes.Patch_NotExists, "The user does not exist."));
+ return NotFound(new CommonResponse(ErrorCodes.Patch_NotExist, "The user does not exist."));
}
}
- [HttpDelete("user/{username}"), AdminAuthorize]
+ [HttpDelete("users/{username}"), AdminAuthorize]
public async Task<IActionResult> Delete([FromRoute] string username)
{
try
diff --git a/Timeline/Controllers/UserTestController.cs b/Timeline/Controllers/UserTestController.cs index f65d9857..2a5f36a1 100644 --- a/Timeline/Controllers/UserTestController.cs +++ b/Timeline/Controllers/UserTestController.cs @@ -5,6 +5,7 @@ using Timeline.Authenticate; namespace Timeline.Controllers
{
[Route("Test/User")]
+ [ApiController]
public class UserTestController : Controller
{
[HttpGet("[action]")]
|