aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-01-31 22:46:17 +0800
committercrupest <crupest@outlook.com>2020-01-31 22:46:17 +0800
commit873d0d8df10e1d6403b7a4eac1980f874dfe1d05 (patch)
treef969a960472bbf828e5d639a8deef65341b52a6a
parentb892622e7ffdf4220f6631ec58f7a6692881dd35 (diff)
downloadtimeline-873d0d8df10e1d6403b7a4eac1980f874dfe1d05.tar.gz
timeline-873d0d8df10e1d6403b7a4eac1980f874dfe1d05.tar.bz2
timeline-873d0d8df10e1d6403b7a4eac1980f874dfe1d05.zip
Make all patch return the new entity.
-rw-r--r--Timeline.Tests/IntegratedTests/PersonalTimelineTest.cs9
-rw-r--r--Timeline.Tests/IntegratedTests/UserTest.cs10
-rw-r--r--Timeline/Controllers/PersonalTimelineController.cs5
-rw-r--r--Timeline/Controllers/UserController.cs10
-rw-r--r--Timeline/Services/TimelineService.cs32
-rw-r--r--Timeline/Services/UserService.cs14
6 files changed, 49 insertions, 31 deletions
diff --git a/Timeline.Tests/IntegratedTests/PersonalTimelineTest.cs b/Timeline.Tests/IntegratedTests/PersonalTimelineTest.cs
index f3d6b172..81446fd8 100644
--- a/Timeline.Tests/IntegratedTests/PersonalTimelineTest.cs
+++ b/Timeline.Tests/IntegratedTests/PersonalTimelineTest.cs
@@ -119,19 +119,22 @@ namespace Timeline.Tests.IntegratedTests
{
var res = await client.PatchAsJsonAsync("users/user1/timeline",
new TimelinePatchRequest { Description = mockDescription });
- res.Should().HaveStatusCode(200);
+ res.Should().HaveStatusCode(200)
+ .And.HaveJsonBody<BaseTimelineInfo>().Which.Description.Should().Be(mockDescription);
await AssertDescription(mockDescription);
}
{
var res = await client.PatchAsJsonAsync("users/user1/timeline",
new TimelinePatchRequest { Description = null });
- res.Should().HaveStatusCode(200);
+ res.Should().HaveStatusCode(200)
+ .And.HaveJsonBody<BaseTimelineInfo>().Which.Description.Should().Be(mockDescription);
await AssertDescription(mockDescription);
}
{
var res = await client.PatchAsJsonAsync("users/user1/timeline",
new TimelinePatchRequest { Description = "" });
- res.Should().HaveStatusCode(200);
+ res.Should().HaveStatusCode(200)
+ .And.HaveJsonBody<BaseTimelineInfo>().Which.Description.Should().Be("");
await AssertDescription("");
}
}
diff --git a/Timeline.Tests/IntegratedTests/UserTest.cs b/Timeline.Tests/IntegratedTests/UserTest.cs
index f863eb6c..8ce76299 100644
--- a/Timeline.Tests/IntegratedTests/UserTest.cs
+++ b/Timeline.Tests/IntegratedTests/UserTest.cs
@@ -102,7 +102,9 @@ namespace Timeline.Tests.IntegratedTests
{
var res = await client.PatchAsJsonAsync("/users/user1",
new UserPatchRequest { Nickname = "aaa" });
- res.Should().HaveStatusCode(200);
+ res.Should().HaveStatusCode(200)
+ .And.HaveJsonBody<UserInfo>()
+ .Which.Nickname.Should().Be("aaa");
}
{
@@ -128,7 +130,11 @@ namespace Timeline.Tests.IntegratedTests
Administrator = true,
Nickname = "aaa"
});
- res.Should().HaveStatusCode(200);
+ var body = res.Should().HaveStatusCode(200)
+ .And.HaveJsonBody<UserInfo>()
+ .Which;
+ body.Administrator.Should().Be(true);
+ body.Nickname.Should().Be("aaa");
}
{
diff --git a/Timeline/Controllers/PersonalTimelineController.cs b/Timeline/Controllers/PersonalTimelineController.cs
index 27618c41..11353bb5 100644
--- a/Timeline/Controllers/PersonalTimelineController.cs
+++ b/Timeline/Controllers/PersonalTimelineController.cs
@@ -77,14 +77,15 @@ namespace Timeline.Controllers
[HttpPatch("users/{username}/timeline")]
[Authorize]
- public async Task<ActionResult> TimelinePatch([FromRoute][Username] string username, [FromBody] TimelinePatchRequest body)
+ public async Task<ActionResult<BaseTimelineInfo>> TimelinePatch([FromRoute][Username] string username, [FromBody] TimelinePatchRequest body)
{
if (!this.IsAdministrator() && !(User.Identity.Name == username))
{
return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid());
}
await _service.ChangeProperty(username, body);
- return Ok();
+ var timeline = await _service.GetTimeline(username);
+ return Ok(timeline);
}
[HttpPut("users/{username}/timeline/members/{member}")]
diff --git a/Timeline/Controllers/UserController.cs b/Timeline/Controllers/UserController.cs
index 26e63f63..a3e8d816 100644
--- a/Timeline/Controllers/UserController.cs
+++ b/Timeline/Controllers/UserController.cs
@@ -55,14 +55,14 @@ namespace Timeline.Controllers
}
[HttpPatch("users/{username}"), Authorize]
- public async Task<ActionResult> Patch([FromBody] UserPatchRequest body, [FromRoute][Username] string username)
+ public async Task<ActionResult<UserInfo>> Patch([FromBody] UserPatchRequest body, [FromRoute][Username] string username)
{
if (this.IsAdministrator())
{
try
{
- await _userService.ModifyUser(username, _mapper.Map<User>(body));
- return Ok();
+ var user = await _userService.ModifyUser(username, _mapper.Map<User>(body));
+ return Ok(ConvertToUserInfo(user));
}
catch (UserNotExistException e)
{
@@ -92,8 +92,8 @@ namespace Timeline.Controllers
return StatusCode(StatusCodes.Status403Forbidden,
ErrorResponse.Common.CustomMessage_Forbid(UserController_Patch_Forbid_Administrator));
- await _userService.ModifyUser(this.GetUserId(), _mapper.Map<User>(body));
- return Ok();
+ var user = await _userService.ModifyUser(this.GetUserId(), _mapper.Map<User>(body));
+ return Ok(ConvertToUserInfo(user));
}
}
diff --git a/Timeline/Services/TimelineService.cs b/Timeline/Services/TimelineService.cs
index 85445973..0ea68265 100644
--- a/Timeline/Services/TimelineService.cs
+++ b/Timeline/Services/TimelineService.cs
@@ -81,21 +81,6 @@ namespace Timeline.Services
Task DeletePost(string name, long id);
/// <summary>
- /// Set the properties of a timeline.
- /// </summary>
- /// <param name="name">Username or the timeline name. See remarks of <see cref="IBaseTimelineService"/>.</param>
- /// <param name="newProperties">The new properties. Null member means not to change.</param>
- /// <exception cref="ArgumentNullException">Thrown when <paramref name="name"/> or <paramref name="newProperties"/> is null.</exception>
- /// <exception cref="ArgumentException">Thrown when <paramref name="name"/> is illegal. It is not a valid timeline name (for normal timeline service) or a valid username (for personal timeline service).</exception>
- /// <exception cref="TimelineNotExistException">
- /// Thrown when timeline does not exist.
- /// For normal timeline, it means the name does not exist.
- /// For personal timeline, it means the user of that username does not exist
- /// and the inner exception should be a <see cref="UserNotExistException"/>.
- /// </exception>
- Task ChangeProperty(string name, TimelinePatchRequest newProperties);
-
- /// <summary>
/// Remove members to a timeline.
/// </summary>
/// <param name="name">Username or the timeline name. See remarks of <see cref="IBaseTimelineService"/>.</param>
@@ -235,6 +220,23 @@ namespace Timeline.Services
/// Thrown when the user does not exist. Inner exception MUST be <see cref="UserNotExistException"/>.
/// </exception>
Task<BaseTimelineInfo> GetTimeline(string username);
+
+ /// <summary>
+ /// Set the properties of a timeline.
+ /// </summary>
+ /// <param name="name">Username or the timeline name. See remarks of <see cref="IBaseTimelineService"/>.</param>
+ /// <param name="newProperties">The new properties. Null member means not to change.</param>
+ /// <exception cref="ArgumentNullException">
+ /// Thrown when <paramref name="username"/> is null.
+ /// </exception>
+ /// <exception cref="ArgumentException">
+ /// Thrown when <paramref name="username"/> is of bad format.
+ /// </exception>
+ /// <exception cref="TimelineNotExistException">
+ /// Thrown when the user does not exist. Inner exception MUST be <see cref="UserNotExistException"/>.
+ /// </exception>
+ Task ChangeProperty(string name, TimelinePatchRequest newProperties);
+
}
public abstract class BaseTimelineService : IBaseTimelineService
diff --git a/Timeline/Services/UserService.cs b/Timeline/Services/UserService.cs
index 93d92740..7dc7159d 100644
--- a/Timeline/Services/UserService.cs
+++ b/Timeline/Services/UserService.cs
@@ -82,6 +82,7 @@ namespace Timeline.Services
/// </summary>
/// <param name="id">The id of the user.</param>
/// <param name="info">The new info. May be null.</param>
+ /// <returns>The new user info.</returns>
/// <exception cref="ArgumentException">Thrown when some fields in <paramref name="info"/> is bad.</exception>
/// <exception cref="UserNotExistException">Thrown when user with given id does not exist.</exception>
/// <remarks>
@@ -96,13 +97,14 @@ namespace Timeline.Services
///
/// </remarks>
/// <seealso cref="ModifyUser(string, User)"/>
- Task ModifyUser(long id, User? info);
+ Task<User> ModifyUser(long id, User? info);
/// <summary>
/// Modify a user's info.
/// </summary>
/// <param name="username">The username of the user.</param>
/// <param name="info">The new info. May be null.</param>
+ /// <returns>The new user info.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="username"/> is null.</exception>
/// <exception cref="ArgumentException">Thrown when <paramref name="username"/> is of bad format or some fields in <paramref name="info"/> is bad.</exception>
/// <exception cref="UserNotExistException">Thrown when user with given id does not exist.</exception>
@@ -120,7 +122,7 @@ namespace Timeline.Services
/// Note: Whether <see cref="User.Version"/> is set or not, version will increase and not set to the specified value if there is one.
/// </remarks>
/// <seealso cref="ModifyUser(long, User)"/>
- Task ModifyUser(string username, User? info);
+ Task<User> ModifyUser(string username, User? info);
/// <summary>
/// Delete a user of given id.
@@ -370,7 +372,7 @@ namespace Timeline.Services
}
- public async Task ModifyUser(long id, User? info)
+ public async Task<User> ModifyUser(long id, User? info)
{
ValidateModifyUserInfo(info);
@@ -382,9 +384,11 @@ namespace Timeline.Services
await _databaseContext.SaveChangesAsync();
_logger.LogInformation(LogDatabaseUpdate, ("Id", id));
+
+ return CreateUserFromEntity(entity);
}
- public async Task ModifyUser(string username, User? info)
+ public async Task<User> ModifyUser(string username, User? info)
{
if (username == null)
throw new ArgumentNullException(nameof(username));
@@ -400,6 +404,8 @@ namespace Timeline.Services
await _databaseContext.SaveChangesAsync();
_logger.LogInformation(LogDatabaseUpdate, ("Username", username));
+
+ return CreateUserFromEntity(entity);
}
public async Task<bool> DeleteUser(long id)