diff --git a/rentplace/src/main/java/kattsyn/dev/rentplace/controllers/UserController.java b/rentplace/src/main/java/kattsyn/dev/rentplace/controllers/UserController.java index 0d1f630..66ba7c2 100644 --- a/rentplace/src/main/java/kattsyn/dev/rentplace/controllers/UserController.java +++ b/rentplace/src/main/java/kattsyn/dev/rentplace/controllers/UserController.java @@ -171,4 +171,23 @@ public ResponseEntity deleteUser( return new ResponseEntity<>(HttpStatus.NO_CONTENT); } + @Operation( + summary = "Удалить свой профиль", + description = "Метод, чтобы пользователь мог удалить свой профиль." + ) + @DeleteMapping("/me") + @ApiResponses(value = { + @ApiResponse(responseCode = "204", description = "Успешно. Пустой ответ", content = @Content), + @ApiResponse(responseCode = "404", description = "Пользователь не найден", content = @Content), + @ApiResponse(responseCode = "422", description = "Ошибка валидации", content = @Content), + @ApiResponse(responseCode = "500", description = "Непредвиденная ошибка со стороны сервера", content = @Content) + }) + @SecurityRequirement(name = "JWT") + public ResponseEntity deleteMe( + Authentication authentication + ) { + userService.deleteMe(authentication); + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } + } diff --git a/rentplace/src/main/java/kattsyn/dev/rentplace/services/UserService.java b/rentplace/src/main/java/kattsyn/dev/rentplace/services/UserService.java index 43fb9e1..cf6f1c8 100644 --- a/rentplace/src/main/java/kattsyn/dev/rentplace/services/UserService.java +++ b/rentplace/src/main/java/kattsyn/dev/rentplace/services/UserService.java @@ -5,6 +5,7 @@ import kattsyn.dev.rentplace.dtos.users.UserCreateEditDTO; import kattsyn.dev.rentplace.dtos.users.UserDTO; import kattsyn.dev.rentplace.entities.User; +import org.springframework.security.core.Authentication; import org.springframework.web.multipart.MultipartFile; import java.util.List; @@ -18,8 +19,6 @@ public interface UserService { Optional getUserOptionalByEmail(String email); - boolean existsByEmail(String email); - UserDTO getUserDTOByEmail(String email); UserDTO findById(Long id); @@ -30,6 +29,8 @@ public interface UserService { void deleteById(long id); + void deleteMe(Authentication authentication); + ImageDTO uploadImage(MultipartFile file, long id); UserDTO updateUserById(long id, UserCreateEditDTO userCreateEditDTO); diff --git a/rentplace/src/main/java/kattsyn/dev/rentplace/services/impl/UserServiceImpl.java b/rentplace/src/main/java/kattsyn/dev/rentplace/services/impl/UserServiceImpl.java index 03d4c64..bfde068 100644 --- a/rentplace/src/main/java/kattsyn/dev/rentplace/services/impl/UserServiceImpl.java +++ b/rentplace/src/main/java/kattsyn/dev/rentplace/services/impl/UserServiceImpl.java @@ -18,6 +18,7 @@ import kattsyn.dev.rentplace.services.UserService; import kattsyn.dev.rentplace.utils.PathResolver; import lombok.RequiredArgsConstructor; +import org.springframework.security.core.Authentication; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; @@ -52,11 +53,6 @@ public Optional getUserOptionalByEmail(String email) { return userRepository.findByEmail(email); } - @Override - public boolean existsByEmail(String email) { - return userRepository.findByEmail(email).isPresent(); - } - @Override public UserDTO getUserDTOByEmail(String email) { return userMapper.fromUser(userRepository.findByEmail(email) @@ -99,6 +95,14 @@ public void deleteById(long id) { userRepository.delete(user); } + @Override + @Transactional + public void deleteMe(Authentication authentication) { + User user = getUserByEmail(authentication.getName()); + + userRepository.delete(user); + } + @Transactional @Override public ImageDTO uploadImage(MultipartFile file, long id) {