diff --git a/sql/schema.sql b/sql/schema.sql index 2f3f0e19..d0e7c86f 100644 --- a/sql/schema.sql +++ b/sql/schema.sql @@ -183,6 +183,7 @@ create table user ( profile_image_url varchar(255), created_at datetime, modified_at datetime, + is_deleted tinyint(1), primary key (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; diff --git a/src/main/java/com/cvsgo/controller/UserController.java b/src/main/java/com/cvsgo/controller/UserController.java index b66f1c14..e2424369 100644 --- a/src/main/java/com/cvsgo/controller/UserController.java +++ b/src/main/java/com/cvsgo/controller/UserController.java @@ -70,6 +70,12 @@ public SuccessResponse updateUser(@LoginUser User user, return SuccessResponse.create(); } + @DeleteMapping("/user") + public SuccessResponse deleteUser(@LoginUser User user) { + userService.deleteUser(user); + return SuccessResponse.create(); + } + @PostMapping("/users/{userId}/followers") @ResponseStatus(HttpStatus.CREATED) public SuccessResponse createUserFollow(@LoginUser User user, @PathVariable Long userId) { diff --git a/src/main/java/com/cvsgo/entity/User.java b/src/main/java/com/cvsgo/entity/User.java index db5f6a14..6e7d3f8a 100644 --- a/src/main/java/com/cvsgo/entity/User.java +++ b/src/main/java/com/cvsgo/entity/User.java @@ -18,9 +18,11 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import org.hibernate.annotations.SQLDelete; import org.springframework.security.crypto.password.PasswordEncoder; @Getter +@SQLDelete(sql = "UPDATE user SET is_deleted = true WHERE id = ?") @NoArgsConstructor(access = AccessLevel.PROTECTED) @Entity public class User extends BaseTimeEntity { @@ -46,7 +48,9 @@ public class User extends BaseTimeEntity { private String profileImageUrl; - @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true) + private Boolean isDeleted = Boolean.FALSE; + + @OneToMany(mappedBy = "user", cascade = CascadeType.PERSIST) private List userTags = new ArrayList<>(); @Builder diff --git a/src/main/java/com/cvsgo/repository/RefreshTokenRepository.java b/src/main/java/com/cvsgo/repository/RefreshTokenRepository.java index 1bba77a6..dbe3e615 100644 --- a/src/main/java/com/cvsgo/repository/RefreshTokenRepository.java +++ b/src/main/java/com/cvsgo/repository/RefreshTokenRepository.java @@ -1,11 +1,13 @@ package com.cvsgo.repository; import com.cvsgo.entity.RefreshToken; -import org.springframework.data.jpa.repository.JpaRepository; - +import com.cvsgo.entity.User; import java.util.Optional; +import org.springframework.data.jpa.repository.JpaRepository; public interface RefreshTokenRepository extends JpaRepository { Optional findByToken(String token); + + void deleteAllByUser(User user); } diff --git a/src/main/java/com/cvsgo/service/UserService.java b/src/main/java/com/cvsgo/service/UserService.java index 068b6da5..cfaa54e3 100644 --- a/src/main/java/com/cvsgo/service/UserService.java +++ b/src/main/java/com/cvsgo/service/UserService.java @@ -19,6 +19,7 @@ import com.cvsgo.exception.BadRequestException; import com.cvsgo.exception.DuplicateException; import com.cvsgo.exception.NotFoundException; +import com.cvsgo.repository.RefreshTokenRepository; import com.cvsgo.repository.ReviewRepository; import com.cvsgo.repository.TagRepository; import com.cvsgo.repository.UserFollowRepository; @@ -46,6 +47,8 @@ public class UserService { private final UserFollowRepository userFollowRepository; + private final RefreshTokenRepository refreshTokenRepository; + private final ReviewRepository reviewRepository; private final PasswordEncoder passwordEncoder; @@ -139,6 +142,17 @@ public void updateUser(User user, UpdateUserRequestDto request) { user.updateProfileImageUrl(request.getProfileImageUrl()); } + /** + * 사용자를 논리 삭제한다. + * + * @param user 로그인한 사용자 + */ + @Transactional + public void deleteUser(User user) { + refreshTokenRepository.deleteAllByUser(user); + userRepository.delete(user); + } + /** * 회원 팔로우를 생성한다. *