From 873d0d8df10e1d6403b7a4eac1980f874dfe1d05 Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 31 Jan 2020 22:46:17 +0800 Subject: Make all patch return the new entity. --- Timeline/Controllers/PersonalTimelineController.cs | 5 ++-- Timeline/Controllers/UserController.cs | 10 +++---- Timeline/Services/TimelineService.cs | 32 ++++++++++++---------- Timeline/Services/UserService.cs | 14 +++++++--- 4 files changed, 35 insertions(+), 26 deletions(-) (limited to 'Timeline') 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 TimelinePatch([FromRoute][Username] string username, [FromBody] TimelinePatchRequest body) + public async Task> 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 Patch([FromBody] UserPatchRequest body, [FromRoute][Username] string username) + public async Task> Patch([FromBody] UserPatchRequest body, [FromRoute][Username] string username) { if (this.IsAdministrator()) { try { - await _userService.ModifyUser(username, _mapper.Map(body)); - return Ok(); + var user = await _userService.ModifyUser(username, _mapper.Map(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(body)); - return Ok(); + var user = await _userService.ModifyUser(this.GetUserId(), _mapper.Map(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 @@ -80,21 +80,6 @@ namespace Timeline.Services /// Task DeletePost(string name, long id); - /// - /// Set the properties of a timeline. - /// - /// Username or the timeline name. See remarks of . - /// The new properties. Null member means not to change. - /// Thrown when or is null. - /// Thrown when is illegal. It is not a valid timeline name (for normal timeline service) or a valid username (for personal timeline service). - /// - /// 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 . - /// - Task ChangeProperty(string name, TimelinePatchRequest newProperties); - /// /// Remove members to a timeline. /// @@ -235,6 +220,23 @@ namespace Timeline.Services /// Thrown when the user does not exist. Inner exception MUST be . /// Task GetTimeline(string username); + + /// + /// Set the properties of a timeline. + /// + /// Username or the timeline name. See remarks of . + /// The new properties. Null member means not to change. + /// + /// Thrown when is null. + /// + /// + /// Thrown when is of bad format. + /// + /// + /// Thrown when the user does not exist. Inner exception MUST be . + /// + 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 /// /// The id of the user. /// The new info. May be null. + /// The new user info. /// Thrown when some fields in is bad. /// Thrown when user with given id does not exist. /// @@ -96,13 +97,14 @@ namespace Timeline.Services /// /// /// - Task ModifyUser(long id, User? info); + Task ModifyUser(long id, User? info); /// /// Modify a user's info. /// /// The username of the user. /// The new info. May be null. + /// The new user info. /// Thrown when is null. /// Thrown when is of bad format or some fields in is bad. /// Thrown when user with given id does not exist. @@ -120,7 +122,7 @@ namespace Timeline.Services /// Note: Whether is set or not, version will increase and not set to the specified value if there is one. /// /// - Task ModifyUser(string username, User? info); + Task ModifyUser(string username, User? info); /// /// Delete a user of given id. @@ -370,7 +372,7 @@ namespace Timeline.Services } - public async Task ModifyUser(long id, User? info) + public async Task 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 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 DeleteUser(long id) -- cgit v1.2.3