Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] Soft Delete 기능 구현 + 과거 문답 리스트 조회 로직 수정 #109

Merged
merged 8 commits into from
Oct 3, 2023
Binary file removed AWSCLIV2.pkg
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,14 @@ public List<QnAListResponseDto> getQnaList(Long userId, Long sectionId) {
Parentchild parentchild = getParentchildByUser(myUser);
List<QnA> qnaList = getQnAListByParentchild(parentchild);

QnA todayQnA = getTodayQnAByParentchild(parentchild);
int doneIndex = parentchild.getCount() - 1;
if (todayQnA.isChildAnswer() && todayQnA.isParentAnswer()) {
doneIndex += 1;
}
Comment on lines +118 to +121
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오오 이거 변수로 뺀 거 넘 좋다


return qnaList.stream()
.limit(parentchild.getCount() - 1) // index까지만 요소를 처리
.limit(doneIndex) // 현재 답변 완료된 index까지 보이도록
.filter(qna -> Objects.equals(qna.getQuestion().getSection().getSectionId(), sectionId))
.map(qna -> {
return QnAListResponseDto.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,27 @@
import sopt.org.umbba.api.service.user.social.kakao.KakaoLoginService;
import sopt.org.umbba.common.exception.ErrorType;
import sopt.org.umbba.common.exception.model.CustomException;
import sopt.org.umbba.domain.domain.parentchild.Parentchild;
import sopt.org.umbba.domain.domain.parentchild.repository.ParentchildRepository;
import sopt.org.umbba.domain.domain.qna.repository.QnARepository;
import sopt.org.umbba.domain.domain.user.SocialPlatform;
import sopt.org.umbba.domain.domain.user.User;
import sopt.org.umbba.domain.domain.user.repository.UserRepository;

import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class AuthService {
public class AuthService {

private final JwtProvider jwtProvider;
private final UserRepository userRepository;
private final ParentchildRepository parentchildRepository;
private final QnARepository qnARepository;

private final AppleLoginService appleLoginService;
private final KakaoLoginService kakaoLoginService;
Expand Down Expand Up @@ -98,9 +104,21 @@ public void logout(Long userId) {
public void signout(Long userId) {
User user = getUserById(userId);
user.updateRefreshToken(null);
jwtProvider.deleteRefreshToken(userId); // 일치하는 ID가 없는 경우에는 아무 동작도 수행하지 않음 (CrudRepository 기본 동작)
user.updateFcmToken(null);
user.deleteSocialInfo();
jwtProvider.deleteRefreshToken(userId); // 일치하는 ID가 없는 경우에는 아무 동작도 수행하지 않음 (CrudRepository 기본 동작)

Parentchild parentChild = user.getParentChild();
List<User> findUsers = userRepository.findUserByParentChild(parentChild);

boolean allUsersDeleted = findUsers.stream()
.allMatch(u -> u.getSocialPlatform().equals(SocialPlatform.WITHDRAW));
if (allUsersDeleted) {
log.info("삭제된 부모자식: {} X {}", findUsers.get(0).getUsername(), findUsers.get(1).getUsername());
parentChild.getQnaList().forEach(qna -> qnARepository.deleteById(qna.getId()));
parentchildRepository.deleteById(parentChild.getId());
findUsers.forEach(u -> userRepository.deleteById(u.getId()));
}
}

private User getUserById(Long userId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import sopt.org.umbba.common.exception.ErrorType;
import sopt.org.umbba.common.exception.model.CustomException;
import sopt.org.umbba.domain.domain.common.AuditingTimeEntity;
Expand All @@ -19,6 +21,8 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
@SQLDelete(sql = "UPDATE parentchild SET deleted=true WHERE parentchild_id=?")
@Where(clause = "deleted=false")
Comment on lines +24 to +25
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오오 soft delete 이렇게 쓰는거구나! 배우고 갑니다 👍

public class Parentchild extends AuditingTimeEntity {

@Id
Expand Down Expand Up @@ -81,6 +85,8 @@ public void changeParentOnboardingAnswerList(List<OnboardingAnswer> onboardingAn
@Column(nullable = false)
private LocalTime pushTime; // default: 오후 11시(클라이언트)

private boolean deleted = Boolean.FALSE;

Comment on lines +88 to +89
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

테스트 결과 확인했습니당

public void initQnA() {
qnaList = new ArrayList<>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public interface ParentchildRepository extends Repository<Parentchild, Long> {

List<Parentchild> findAll();


// UPDATE

// DELETE
void deleteById(Long id);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package sopt.org.umbba.domain.domain.qna;

import lombok.*;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import sopt.org.umbba.domain.domain.common.AuditingTimeEntity;

import javax.persistence.*;
Expand All @@ -10,6 +12,8 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
@SQLDelete(sql = "UPDATE qna SET deleted=true WHERE qna_id=?")
@Where(clause = "deleted=false")
public class QnA extends AuditingTimeEntity {

@Id
Expand All @@ -31,6 +35,7 @@ public class QnA extends AuditingTimeEntity {
@Column(nullable = false)
private boolean isChildAnswer;

private boolean deleted = Boolean.FALSE;

public boolean isParentAnswer() {
return isParentAnswer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface QnARepository extends Repository<QnA, Long> {
void save(QnA qnA);

Optional<QnA> findQnAById(Long id);

void deleteById(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import sopt.org.umbba.domain.domain.common.AuditingTimeEntity;
import sopt.org.umbba.domain.domain.parentchild.Parentchild;

Expand All @@ -15,6 +17,8 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
@SQLDelete(sql = "UPDATE user SET deleted=true WHERE user_id=?")
@Where(clause = "deleted=false")
public class User extends AuditingTimeEntity {

@Id
Expand Down Expand Up @@ -82,7 +86,8 @@ public void updateFcmToken(String fcmToken) {
private String socialAccessToken;

// private String socialRefreshToken;
//

private boolean deleted = Boolean.FALSE;

// 로그인 새롭게 할 때마다 해당 필드들 업데이트
public void updateSocialInfo(String socialNickname, String socialProfileImage, String socialAccessToken/*, String socialRefreshToken*/) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ public interface UserRepository extends Repository<User, Long> {
Optional<User> findBySocialPlatformAndSocialId(SocialPlatform socialPlatform, String socialId);
Optional<User> findByFcmToken(String fcmToken);

// DELETE
void deleteById(Long id);

/*@Query(value = "select user " +
"from User user " +
"where user.parentChild.id = :parentchild_id")*/
// TODO UserRepository 에서 or ParentchildRepository에서?
List<User> findUserByParentChild(Parentchild parentchild);


Expand Down
Loading