Skip to content

Commit

Permalink
Merge pull request #109 from Team-Umbba/feat/#105-soft_delete_strategy
Browse files Browse the repository at this point in the history
[FEAT] Soft Delete 기능 구현 + 과거 문답 리스트 조회 로직 수정
  • Loading branch information
ddongseop authored Oct 3, 2023
2 parents 19aaee9 + 05afc1d commit 4544b9e
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 6 deletions.
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;
}

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")
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;

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

0 comments on commit 4544b9e

Please sign in to comment.