aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2019-04-22 14:45:52 +0800
committercrupest <crupest@outlook.com>2019-04-22 14:45:52 +0800
commit407f97db0be86aa071802b67bfdeadc7703528c9 (patch)
tree5dc7ed4618477fb695fc45d26984616ecd5fd74a
parente347b4a4092a24ff7106ffd3aca67d6ca7decca8 (diff)
downloadtimeline-407f97db0be86aa071802b67bfdeadc7703528c9.tar.gz
timeline-407f97db0be86aa071802b67bfdeadc7703528c9.tar.bz2
timeline-407f97db0be86aa071802b67bfdeadc7703528c9.zip
Move http models in to a new namespace. Revert last commit.
-rw-r--r--Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs2
-rw-r--r--Timeline.Tests/JwtTokenUnitTest.cs2
-rw-r--r--Timeline/Controllers/TokenController.cs2
-rw-r--r--Timeline/Controllers/UserController.cs46
-rw-r--r--Timeline/Entities/Common.cs12
-rw-r--r--Timeline/Entities/Http/Common.cs29
-rw-r--r--Timeline/Entities/Http/Token.cs (renamed from Timeline/Entities/Token.cs)2
-rw-r--r--Timeline/Entities/Http/User.cs26
-rw-r--r--Timeline/Entities/User.cs30
-rw-r--r--Timeline/Services/UserService.cs10
10 files changed, 77 insertions, 84 deletions
diff --git a/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs
index 40191009..cda9fe99 100644
--- a/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs
+++ b/Timeline.Tests/Helpers/Authentication/AuthenticationExtensions.cs
@@ -4,7 +4,7 @@ using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
-using Timeline.Entities;
+using Timeline.Entities.Http;
using Xunit;
namespace Timeline.Tests.Helpers.Authentication
diff --git a/Timeline.Tests/JwtTokenUnitTest.cs b/Timeline.Tests/JwtTokenUnitTest.cs
index 39ffc928..8a503bd7 100644
--- a/Timeline.Tests/JwtTokenUnitTest.cs
+++ b/Timeline.Tests/JwtTokenUnitTest.cs
@@ -2,7 +2,7 @@
using Newtonsoft.Json;
using System.Net;
using System.Net.Http;
-using Timeline.Entities;
+using Timeline.Entities.Http;
using Timeline.Tests.Helpers;
using Timeline.Tests.Helpers.Authentication;
using Xunit;
diff --git a/Timeline/Controllers/TokenController.cs b/Timeline/Controllers/TokenController.cs
index 463fb83c..0be5fb2f 100644
--- a/Timeline/Controllers/TokenController.cs
+++ b/Timeline/Controllers/TokenController.cs
@@ -2,7 +2,7 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
-using Timeline.Entities;
+using Timeline.Entities.Http;
using Timeline.Services;
namespace Timeline.Controllers
diff --git a/Timeline/Controllers/UserController.cs b/Timeline/Controllers/UserController.cs
index d2708eeb..59c7a48c 100644
--- a/Timeline/Controllers/UserController.cs
+++ b/Timeline/Controllers/UserController.cs
@@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using Timeline.Entities;
+using Timeline.Entities.Http;
using Timeline.Services;
namespace Timeline.Controllers
@@ -48,50 +49,29 @@ namespace Timeline.Controllers
}
}
- [HttpPatch("user/{username}"), Authorize]
+ [HttpPatch("user/{username}"), Authorize(Roles = "admin")]
public async Task<IActionResult> Patch([FromBody] UserModifyRequest request, [FromRoute] string username)
{
- if (User.IsInRole("admin"))
- {
- var result = await _userService.PatchUser(username, request.Password, request.Roles);
- switch (result)
- {
- case PatchUserResult.Success:
- return Ok();
- case PatchUserResult.NotExists:
- return NotFound();
- default:
- throw new Exception("Unreachable code.");
- }
- }
- else
+ var result = await _userService.PatchUser(username, request.Password, request.Roles);
+ switch (result)
{
- if (User.Identity.Name != username)
- return StatusCode(403, new MessageResponse("Can't patch other user when you are not admin."));
- if (request.Roles != null)
- return StatusCode(403, new MessageResponse("Can't patch roles when you are not admin."));
-
- var result = await _userService.PatchUser(username, request.Password, null);
- switch (result)
- {
- case PatchUserResult.Success:
- return Ok();
- case PatchUserResult.NotExists:
- return NotFound(new MessageResponse("This username no longer exists. Please update your token."));
- default:
- throw new Exception("Unreachable code.");
- }
+ case PatchUserResult.Success:
+ return Ok();
+ case PatchUserResult.NotExists:
+ return NotFound();
+ default:
+ throw new Exception("Unreachable code.");
}
}
[HttpDelete("user/{username}"), Authorize(Roles = "admin")]
- public async Task<ActionResult<UserDeleteResponse>> Delete([FromRoute] string username)
+ public async Task<IActionResult> Delete([FromRoute] string username)
{
var result = await _userService.DeleteUser(username);
switch (result)
{
- case DeleteUserResult.Success:
- return Ok(UserDeleteResponse.Success);
+ case DeleteUserResult.Deleted:
+ return Ok(UserDeleteResponse.Deleted);
case DeleteUserResult.NotExists:
return Ok(UserDeleteResponse.NotExists);
default:
diff --git a/Timeline/Entities/Common.cs b/Timeline/Entities/Common.cs
deleted file mode 100644
index 235a2a20..00000000
--- a/Timeline/Entities/Common.cs
+++ /dev/null
@@ -1,12 +0,0 @@
-namespace Timeline.Entities
-{
- public class MessageResponse
- {
- public MessageResponse(string message)
- {
- Message = message;
- }
-
- public string Message { get; set; }
- }
-}
diff --git a/Timeline/Entities/Http/Common.cs b/Timeline/Entities/Http/Common.cs
new file mode 100644
index 00000000..9575e6fa
--- /dev/null
+++ b/Timeline/Entities/Http/Common.cs
@@ -0,0 +1,29 @@
+namespace Timeline.Entities.Http
+{
+ public class ReturnCodeMessageResponse
+ {
+ public ReturnCodeMessageResponse()
+ {
+
+ }
+
+ public ReturnCodeMessageResponse(int code)
+ {
+ ReturnCode = code;
+ }
+
+ public ReturnCodeMessageResponse(string message)
+ {
+ Message = message;
+ }
+
+ public ReturnCodeMessageResponse(int code, string message)
+ {
+ ReturnCode = code;
+ Message = message;
+ }
+
+ public int? ReturnCode { get; set; } = null;
+ public string Message { get; set; } = null;
+ }
+}
diff --git a/Timeline/Entities/Token.cs b/Timeline/Entities/Http/Token.cs
index 1b5a469d..45ee0fc5 100644
--- a/Timeline/Entities/Token.cs
+++ b/Timeline/Entities/Http/Token.cs
@@ -1,4 +1,4 @@
-namespace Timeline.Entities
+namespace Timeline.Entities.Http
{
public class CreateTokenRequest
{
diff --git a/Timeline/Entities/Http/User.cs b/Timeline/Entities/Http/User.cs
new file mode 100644
index 00000000..24952ac7
--- /dev/null
+++ b/Timeline/Entities/Http/User.cs
@@ -0,0 +1,26 @@
+namespace Timeline.Entities.Http
+{
+ public class UserModifyRequest
+ {
+ public string Password { get; set; }
+ public string[] Roles { get; set; }
+ }
+
+ public static class UserPutResponse
+ {
+ public const int CreatedCode = 0;
+ public const int ModifiedCode = 1;
+
+ public static ReturnCodeMessageResponse Created { get; } = new ReturnCodeMessageResponse(CreatedCode, "A new user is created.");
+ public static ReturnCodeMessageResponse Modified { get; } = new ReturnCodeMessageResponse(ModifiedCode, "A existing user is modified.");
+ }
+
+ public static class UserDeleteResponse
+ {
+ public const int DeletedCode = 0;
+ public const int NotExistsCode = 1;
+
+ public static ReturnCodeMessageResponse Deleted { get; } = new ReturnCodeMessageResponse(DeletedCode, "A existing user is deleted.");
+ public static ReturnCodeMessageResponse NotExists { get; } = new ReturnCodeMessageResponse(NotExistsCode, "User with given name does not exists.");
+ }
+}
diff --git a/Timeline/Entities/User.cs b/Timeline/Entities/User.cs
deleted file mode 100644
index eb126165..00000000
--- a/Timeline/Entities/User.cs
+++ /dev/null
@@ -1,30 +0,0 @@
-namespace Timeline.Entities
-{
- public class UserModifyRequest
- {
- public string Password { get; set; }
- public string[] Roles { get; set; }
- }
-
- public class UserPutResponse
- {
- public const int CreatedCode = 0;
- public const int ModifiedCode = 1;
-
- public static UserPutResponse Created { get; } = new UserPutResponse { ReturnCode = CreatedCode };
- public static UserPutResponse Modified { get; } = new UserPutResponse { ReturnCode = ModifiedCode };
-
- public int ReturnCode { get; set; }
- }
-
- public class UserDeleteResponse
- {
- public const int SuccessCode = 0;
- public const int NotExistsCode = 1;
-
- public static UserDeleteResponse Success { get; } = new UserDeleteResponse { ReturnCode = SuccessCode };
- public static UserDeleteResponse NotExists { get; } = new UserDeleteResponse { ReturnCode = NotExistsCode };
-
- public int ReturnCode { get; set; }
- }
-}
diff --git a/Timeline/Services/UserService.cs b/Timeline/Services/UserService.cs
index a0d358dd..8615d0c5 100644
--- a/Timeline/Services/UserService.cs
+++ b/Timeline/Services/UserService.cs
@@ -40,9 +40,9 @@ namespace Timeline.Services
public enum DeleteUserResult
{
/// <summary>
- /// Succeed to delete user.
+ /// A existing user is deleted.
/// </summary>
- Success,
+ Deleted,
/// <summary>
/// A user of given username does not exist.
/// </summary>
@@ -105,12 +105,12 @@ namespace Timeline.Services
/// <summary>
/// Delete a user of given username.
- /// Return <see cref="DeleteUserResult.Success"/> if success to delete.
+ /// Return <see cref="DeleteUserResult.Deleted"/> if the user is deleted.
/// Return <see cref="DeleteUserResult.NotExists"/> if the user of given username
/// does not exist.
/// </summary>
/// <param name="username">Username of thet user to delete.</param>
- /// <returns><see cref="DeleteUserResult.Success"/> if success to delete.
+ /// <returns><see cref="DeleteUserResult.Deleted"/> if the user is deleted.
/// <see cref="DeleteUserResult.NotExists"/> if the user doesn't exist.</returns>
Task<DeleteUserResult> DeleteUser(string username);
}
@@ -250,7 +250,7 @@ namespace Timeline.Services
_databaseContext.Users.Remove(user);
await _databaseContext.SaveChangesAsync();
- return DeleteUserResult.Success;
+ return DeleteUserResult.Deleted;
}
}
}