Compare commits

..

2 commits

Author SHA1 Message Date
KamilM1205
94a6f234c5 Fix: removed unused imports. 2025-06-29 23:53:56 +04:00
KamilM1205
a4bdcda165 Add: Response exceptions. Handling exceptions. 2025-06-29 23:52:52 +04:00
8 changed files with 135 additions and 5 deletions

View file

@ -2,6 +2,9 @@ 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;
@ -9,6 +12,7 @@ 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;
@ -20,33 +24,88 @@ 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) {
UserDTO user = userService.getByUsername(principal.getName());
if (principal == null) {
throw new UnauthorizedException();
}
UserDTO user;
try {
user = userService.getByUsername(principal.getName());
} catch (Exception ex) {
throw new UserNotFoundException();
}
userService.deleteUser(user.getId());
}
@PostMapping("/")
public void createMe(@RequestBody CreateUserRequest request) {
userService.createUser(UserMapper.INSTANCE.toUserDTO(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();
}
}
@GetMapping("/{id}")
public GetUserResponse getUser(@PathVariable UUID id) {
UserDTO user = userService.getById(id);
UserDTO user;
try {
user = userService.getById(id);
} catch (ru.team58.profileservice.service.exceptions.UserNotFoundException ex) {
throw new UserNotFoundException();
}
return UserMapper.INSTANCE.toGetUserResponse(user);
}
@DeleteMapping("/{id}")
public void deleteUser(@PathVariable UUID id) {
userService.deleteUser(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();
}
}
}

View file

@ -0,0 +1,11 @@
package ru.team58.profileservice.controller.advice;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public class ErrorDetails {
private String message;
private int status;
}

View file

@ -0,0 +1,40 @@
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);
}
}

View file

@ -0,0 +1,4 @@
package ru.team58.profileservice.controller.exceptions;
public class BadRequestException extends RuntimeException {
}

View file

@ -0,0 +1,4 @@
package ru.team58.profileservice.controller.exceptions;
public class ForbiddenException extends RuntimeException {
}

View file

@ -0,0 +1,4 @@
package ru.team58.profileservice.controller.exceptions;
public class UnauthorizedException extends RuntimeException {
}

View file

@ -0,0 +1,4 @@
package ru.team58.profileservice.controller.exceptions;
public class UserAlreadyExistsException extends BadRequestException{
}

View file

@ -0,0 +1,4 @@
package ru.team58.profileservice.controller.exceptions;
public class UserNotFoundException extends ForbiddenException {
}