diff options
author | crupest <crupest@outlook.com> | 2021-04-30 16:52:55 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-04-30 16:52:55 +0800 |
commit | 88002173a1155883d1fb46683a9a7ad1f521eb56 (patch) | |
tree | 742cce92b6e424d7bcd4a51a8da63dd10e18c4c7 /BackEnd/Timeline/Controllers/UserController.cs | |
parent | bf4c48980f81e566065c07f3fe534ce7551ebdcc (diff) | |
download | timeline-88002173a1155883d1fb46683a9a7ad1f521eb56.tar.gz timeline-88002173a1155883d1fb46683a9a7ad1f521eb56.tar.bz2 timeline-88002173a1155883d1fb46683a9a7ad1f521eb56.zip |
refactor: ...
Diffstat (limited to 'BackEnd/Timeline/Controllers/UserController.cs')
-rw-r--r-- | BackEnd/Timeline/Controllers/UserController.cs | 53 |
1 files changed, 10 insertions, 43 deletions
diff --git a/BackEnd/Timeline/Controllers/UserController.cs b/BackEnd/Timeline/Controllers/UserController.cs index c0ae6a09..bdf9c0b7 100644 --- a/BackEnd/Timeline/Controllers/UserController.cs +++ b/BackEnd/Timeline/Controllers/UserController.cs @@ -6,7 +6,6 @@ using System.Threading.Tasks; using Timeline.Auth;
using Timeline.Models.Http;
using Timeline.Models.Validation;
-using Timeline.Services;
using Timeline.Services.Mapper;
using Timeline.Services.User;
@@ -59,16 +58,10 @@ namespace Timeline.Controllers [ProducesResponseType(StatusCodes.Status403Forbidden)]
public async Task<ActionResult<HttpUser>> Post([FromBody] HttpUserPostRequest body)
{
- try
- {
- var user = await _userService.CreateUserAsync(
- new CreateUserParams(body.Username, body.Password) { Nickname = body.Nickname });
- return await _mapper.MapAsync<HttpUser>(user, Url, User);
- }
- catch (EntityAlreadyExistException e) when (e.EntityName == EntityNames.User)
- {
- return BadRequest(ErrorResponse.UserController.UsernameConflict());
- }
+
+ var user = await _userService.CreateUserAsync(
+ new CreateUserParams(body.Username, body.Password) { Nickname = body.Nickname });
+ return await _mapper.MapAsync<HttpUser>(user, Url, User);
}
/// <summary>
@@ -81,16 +74,9 @@ namespace Timeline.Controllers [ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<HttpUser>> Get([FromRoute][Username] string username)
{
- try
- {
- var id = await _userService.GetUserIdByUsernameAsync(username);
- var user = await _userService.GetUserAsync(id);
- return await _mapper.MapAsync<HttpUser>(user, Url, User);
- }
- catch (UserNotExistException)
- {
- return NotFound(ErrorResponse.UserCommon.NotExist());
- }
+ var id = await _userService.GetUserIdByUsernameAsync(username);
+ var user = await _userService.GetUserAsync(id);
+ return await _mapper.MapAsync<HttpUser>(user, Url, User);
}
/// <summary>
@@ -109,20 +95,9 @@ namespace Timeline.Controllers {
if (UserHasUserManagementPermission)
{
- try
- {
- var id = await _userService.GetUserIdByUsernameAsync(username);
- var user = await _userService.ModifyUserAsync(id, _mapper.AutoMapperMap<ModifyUserParams>(body));
- return await _mapper.MapAsync<HttpUser>(user, Url, User);
- }
- catch (UserNotExistException)
- {
- return NotFound(ErrorResponse.UserCommon.NotExist());
- }
- catch (EntityAlreadyExistException e) when (e.EntityName == EntityNames.User)
- {
- return BadRequest(ErrorResponse.UserController.UsernameConflict());
- }
+ var id = await _userService.GetUserIdByUsernameAsync(username);
+ var user = await _userService.ModifyUserAsync(id, _mapper.AutoMapperMap<ModifyUserParams>(body));
+ return await _mapper.MapAsync<HttpUser>(user, Url, User);
}
else
{
@@ -204,10 +179,6 @@ namespace Timeline.Controllers await _userPermissionService.AddPermissionToUserAsync(id, permission);
return Ok();
}
- catch (UserNotExistException)
- {
- return NotFound(ErrorResponse.UserCommon.NotExist());
- }
catch (InvalidOperationOnRootUserException)
{
return BadRequest(ErrorResponse.UserController.ChangePermission_RootUser());
@@ -228,10 +199,6 @@ namespace Timeline.Controllers await _userPermissionService.RemovePermissionFromUserAsync(id, permission);
return Ok();
}
- catch (UserNotExistException)
- {
- return NotFound(ErrorResponse.UserCommon.NotExist());
- }
catch (InvalidOperationOnRootUserException)
{
return BadRequest(ErrorResponse.UserController.ChangePermission_RootUser());
|