Compare commits
No commits in common. "94a6f234c58af14d17ce0d1b9e6dbf63b303f4f2" and "e86ab4f2091ce9db1f97b1bbeb31dd00935721ef" have entirely different histories.
94a6f234c5
...
e86ab4f209
8 changed files with 5 additions and 135 deletions
|
|
@ -2,9 +2,6 @@ package ru.team58.profileservice.controller;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.*;
|
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.CreateUserRequest;
|
||||||
import ru.team58.profileservice.controller.schemes.GetUserResponse;
|
import ru.team58.profileservice.controller.schemes.GetUserResponse;
|
||||||
import ru.team58.profileservice.controller.schemes.UpdateUserRequest;
|
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.mapper.UserMapper;
|
||||||
import ru.team58.profileservice.service.UserDTO;
|
import ru.team58.profileservice.service.UserDTO;
|
||||||
import ru.team58.profileservice.service.UserService;
|
import ru.team58.profileservice.service.UserService;
|
||||||
import ru.team58.profileservice.service.exceptions.UserAlreadyExistsException;
|
|
||||||
|
|
||||||
import java.security.Principal;
|
import java.security.Principal;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
@ -24,88 +20,33 @@ public class UserController {
|
||||||
|
|
||||||
@GetMapping("/")
|
@GetMapping("/")
|
||||||
public UserResponse getMe(Principal principal) {
|
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()));
|
return UserMapper.INSTANCE.toUserResponse(userService.getByUsername(principal.getName()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping("/")
|
@PutMapping("/")
|
||||||
public void updateMe(Principal principal, @RequestBody UpdateUserRequest request) {
|
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));
|
userService.updateUser(UserMapper.INSTANCE.toUserDTO(request));
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/")
|
@DeleteMapping("/")
|
||||||
public void deleteMe(Principal principal) {
|
public void deleteMe(Principal principal) {
|
||||||
if (principal == null) {
|
UserDTO user = userService.getByUsername(principal.getName());
|
||||||
throw new UnauthorizedException();
|
|
||||||
}
|
|
||||||
|
|
||||||
UserDTO user;
|
|
||||||
|
|
||||||
try {
|
|
||||||
user = userService.getByUsername(principal.getName());
|
|
||||||
} catch (Exception ex) {
|
|
||||||
throw new UserNotFoundException();
|
|
||||||
}
|
|
||||||
|
|
||||||
userService.deleteUser(user.getId());
|
userService.deleteUser(user.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/")
|
@PostMapping("/")
|
||||||
public void createMe(@RequestBody CreateUserRequest request) {
|
public void createMe(@RequestBody CreateUserRequest request) {
|
||||||
UserDTO user = UserMapper.INSTANCE.toUserDTO(request);
|
userService.createUser(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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
public GetUserResponse getUser(@PathVariable UUID id) {
|
public GetUserResponse getUser(@PathVariable UUID id) {
|
||||||
UserDTO user;
|
UserDTO user = userService.getById(id);
|
||||||
|
|
||||||
try {
|
|
||||||
user = userService.getById(id);
|
|
||||||
} catch (ru.team58.profileservice.service.exceptions.UserNotFoundException ex) {
|
|
||||||
throw new UserNotFoundException();
|
|
||||||
}
|
|
||||||
|
|
||||||
return UserMapper.INSTANCE.toGetUserResponse(user);
|
return UserMapper.INSTANCE.toGetUserResponse(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/{id}")
|
@DeleteMapping("/{id}")
|
||||||
public void deleteUser(Principal principal, @PathVariable UUID id) {
|
public void deleteUser(@PathVariable UUID id) {
|
||||||
// TODO: Add checking role
|
|
||||||
if (principal == null) {
|
|
||||||
throw new UnauthorizedException();
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
userService.deleteUser(id);
|
userService.deleteUser(id);
|
||||||
} catch (ru.team58.profileservice.service.exceptions.UserNotFoundException ex) {
|
|
||||||
throw new UserNotFoundException();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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;
|
|
||||||
}
|
|
||||||
|
|
@ -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<ErrorDetails> handleUserNotFoundException(UserNotFoundException ex) {
|
|
||||||
ErrorDetails errorDetails = new ErrorDetails("User not found", HttpStatus.FORBIDDEN.value());
|
|
||||||
return new ResponseEntity<>(errorDetails, HttpStatus.FORBIDDEN);
|
|
||||||
}
|
|
||||||
|
|
||||||
@ExceptionHandler(UserAlreadyExistsException.class)
|
|
||||||
public ResponseEntity<ErrorDetails> 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<ErrorDetails> 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<ErrorDetails> handleForbiddenException(ForbiddenException ex) {
|
|
||||||
ErrorDetails errorDetails = new ErrorDetails("Forbidden", HttpStatus.FORBIDDEN.value());
|
|
||||||
return new ResponseEntity<>(errorDetails, HttpStatus.FORBIDDEN);
|
|
||||||
}
|
|
||||||
|
|
||||||
@ExceptionHandler(UnauthorizedException.class)
|
|
||||||
public ResponseEntity<ErrorDetails> handleUnauthorizedException(UnauthorizedException ex) {
|
|
||||||
ErrorDetails errorDetails = new ErrorDetails("Unauthorized", HttpStatus.UNAUTHORIZED.value());
|
|
||||||
return new ResponseEntity<>(errorDetails, HttpStatus.UNAUTHORIZED);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
package ru.team58.profileservice.controller.exceptions;
|
|
||||||
|
|
||||||
public class BadRequestException extends RuntimeException {
|
|
||||||
}
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
package ru.team58.profileservice.controller.exceptions;
|
|
||||||
|
|
||||||
public class ForbiddenException extends RuntimeException {
|
|
||||||
}
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
package ru.team58.profileservice.controller.exceptions;
|
|
||||||
|
|
||||||
public class UnauthorizedException extends RuntimeException {
|
|
||||||
}
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
package ru.team58.profileservice.controller.exceptions;
|
|
||||||
|
|
||||||
public class UserAlreadyExistsException extends BadRequestException{
|
|
||||||
}
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
package ru.team58.profileservice.controller.exceptions;
|
|
||||||
|
|
||||||
public class UserNotFoundException extends ForbiddenException {
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue