Skip to content

Commit

Permalink
feat: 회원 탈퇴 API 추가 #117
Browse files Browse the repository at this point in the history
  • Loading branch information
chaewss committed Apr 21, 2024
1 parent 97983b5 commit cb161fd
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
1 change: 1 addition & 0 deletions sql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/cvsgo/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ public SuccessResponse<Void> updateUser(@LoginUser User user,
return SuccessResponse.create();
}

@DeleteMapping("/user")
public SuccessResponse<Void> deleteUser(@LoginUser User user) {
userService.deleteUser(user);
return SuccessResponse.create();
}

@PostMapping("/users/{userId}/followers")
@ResponseStatus(HttpStatus.CREATED)
public SuccessResponse<Void> createUserFollow(@LoginUser User user, @PathVariable Long userId) {
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/cvsgo/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<UserTag> userTags = new ArrayList<>();

@Builder
Expand Down
Original file line number Diff line number Diff line change
@@ -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<RefreshToken, Long> {

Optional<RefreshToken> findByToken(String token);

void deleteAllByUser(User user);
}
14 changes: 14 additions & 0 deletions src/main/java/com/cvsgo/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,6 +47,8 @@ public class UserService {

private final UserFollowRepository userFollowRepository;

private final RefreshTokenRepository refreshTokenRepository;

private final ReviewRepository reviewRepository;

private final PasswordEncoder passwordEncoder;
Expand Down Expand Up @@ -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);
}

/**
* 회원 팔로우를 생성한다.
*
Expand Down

0 comments on commit cb161fd

Please sign in to comment.