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

[CHORE] 실서버 배포 #183

Merged
merged 8 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/main/java/com/smeme/server/model/DeletedDiary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.smeme.server.model;

import com.smeme.server.model.topic.Topic;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

import static jakarta.persistence.FetchType.LAZY;
import static jakarta.persistence.GenerationType.IDENTITY;

@Entity
@Getter
@NoArgsConstructor
public class DeletedDiary {
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "diary_id")
private Long id;

@Column(columnDefinition = "TEXT")
private String content;

@Column(nullable = false)
@Enumerated(value = EnumType.STRING)
private LangType targetLang;

private boolean isPublic;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "topic_id")
private Topic topic;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "member_id")
private Member member;

private LocalDateTime createdAt;

private LocalDateTime updatedAt;

public DeletedDiary(Diary diary) {
this.content = diary.getContent();
this.targetLang = diary.getTargetLang();
this.isPublic = diary.isPublic();
this.topic = diary.getTopic();
this.member = diary.getMember();
this.createdAt = diary.getCreatedAt();
this.updatedAt = LocalDateTime.now();
}
}
14 changes: 1 addition & 13 deletions src/main/java/com/smeme/server/model/Diary.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ public class Diary extends BaseTimeEntity {

private boolean isPublic;

private boolean isDeleted;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "topic_id")
private Topic topic;
Expand All @@ -53,7 +51,6 @@ public Diary(String content, Topic topic, Member member) {
this.content = content;
this.targetLang = member.getTargetLang();
this.isPublic = true;
this.isDeleted = false;
this.topic = topic;
setMember(member);
this.createdAt = LocalDateTime.now();
Expand All @@ -63,22 +60,13 @@ public void updateContent(String content) {
this.content = content;
}

public void delete() {
this.isDeleted = true;
deleteFromMember();
this.member.updateDiaryCombo();
}

public void deleteFromMember() {
if (nonNull(this.member)) {
this.member.getDiaries().remove(this);
this.member.updateDiaryCombo();
}
}

public boolean isValid() {
return !this.isDeleted;
}

public boolean isCreatedAt(LocalDateTime createdAt) {
return getStartOfDay(this.createdAt).isEqual(getStartOfDay(createdAt));
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/smeme/server/model/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ public void updateGoal(GoalType goal) {

public boolean wroteDiaryToday() {
return this.diaries.stream()
.filter(Diary::isValid)
.anyMatch(diary -> diary.isCreatedAt(now()));
}

Expand All @@ -105,7 +104,7 @@ public void updateDiaryCombo(boolean isCombo) {

public void updateDiaryCombo() {
List<Diary> diaries = this.diaries.stream()
.filter(diary -> diary.isValid() && !diary.isCreatedAt(now()))
.filter(diary -> !diary.isCreatedAt(now()))
.sorted((a, b) -> b.createdAt.compareTo(a.createdAt)).toList();

int count = wroteDiaryToday() ? 1 : 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ public int countByMember(Member member) {
.select(correction.count())
.from(correction)
.leftJoin(correction.diary, diary)
.where(
diary.member.eq(member),
diary.isDeleted.eq(false)
)
.where(diary.member.eq(member))
.fetchFirst());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.smeme.server.repository.diary;

import com.smeme.server.model.DeletedDiary;
import org.springframework.data.jpa.repository.JpaRepository;

import java.time.LocalDateTime;

public interface DeletedDiaryRepository extends JpaRepository<DeletedDiary, Long> {
void deleteByUpdatedAtBefore(LocalDateTime expiredAt);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package com.smeme.server.repository.diary;

import java.util.List;
import java.util.Optional;

import com.smeme.server.model.Diary;

public interface DiaryCustomRepository {
List<Diary> findByExpiredDate();

Optional<Diary> findByIdFetchJoinCorrections(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@

import static com.smeme.server.model.QCorrection.correction;
import static com.smeme.server.model.QDiary.*;
import static com.smeme.server.util.Util.getStartOfDay;
import static java.lang.Integer.parseInt;

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

import com.smeme.server.config.ValueConfig;
import org.springframework.stereotype.Repository;

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

private final JPAQueryFactory queryFactory;
private final ValueConfig valueConfig;

@Override
public List<Diary> findByExpiredDate() {
int expiredDay = parseInt(valueConfig.getDURATION_EXPIRED());
LocalDateTime expiredDate = getStartOfDay(LocalDateTime.now()).minusDays(expiredDay - 1);

return queryFactory
.selectFrom(diary)
.where(
diary.isDeleted.eq(true),
diary.createdAt.before(expiredDate)
)
.fetch();
}

@Override
public Optional<Diary> findByIdFetchJoinCorrections(Long id) {
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/smeme/server/scheduler/DiaryScheduler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.smeme.server.scheduler;

import com.smeme.server.service.DiaryService;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@EnableScheduling
@RequiredArgsConstructor
public class DiaryScheduler {
private final DiaryService diaryService;

@Scheduled(cron = "0 0 0 * * *")
public void deleteExpiredDiaries() {
diaryService.deleteExpiredDiary();
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,24 @@
package com.smeme.server.controller;

import java.time.LocalDateTime;
package com.smeme.server.scheduler;

import com.smeme.server.config.ValueConfig;
import com.smeme.server.service.MessageService;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RestController;

import com.smeme.server.service.DiaryService;
import com.smeme.server.service.MessageService;
import org.springframework.stereotype.Component;

import lombok.RequiredArgsConstructor;
import java.time.LocalDateTime;

@RestController
@Component
@EnableScheduling
@RequiredArgsConstructor
public class ScheduleController {

public class MessageScheduler {
private final MessageService messageService;
private final DiaryService diaryService;
private final ValueConfig valueConfig;

// @Scheduled(cron = "0 0/30 * * * *")
@Scheduled(cron = "${fcm.cron_expression}")
public void pushMessage() throws InterruptedException {
Thread.sleep(1000);
messageService.pushMessageForTrainingTime(LocalDateTime.now(), valueConfig.getMESSAGE_TITLE(), valueConfig.getMESSAGE_BODY());
}

@Scheduled(cron = "0 0 0 * * *")
public void deleteExpiredDiaries() {
diaryService.deleteByExpiredDate();
}
}
51 changes: 28 additions & 23 deletions src/main/java/com/smeme/server/service/DiaryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
import static java.time.LocalDateTime.now;
import static java.util.Objects.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

import com.smeme.server.config.ValueConfig;
import com.smeme.server.model.DeletedDiary;
import com.smeme.server.repository.correction.CorrectionRepository;
import com.smeme.server.repository.diary.DeletedDiaryRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -34,6 +37,7 @@ public class DiaryService {

private final DiaryRepository diaryRepository;
private final CorrectionRepository correctionRepository;
private final DeletedDiaryRepository deletedDiaryRepository;

private final BadgeService badgeService;
private final TopicService topicService;
Expand Down Expand Up @@ -72,24 +76,40 @@ public void update(Long id, DiaryRequestDTO request) {
@Transactional
public void delete(Long id) {
Diary diary = get(id);
diary.delete();
copyToDeletedDiary(diary);
delete(diary);
}

private void copyToDeletedDiary(Diary diary) {
deletedDiaryRepository.save(new DeletedDiary(diary));
}

private void delete(Diary diary) {
diary.deleteFromMember();
correctionRepository.deleteAll(diary.getCorrections());
diaryRepository.deleteById(diary.getId());
}

public DiariesResponseDTO getDiaries(Long memberId, String startDate, String endDate) {
Member member = memberService.get(memberId);
List<Diary> diaries = member.getDiaries().stream()
.filter(diary -> diary.isValid() && diary.isBetween(stringToDate(startDate), stringToDate(endDate)))
.filter(diary -> diary.isBetween(stringToDate(startDate), stringToDate(endDate)))
.toList();
boolean hasRemind = member.getDiaries().stream()
.filter(Diary::isValid)
.anyMatch(diary -> diary.isCreatedAt(now().minusDays(parseInt(valueConfig.getDURATION_REMIND()))));

return DiariesResponseDTO.of(diaries, hasRemind);
}

@Transactional
public void deleteByExpiredDate() {
diaryRepository.findByExpiredDate().forEach(this::delete);
public void deleteExpiredDiary() {
LocalDateTime expiryDate = getExpiryDate();
deletedDiaryRepository.deleteByUpdatedAtBefore(expiryDate);
}

private LocalDateTime getExpiryDate() {
int expiredDay = parseInt(valueConfig.getDURATION_EXPIRED()) - 1;
return LocalDate.now().minusDays(expiredDay).atStartOfDay();
}

@Transactional
Expand All @@ -102,21 +122,13 @@ public List<Diary> getAllByMemberId(Long memberId) {
}

protected Diary get(Long id) {
Diary diary = diaryRepository.findById(id)
return diaryRepository.findById(id)
.orElseThrow(() -> new EntityNotFoundException(INVALID_DIARY.getMessage()));
if (diary.isDeleted()) {
throw new NoSuchElementException(DELETED_DIARY.getMessage());
}
return diary;
}

protected Diary getFetchJoinCorrections(Long id) {
Diary diary = diaryRepository.findByIdFetchJoinCorrections(id)
return diaryRepository.findByIdFetchJoinCorrections(id)
.orElseThrow(() -> new EntityNotFoundException(INVALID_DIARY.getMessage()));
if (diary.isDeleted()) {
throw new EntityNotFoundException(INVALID_DIARY.getMessage());
}
return diary;
}

private List<Badge> obtainBadges(Member member, LocalDateTime createdAt) {
Expand All @@ -136,7 +148,7 @@ private List<Badge> obtainBadges(Member member, LocalDateTime createdAt) {
}

private Badge obtainCountingBadge(Member member) {
int count = member.getDiaries().stream().filter(Diary::isValid).toList().size();
int count = member.getDiaries().stream().toList().size();

return switch (count) {
case 50 -> badgeService.get(5L);
Expand All @@ -149,7 +161,6 @@ private Badge obtainCountingBadge(Member member) {

private Badge obtainComboBadge(Member member, LocalDateTime createdAt) {
boolean isCombo = member.getDiaries().stream()
.filter(Diary::isValid)
.anyMatch(diary -> diary.isCreatedAt(createdAt.minusDays(1)));

member.updateDiaryCombo(isCombo);
Expand All @@ -162,10 +173,4 @@ private Badge obtainComboBadge(Member member, LocalDateTime createdAt) {
default -> null;
};
}

private void delete(Diary diary) {
diary.deleteFromMember();
correctionRepository.deleteAll(diary.getCorrections());
diaryRepository.deleteById(diary.getId());
}
}
1 change: 1 addition & 0 deletions src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ fcm:
google_api: https://www.googleapis.com/auth/cloud-platform
smeem_title: "오늘의 영어 훈련, 딱 5분 걸려요!"
smeem_body: "지금 눌러서 일기 쓰기 ✍️"
cron_expression: "-"

jwt:
secret: ${JWT.SECRET}
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
spring:
config:
import: application-secret.yml
activate:
on-profile: prod
datasource:
Expand All @@ -26,6 +27,7 @@ fcm:
google_api: https://www.googleapis.com/auth/cloud-platform
smeem_title: "오늘의 영어 훈련, 딱 5분 걸려요!"
smeem_body: "지금 눌러서 일기 쓰기 ✍️"
cron_expression: "0 0/30 * * * *"

jwt:
secret: ${JWT.SECRET}
Expand Down
Loading