diff options
author | crupest <crupest@outlook.com> | 2021-05-03 00:34:30 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-05-03 00:34:30 +0800 |
commit | cc6d33511a6a4c43630a36b13d03db57f432520c (patch) | |
tree | 01faaeebbfce856a5434495367a0163ee08f18b6 /BackEnd/Timeline | |
parent | 1908601e92218e1b2271f574f107056e2d9e04d6 (diff) | |
download | timeline-cc6d33511a6a4c43630a36b13d03db57f432520c.tar.gz timeline-cc6d33511a6a4c43630a36b13d03db57f432520c.tar.bz2 timeline-cc6d33511a6a4c43630a36b13d03db57f432520c.zip |
refactor: ...
Diffstat (limited to 'BackEnd/Timeline')
6 files changed, 18 insertions, 15 deletions
diff --git a/BackEnd/Timeline/Controllers/ActionResultControllerExtensions.cs b/BackEnd/Timeline/Controllers/ActionResultControllerExtensions.cs index a3da73fa..a7a5486c 100644 --- a/BackEnd/Timeline/Controllers/ActionResultControllerExtensions.cs +++ b/BackEnd/Timeline/Controllers/ActionResultControllerExtensions.cs @@ -16,6 +16,11 @@ namespace Timeline.Controllers return controller.StatusCode(StatusCodes.Status403Forbidden, new CommonResponse(ErrorCodes.Common.Forbid, message ?? Resource.MessageForbid));
}
+ public static ObjectResult Delete(this ControllerBase controller, bool delete = true)
+ {
+ return controller.StatusCode(StatusCodes.Status200OK, CommonDeleteResponse.Create(delete));
+ }
+
public static BadRequestObjectResult BadRequestWithCommonResponse(this ControllerBase controller, int code, string message)
{
return controller.BadRequest(new CommonResponse(code, message));
diff --git a/BackEnd/Timeline/Controllers/TimelineController.cs b/BackEnd/Timeline/Controllers/TimelineController.cs index f04982dc..7347f135 100644 --- a/BackEnd/Timeline/Controllers/TimelineController.cs +++ b/BackEnd/Timeline/Controllers/TimelineController.cs @@ -243,7 +243,7 @@ namespace Timeline.Controllers }
await _service.DeleteTimelineAsync(timelineId);
- return Ok();
+ return this.Delete();
}
}
}
diff --git a/BackEnd/Timeline/Controllers/TimelinePostController.cs b/BackEnd/Timeline/Controllers/TimelinePostController.cs index 9f69b59b..09e7e624 100644 --- a/BackEnd/Timeline/Controllers/TimelinePostController.cs +++ b/BackEnd/Timeline/Controllers/TimelinePostController.cs @@ -266,7 +266,7 @@ namespace Timeline.Controllers await _postService.DeletePostAsync(timelineId, post);
- return Ok();
+ return this.Delete();
}
}
}
diff --git a/BackEnd/Timeline/Controllers/UserController.cs b/BackEnd/Timeline/Controllers/UserController.cs index ec732caa..47d2ee41 100644 --- a/BackEnd/Timeline/Controllers/UserController.cs +++ b/BackEnd/Timeline/Controllers/UserController.cs @@ -129,11 +129,8 @@ namespace Timeline.Controllers {
try
{
- var delete = await _userDeleteService.DeleteUserAsync(username);
- if (delete)
- return Ok(CommonDeleteResponse.Delete());
- else
- return Ok(CommonDeleteResponse.NotExist());
+ await _userDeleteService.DeleteUserAsync(username);
+ return this.Delete();
}
catch (InvalidOperationOnRootUserException)
{
@@ -187,7 +184,6 @@ namespace Timeline.Controllers [ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
- [ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult> DeleteUserPermission([FromRoute][Username] string username, [FromRoute] UserPermission permission)
{
try
diff --git a/BackEnd/Timeline/Services/User/IUserDeleteService.cs b/BackEnd/Timeline/Services/User/IUserDeleteService.cs index 992a565c..3b872942 100644 --- a/BackEnd/Timeline/Services/User/IUserDeleteService.cs +++ b/BackEnd/Timeline/Services/User/IUserDeleteService.cs @@ -9,10 +9,10 @@ namespace Timeline.Services.User /// Delete a user of given username.
/// </summary>
/// <param name="username">Username of the user to delete. Can't be null.</param>
- /// <returns>True if user is deleted, false if user not exist.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="username"/> is null.</exception>
/// <exception cref="ArgumentException">Thrown when <paramref name="username"/> is of bad format.</exception>
+ /// <exception cref="EntityNotExistException">Thrown when the user does not exist. </exception>
/// <exception cref="InvalidOperationOnRootUserException">Thrown when deleting root user.</exception>
- Task<bool> DeleteUserAsync(string username);
+ Task DeleteUserAsync(string username);
}
}
diff --git a/BackEnd/Timeline/Services/User/UserDeleteService.cs b/BackEnd/Timeline/Services/User/UserDeleteService.cs index 3e3e29e2..c21042df 100644 --- a/BackEnd/Timeline/Services/User/UserDeleteService.cs +++ b/BackEnd/Timeline/Services/User/UserDeleteService.cs @@ -1,6 +1,7 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
+using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
@@ -27,7 +28,7 @@ namespace Timeline.Services.User _timelinePostService = timelinePostService;
}
- public async Task<bool> DeleteUserAsync(string username)
+ public async Task DeleteUserAsync(string username)
{
if (username == null)
throw new ArgumentNullException(nameof(username));
@@ -38,8 +39,11 @@ namespace Timeline.Services.User }
var user = await _databaseContext.Users.Where(u => u.Username == username).SingleOrDefaultAsync();
- if (user == null)
- return false;
+ if (user is null)
+ throw new EntityNotExistException(EntityTypes.User, new Dictionary<string, object>
+ {
+ ["username"] = username
+ });
if (user.Id == 1)
throw new InvalidOperationOnRootUserException(Resource.ExceptionDeleteRootUser);
@@ -50,8 +54,6 @@ namespace Timeline.Services.User await _databaseContext.SaveChangesAsync();
_logger.LogWarning(Resource.LogDeleteUser, user.Username, user.Id);
-
- return true;
}
}
|