Skip to content

Commit b96ae96

Browse files
committed
feat: 회원탈퇴 기능 구현
1 parent 5710915 commit b96ae96

File tree

6 files changed

+54
-12
lines changed

6 files changed

+54
-12
lines changed

src/main/java/com/threestar/trainus/domain/comment/repository/CommentRepository.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,38 @@ public interface CommentRepository extends JpaRepository<Comment, Long> {
1515
@Query(value = """
1616
select comments.comment_id, comments.lesson_id, comments.user_id, comments.content,
1717
comments.parent_comment_id, comments.deleted, comments.created_at, comments.updated_at
18-
from ( select comment_id from comments where lesson_id = :lessonId order by parent_comment_id asc, comment_id asc limit :limit offset :offset
18+
from (
19+
select comment_id
20+
from comments c
21+
join users u on c.user_id = u.id and u.deleted_at IS NULL
22+
where lesson_id = :lessonId
23+
order by parent_comment_id asc, comment_id asc
24+
limit :limit offset :offset
1925
) t left join comments on t.comment_id = comments.comment_id
2026
""", nativeQuery = true)
2127
List<Comment> findAll(@Param("lessonId") Long lessonId, @Param("offset") int offset, @Param("limit") int limit);
2228

2329
@Query(value = """
24-
select count(*) from ( select comment_id from comments where lesson_id = :lessonId limit :limit) t
30+
select count(*) from (
31+
select comment_id
32+
from comments c
33+
join users u on c.user_id = u.id and u.deleted_at IS NULL
34+
where lesson_id = :lessonId
35+
limit :limit
36+
) t
2537
""", nativeQuery = true)
2638
Integer count(@Param("lessonId") Long lessonId, @Param("limit") int limit);
2739

2840
@Query(value = """
29-
select count(*) from (select comment_id from comments where lesson_id = :lessonId and parent_comment_id = :parentCommentId limit :limit) t
41+
select count(*) from (
42+
select comment_id
43+
from comments c
44+
join users u on c.user_id = u.id and u.deleted_at IS NULL
45+
where lesson_id = :lessonId and parent_comment_id = :parentCommentId
46+
limit :limit
47+
) t
3048
""", nativeQuery = true)
31-
Long countBy(@Param("lessonId") Long lessonId, @Param("parentCommentId") Long parentCommentId,
32-
@Param("limit") int limit);
49+
Long countBy(@Param("lessonId") Long lessonId, @Param("parentCommentId") Long parentCommentId, @Param("limit") int limit);
3350

3451
Optional<Comment> findByCommentIdAndUserId(Long commentId, Long userId);
3552

src/main/java/com/threestar/trainus/domain/review/repository/ReviewRepository.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@ public interface ReviewRepository extends JpaRepository<Review, Long> {
1515
boolean existsByReviewer_IdAndLessonId(Long reviewerId, Long lessonId);
1616

1717
@Query(value = """
18-
select count(*) from (select review_id from reviews where reviewee_id = :userId limit :limit ) t
18+
SELECT COUNT(*) FROM (
19+
SELECT r.review_id
20+
FROM reviews r
21+
JOIN users u1 ON r.reviewee_id = u1.id AND u1.deleted_at IS NULL
22+
JOIN users u2 ON r.reviewer_id = u2.id AND u2.deleted_at IS NULL
23+
WHERE r.reviewee_id = :userId
24+
LIMIT :limit
25+
) t
1926
""", nativeQuery = true)
2027
Integer count(
2128
@Param("userId") Long userId,

src/main/java/com/threestar/trainus/domain/user/controller/UserController.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.springframework.http.HttpStatus;
44
import org.springframework.http.ResponseEntity;
5+
import org.springframework.web.bind.annotation.DeleteMapping;
56
import org.springframework.web.bind.annotation.PatchMapping;
67
import org.springframework.web.bind.annotation.PostMapping;
78
import org.springframework.web.bind.annotation.RequestBody;
@@ -100,4 +101,13 @@ public ResponseEntity<BaseResponse<Void>> updatePassword(
100101
userService.updatePassword(request, loginUserId);
101102
return BaseResponse.ok("비밀번호 변경이 완료되었습니다.", null, HttpStatus.OK);
102103
}
104+
105+
@DeleteMapping("/withdraw")
106+
@Operation(summary = "회원탈퇴 api")
107+
public ResponseEntity<BaseResponse<Void>> withdraw(
108+
@LoginUser Long loginUserId
109+
) {
110+
userService.withdraw(loginUserId);
111+
return BaseResponse.okOnlyStatus(HttpStatus.NO_CONTENT);
112+
}
103113
}

src/main/java/com/threestar/trainus/domain/user/entity/User.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.util.ArrayList;
55
import java.util.List;
66

7+
import org.hibernate.annotations.SQLRestriction;
8+
79
import com.threestar.trainus.domain.coupon.user.entity.UserCoupon;
810
import com.threestar.trainus.domain.metadata.entity.ProfileMetadata;
911
import com.threestar.trainus.domain.profile.entity.Profile;
@@ -32,6 +34,7 @@
3234
@NoArgsConstructor(access = AccessLevel.PROTECTED)
3335
@Builder
3436
@AllArgsConstructor
37+
@SQLRestriction("deleted_at IS NULL") //삭제되지 않은 데이터만 기본적으로 조회하는 필터
3538
public class User extends BaseDateEntity {
3639

3740
@Id
@@ -65,4 +68,8 @@ public class User extends BaseDateEntity {
6568
public void updatePassword(String newPassword) {
6669
this.password = newPassword;
6770
}
71+
72+
public void withdraw() {
73+
this.deletedAt = LocalDateTime.now();
74+
}
6875
}

src/main/java/com/threestar/trainus/domain/user/repository/UserRepository.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,4 @@ public interface UserRepository extends JpaRepository<User, Long> {
1313
boolean existsByNickname(String nickname);
1414

1515
Optional<User> findByEmail(String email);
16-
17-
// 닉네임으로 사용자 찾기
18-
Optional<User> findByNickname(String nickname);
19-
20-
// 삭제되지 않은 사용자만 조회
21-
Optional<User> findByIdAndDeletedAtIsNull(Long id);
2216
}

src/main/java/com/threestar/trainus/domain/user/service/UserService.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,11 @@ public void updatePassword(PasswordUpdateDto request, Long userId) {
138138

139139
userRepository.save(user);
140140
}
141+
142+
@Transactional
143+
public void withdraw(Long userId) {
144+
User user = getUserById(userId);
145+
146+
user.withdraw();
147+
}
141148
}

0 commit comments

Comments
 (0)