Skip to content

Commit

Permalink
[REFACT] Diary 리팩토링 정리
Browse files Browse the repository at this point in the history
  • Loading branch information
thguss committed Sep 19, 2023
1 parent 30467f9 commit 9b5ac79
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 62 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/smeme/server/controller/ErrorHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,9 @@ public ResponseEntity<ApiResponse> invalidBearerTokenException(InvalidBearerToke
public ResponseEntity<ApiResponse> ioException(IOException ex) {
return ResponseEntity.status(INTERNAL_SERVER_ERROR).body(fail(ex.getMessage()));
}

@ExceptionHandler(IllegalStateException.class)
public ResponseEntity<ApiResponse> illegalStateException(IllegalStateException ex) {
return ResponseEntity.status(BAD_REQUEST).body(fail(ex.getMessage()));
}
}
5 changes: 2 additions & 3 deletions src/main/java/com/smeme/server/model/Diary.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class Diary extends BaseTimeEntity {
@JoinColumn(name = "member_id")
private Member member;

@OneToMany(mappedBy = "diary")
@OneToMany(mappedBy = "diary", orphanRemoval = true)
private final List<Correction> corrections = new ArrayList<>();

public Diary(String content, Topic topic, Member member) {
Expand All @@ -61,9 +61,8 @@ public void updateContent(String content) {
this.updatedAt = LocalDateTime.now();
}

public void deleteDiary() {
public void delete() {
this.isDeleted = true;
this.updatedAt = LocalDateTime.now();
this.member.getDiaries().remove(this);
}

Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/smeme/server/model/Member.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.smeme.server.model;

import static com.smeme.server.util.Util.getStartOfDay;
import static jakarta.persistence.GenerationType.*;

import jakarta.persistence.*;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -49,6 +51,8 @@ public class Member extends BaseTimeEntity {
@Enumerated(value = EnumType.STRING)
private LangType targetLang;

private int diaryComboCount; //TODO: 논의

@OneToMany(mappedBy = "member")
private final List<TrainingTime> trainingTimes = new ArrayList<>();

Expand All @@ -65,6 +69,7 @@ public Member(SocialType social, String socialId, LangType targetLang, String fc
this.targetLang = targetLang;
this.fcmToken = fcmToken;
this.goal = null;
this.diaryComboCount = 0; //TODO: 논의
}

public void updateRefreshToken(String refreshToken) {
Expand All @@ -86,4 +91,15 @@ public void updateHasAlarm(boolean hasAlarm) {
public void updateGoal(GoalType goal) {
this.goal = goal;
}

public boolean isExistTodayDiary() {
LocalDateTime now = getStartOfDay(LocalDateTime.now());
return diaries.stream().anyMatch(diary -> getStartOfDay(diary.createdAt).equals(now));
}

//TODO: 논의
public void updateDiaryCombo(boolean isCombo) {
this.diaryComboCount = isCombo ? this.diaryComboCount + 1 : 1;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import com.smeme.server.model.Diary;
import com.smeme.server.model.Member;
import org.springframework.data.jpa.repository.Modifying;

public interface DiaryCustomRepository {
boolean existTodayDiary(Member member);
Expand All @@ -16,4 +17,7 @@ public interface DiaryCustomRepository {
List<Diary> findDiariesDeleted30Past(LocalDateTime past);

boolean existDiaryInDate(Member member, LocalDateTime createdDate);

@Modifying(clearAutomatically = true)
void deleteByMemberAndExpired(Member member); // 확인하고 member 빼기, 이름 바꾸기
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.time.LocalDateTime;
import java.util.List;

import com.smeme.server.util.Util;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.querydsl.jpa.impl.JPAQueryFactory;
Expand All @@ -17,6 +19,9 @@
@RequiredArgsConstructor
public class DiaryRepositoryImpl implements DiaryCustomRepository {

@Value("${expired.duration}")
private String expiredDuration; // ValueConfig 파일로 옮길 것

private final JPAQueryFactory queryFactory;

@Override
Expand Down Expand Up @@ -91,6 +96,20 @@ public boolean existDiaryInDate(Member member, LocalDateTime createdDate) {
.fetchFirst() != null;
}

@Override
public void deleteByMemberAndExpired(Member member) { // 30일 "지난"
int expiredDay = Integer.parseInt(expiredDuration);
LocalDateTime expiredDate = Util.getStartOfDay(LocalDateTime.now()).minusDays(expiredDay - 1);

queryFactory
.delete(diary)
.where(
diary.member.eq(member),
diary.createdAt.before(expiredDate)
)
.execute();
}

private LocalDateTime get12midnight(LocalDateTime now) {
return LocalDateTime.of(now.getYear(), now.getMonth(), now.getDayOfMonth(), 0, 0, 0);
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/smeme/server/service/CorrectionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ public void updateCorrection(Long correctionId, CorrectionRequestDTO request) {
correction.updateCorrection(request.content());
}

@Transactional
public void deleteByDiary(Diary diary) {

}

private Diary getDiary(Long diaryId) {
Diary diary = diaryRepository.findById(diaryId)
.orElseThrow(() -> new EntityNotFoundException(INVALID_DIARY.getMessage()));
Expand Down
Loading

0 comments on commit 9b5ac79

Please sign in to comment.