diff --git a/src/main/java/ru/team58/profileservice/controller/UserController.java b/src/main/java/ru/team58/profileservice/controller/UserController.java index 2c18b1b..70378ed 100644 --- a/src/main/java/ru/team58/profileservice/controller/UserController.java +++ b/src/main/java/ru/team58/profileservice/controller/UserController.java @@ -2,9 +2,6 @@ package ru.team58.profileservice.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; -import ru.team58.profileservice.controller.exceptions.BadRequestException; -import ru.team58.profileservice.controller.exceptions.UnauthorizedException; -import ru.team58.profileservice.controller.exceptions.UserNotFoundException; import ru.team58.profileservice.controller.schemes.CreateUserRequest; import ru.team58.profileservice.controller.schemes.GetUserResponse; import ru.team58.profileservice.controller.schemes.UpdateUserRequest; @@ -12,7 +9,6 @@ import ru.team58.profileservice.controller.schemes.UserResponse; import ru.team58.profileservice.mapper.UserMapper; import ru.team58.profileservice.service.UserDTO; import ru.team58.profileservice.service.UserService; -import ru.team58.profileservice.service.exceptions.UserAlreadyExistsException; import java.security.Principal; import java.util.UUID; @@ -24,88 +20,33 @@ public class UserController { @GetMapping("/") public UserResponse getMe(Principal principal) { - if (principal == null) { - throw new UnauthorizedException(); - } - - try { - userService.getByUsername(principal.getName()); - } catch (Exception ex) { - throw new UserNotFoundException(); - } - return UserMapper.INSTANCE.toUserResponse(userService.getByUsername(principal.getName())); } @PutMapping("/") public void updateMe(Principal principal, @RequestBody UpdateUserRequest request) { - if (principal == null) { - throw new UnauthorizedException(); - } - - try { - userService.getByUsername(principal.getName()); - } catch (Exception ex) { - throw new UserNotFoundException(); - } - userService.updateUser(UserMapper.INSTANCE.toUserDTO(request)); } @DeleteMapping("/") public void deleteMe(Principal principal) { - if (principal == null) { - throw new UnauthorizedException(); - } - - UserDTO user; - - try { - user = userService.getByUsername(principal.getName()); - } catch (Exception ex) { - throw new UserNotFoundException(); - } - + UserDTO user = userService.getByUsername(principal.getName()); userService.deleteUser(user.getId()); } @PostMapping("/") public void createMe(@RequestBody CreateUserRequest request) { - UserDTO user = UserMapper.INSTANCE.toUserDTO(request); - - try { - userService.createUser(user); - } catch (UserAlreadyExistsException ex) { - throw new ru.team58.profileservice.controller.exceptions.UserAlreadyExistsException(); - } catch (IllegalArgumentException ex) { - throw new BadRequestException(); - } + userService.createUser(UserMapper.INSTANCE.toUserDTO(request)); } @GetMapping("/{id}") public GetUserResponse getUser(@PathVariable UUID id) { - UserDTO user; - - try { - user = userService.getById(id); - } catch (ru.team58.profileservice.service.exceptions.UserNotFoundException ex) { - throw new UserNotFoundException(); - } - + UserDTO user = userService.getById(id); return UserMapper.INSTANCE.toGetUserResponse(user); } @DeleteMapping("/{id}") - public void deleteUser(Principal principal, @PathVariable UUID id) { - // TODO: Add checking role - if (principal == null) { - throw new UnauthorizedException(); - } - - try { - userService.deleteUser(id); - } catch (ru.team58.profileservice.service.exceptions.UserNotFoundException ex) { - throw new UserNotFoundException(); - } + public void deleteUser(@PathVariable UUID id) { + userService.deleteUser(id); } } diff --git a/src/main/java/ru/team58/profileservice/controller/advice/ErrorDetails.java b/src/main/java/ru/team58/profileservice/controller/advice/ErrorDetails.java deleted file mode 100644 index e9026b3..0000000 --- a/src/main/java/ru/team58/profileservice/controller/advice/ErrorDetails.java +++ /dev/null @@ -1,11 +0,0 @@ -package ru.team58.profileservice.controller.advice; - -import lombok.AllArgsConstructor; -import lombok.Getter; - -@Getter -@AllArgsConstructor -public class ErrorDetails { - private String message; - private int status; -} diff --git a/src/main/java/ru/team58/profileservice/controller/advice/UserControllerExceptionHandler.java b/src/main/java/ru/team58/profileservice/controller/advice/UserControllerExceptionHandler.java deleted file mode 100644 index bd0d990..0000000 --- a/src/main/java/ru/team58/profileservice/controller/advice/UserControllerExceptionHandler.java +++ /dev/null @@ -1,40 +0,0 @@ -package ru.team58.profileservice.controller.advice; - -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.ControllerAdvice; -import org.springframework.web.bind.annotation.ExceptionHandler; -import ru.team58.profileservice.controller.exceptions.*; - -@ControllerAdvice -public class UserControllerExceptionHandler { - @ExceptionHandler(UserNotFoundException.class) - public ResponseEntity handleUserNotFoundException(UserNotFoundException ex) { - ErrorDetails errorDetails = new ErrorDetails("User not found", HttpStatus.FORBIDDEN.value()); - return new ResponseEntity<>(errorDetails, HttpStatus.FORBIDDEN); - } - - @ExceptionHandler(UserAlreadyExistsException.class) - public ResponseEntity handleUserAlreadyExistsException(UserAlreadyExistsException ex) { - ErrorDetails errorDetails = new ErrorDetails("User already exists", HttpStatus.BAD_REQUEST.value()); - return new ResponseEntity<>(errorDetails, HttpStatus.BAD_REQUEST); - } - - @ExceptionHandler(BadRequestException.class) - public ResponseEntity handleBadRequestException(BadRequestException ex) { - ErrorDetails errorDetails = new ErrorDetails("Bad Request", HttpStatus.BAD_REQUEST.value()); - return new ResponseEntity<>(errorDetails, HttpStatus.BAD_REQUEST); - } - - @ExceptionHandler(ForbiddenException.class) - public ResponseEntity handleForbiddenException(ForbiddenException ex) { - ErrorDetails errorDetails = new ErrorDetails("Forbidden", HttpStatus.FORBIDDEN.value()); - return new ResponseEntity<>(errorDetails, HttpStatus.FORBIDDEN); - } - - @ExceptionHandler(UnauthorizedException.class) - public ResponseEntity handleUnauthorizedException(UnauthorizedException ex) { - ErrorDetails errorDetails = new ErrorDetails("Unauthorized", HttpStatus.UNAUTHORIZED.value()); - return new ResponseEntity<>(errorDetails, HttpStatus.UNAUTHORIZED); - } -} diff --git a/src/main/java/ru/team58/profileservice/controller/exceptions/BadRequestException.java b/src/main/java/ru/team58/profileservice/controller/exceptions/BadRequestException.java deleted file mode 100644 index 48fd739..0000000 --- a/src/main/java/ru/team58/profileservice/controller/exceptions/BadRequestException.java +++ /dev/null @@ -1,4 +0,0 @@ -package ru.team58.profileservice.controller.exceptions; - -public class BadRequestException extends RuntimeException { -} diff --git a/src/main/java/ru/team58/profileservice/controller/exceptions/ForbiddenException.java b/src/main/java/ru/team58/profileservice/controller/exceptions/ForbiddenException.java deleted file mode 100644 index 4461ccc..0000000 --- a/src/main/java/ru/team58/profileservice/controller/exceptions/ForbiddenException.java +++ /dev/null @@ -1,4 +0,0 @@ -package ru.team58.profileservice.controller.exceptions; - -public class ForbiddenException extends RuntimeException { -} diff --git a/src/main/java/ru/team58/profileservice/controller/exceptions/UnauthorizedException.java b/src/main/java/ru/team58/profileservice/controller/exceptions/UnauthorizedException.java deleted file mode 100644 index c787bb2..0000000 --- a/src/main/java/ru/team58/profileservice/controller/exceptions/UnauthorizedException.java +++ /dev/null @@ -1,4 +0,0 @@ -package ru.team58.profileservice.controller.exceptions; - -public class UnauthorizedException extends RuntimeException { -} diff --git a/src/main/java/ru/team58/profileservice/controller/exceptions/UserAlreadyExistsException.java b/src/main/java/ru/team58/profileservice/controller/exceptions/UserAlreadyExistsException.java deleted file mode 100644 index 9108862..0000000 --- a/src/main/java/ru/team58/profileservice/controller/exceptions/UserAlreadyExistsException.java +++ /dev/null @@ -1,4 +0,0 @@ -package ru.team58.profileservice.controller.exceptions; - -public class UserAlreadyExistsException extends BadRequestException{ -} diff --git a/src/main/java/ru/team58/profileservice/controller/exceptions/UserNotFoundException.java b/src/main/java/ru/team58/profileservice/controller/exceptions/UserNotFoundException.java deleted file mode 100644 index 0e95904..0000000 --- a/src/main/java/ru/team58/profileservice/controller/exceptions/UserNotFoundException.java +++ /dev/null @@ -1,4 +0,0 @@ -package ru.team58.profileservice.controller.exceptions; - -public class UserNotFoundException extends ForbiddenException { -}