Skip to content

Commit

Permalink
Merge pull request #30 from Jhsysng/feat/reminder
Browse files Browse the repository at this point in the history
feat/reminder
  • Loading branch information
Jhsysng authored Mar 21, 2024
2 parents 558645a + e9d103c commit c13d20d
Show file tree
Hide file tree
Showing 13 changed files with 380 additions and 3 deletions.
2 changes: 1 addition & 1 deletion 2024_BEOTKKOTTHON_TEAM_24_BE_ENV
1 change: 0 additions & 1 deletion src/main/java/goorm/reinput/folder/domain/Folder.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public class Folder extends BaseTimeEntity {
private FolderColor folderColor;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "userId")
private User user;


Expand Down
4 changes: 4 additions & 0 deletions src/main/java/goorm/reinput/insight/domain/Insight.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import goorm.reinput.folder.domain.Folder;
import goorm.reinput.global.domain.BaseTimeEntity;
import goorm.reinput.reminder.domain.Reminder;
import jakarta.persistence.*;
import lombok.Builder;
import lombok.Getter;
Expand Down Expand Up @@ -30,6 +31,9 @@ public class Insight extends BaseTimeEntity {
@JoinColumn(name = "folderId")
private Folder folder;

@OneToOne(mappedBy = "insight")
private Reminder reminder;

@OneToMany(mappedBy = "insight", cascade = CascadeType.ALL, orphanRemoval = true)
private List<HashTag> hashTagList;

Expand Down
23 changes: 23 additions & 0 deletions src/main/java/goorm/reinput/reminder/domain/Question.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package goorm.reinput.reminder.domain;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@NoArgsConstructor(access = lombok.AccessLevel.PROTECTED)
public class Question {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String question;

public Question(String question) {
this.question = question;
}
}
6 changes: 6 additions & 0 deletions src/main/java/goorm/reinput/reminder/domain/Reminder.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public class Reminder extends BaseTimeEntity {
@OneToOne(fetch = FetchType.LAZY)
private Insight insight;

@OneToOne(mappedBy = "reminder")
private ReminderDate reminderDate;

@OneToOne(mappedBy = "reminder")
private ReminderQuestion reminderQuestion;

@Builder
public Reminder(Boolean isEnable, LocalDateTime lastRemindedAt, Insight insight) {
this.isEnable = isEnable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class ReminderDate {
@Column(name = "remind_day")
private List<Integer> remindDays;


@Builder
public ReminderDate(Reminder reminder, RemindType remindType, List<Integer> remindDays){
this.reminder = reminder;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package goorm.reinput.reminder.domain.dto;

import goorm.reinput.insight.domain.HashTag;
import lombok.*;

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

@Data
@RequiredArgsConstructor
public class ReminderQuestionQueryDto {
private String reminderQuestion;
private Long insightId;
private Long reminderId;
private String insightTitle;
private String insightMainImage;
private LocalDateTime lastRemindedAt;
private List<String> insightTagList;

@Builder
public ReminderQuestionQueryDto(String reminderQuestion, Long insightId, Long reminderId, String insightTitle, String insightMainImage, List<String> insightTagList) {
this.reminderQuestion = reminderQuestion;
this.insightId = insightId;
this.reminderId = reminderId;
this.insightTitle = insightTitle;
this.insightMainImage = insightMainImage;
this.insightTagList = insightTagList;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package goorm.reinput.reminder.domain.dto;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@Getter
@NoArgsConstructor
public class ReminderQuestionResponseDto {
private boolean todayClear;
private List<ReminderQuestionQueryDto> reminderQuestionList;

@Builder
public ReminderQuestionResponseDto(boolean todayClear, List<ReminderQuestionQueryDto> reminderQuestionList) {
this.todayClear = todayClear;
this.reminderQuestionList = reminderQuestionList;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package goorm.reinput.reminder.repository;

import goorm.reinput.reminder.domain.Question;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

@Repository
public interface QuestionRepository extends JpaRepository<Question, Long> {
@Query(value = "SELECT * FROM question ORDER BY RAND() LIMIT 1", nativeQuery = true)
Question findRandomQuestion();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package goorm.reinput.reminder.repository.impl;

import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import goorm.reinput.insight.domain.HashTag;
import goorm.reinput.reminder.domain.RemindType;
import goorm.reinput.reminder.domain.Reminder;
import goorm.reinput.reminder.domain.ReminderQuestion;
import goorm.reinput.reminder.domain.dto.ReminderQuestionQueryDto;
import jakarta.persistence.EntityManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import static goorm.reinput.insight.domain.QHashTag.hashTag;
import static goorm.reinput.insight.domain.QInsight.insight;
import static goorm.reinput.reminder.domain.QReminder.reminder;
import static goorm.reinput.reminder.domain.QReminderDate.reminderDate;


@Repository
@Slf4j
public class CustomReminderRepository {
private final EntityManager em;
private final JPAQueryFactory queryFactory;

public CustomReminderRepository(EntityManager em) {
this.em = em;
this.queryFactory = new JPAQueryFactory(this.em);
}
//๊ฐ€์žฅ ์ฝ์€ ์˜ค๋ž˜๋œ ๋ฆฌ๋งˆ์ธ๋” 5๊ฐœ๋งŒ ์กฐํšŒ, insight์™€ reminder question join
public List<ReminderQuestionQueryDto> findOldestReminderDto(Long userId) {
// ํ•ด๋‹น user์˜ reminder ์ค‘ ๊ฐ€์žฅ ์˜ค๋ž˜๋œ 5๊ฐœ๋ฅผ ์กฐํšŒ
List<ReminderQuestionQueryDto> results = queryFactory
.select(Projections.fields(ReminderQuestionQueryDto.class,
reminder.reminderQuestion.reminderQuestion.as("reminderQuestion"),
insight.insightId.as("insightId"),
reminder.reminderId.as("reminderId"),
insight.insightTitle.as("insightTitle"),
insight.insightMainImage.as("insightMainImage"),
reminder.lastRemindedAt.as("lastRemindedAt")
))
.from(reminder)
.join(reminder.insight, insight)
.where(insight.folder.user.userId.eq(userId))
.orderBy(reminder.lastRemindedAt.asc())
.limit(5)
.fetch();
results.forEach(dto -> {
List<String> tags = queryFactory
.select(hashTag.hashTagName)
.from(hashTag)
.where(hashTag.insight.insightId.eq(dto.getInsightId()))
.fetch();
dto.setInsightTagList(tags);
});

return results;
}

public List<Reminder> findOldestReminders(Long userId) {
return queryFactory
.selectFrom(reminder)
.orderBy(reminder.lastRemindedAt.asc())
.where(reminder.isEnable.isTrue().and(insight.folder.user.userId.eq(userId)))
.join(reminder.insight, insight)
.limit(5)
.fetch();
}

// ๋ฆฌ๋งˆ์ธ๋“œํ•  ์ธ์‚ฌ์ดํŠธ ์กฐํšŒ
public List<Reminder> findRemindersToNotify(Long userId) {

// ์˜ค๋Š˜ ๋‚ ์งœ์™€ ์š”์ผ
LocalDate today = LocalDate.now();
DayOfWeek todayDayOfWeek = today.getDayOfWeek();
int todayMonthDay = today.getDayOfMonth();

return queryFactory
.selectFrom(reminder)
.join(reminder.reminderDate, reminderDate)
.join(reminder.insight, insight)
.where(reminder.isEnable.isTrue().and(insight.folder.user.userId.eq(userId))
.and(reminderDate.remindType.eq(RemindType.DEFAULT)
.and(reminder.lastRemindedAt.after(LocalDate.now().minusDays(1).atStartOfDay())
.or(reminder.lastRemindedAt.after(LocalDate.now().minusWeeks(1).atStartOfDay()))
.or(reminder.lastRemindedAt.after(LocalDate.now().minusMonths(1).atStartOfDay()))))
.or(reminderDate.remindType.eq(RemindType.WEEK)
.and(reminderDate.remindDays.contains(todayDayOfWeek.getValue())))
.or(reminderDate.remindType.eq(RemindType.MONTH)
.and(reminderDate.remindDays.contains(todayMonthDay))))
.fetch();

}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,51 @@
package goorm.reinput.reminder.service;

import goorm.reinput.reminder.domain.Reminder;
import goorm.reinput.reminder.domain.ReminderQuestion;
import goorm.reinput.reminder.domain.dto.ReminderQuestionQueryDto;
import goorm.reinput.reminder.domain.dto.ReminderQuestionResponseDto;
import goorm.reinput.reminder.repository.QuestionRepository;
import goorm.reinput.reminder.repository.ReminderRepository;
import goorm.reinput.reminder.repository.impl.CustomReminderRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Service
public class ReminderService {

private final ReminderRepository reminderRepository;
private final CustomReminderRepository customReminderRepository;
private final QuestionRepository questionRepository;

private void makeReminderQuestionList(Long userId) {
log.info("makeReminderQuestionList start");
List<Reminder> reminders = customReminderRepository.findOldestReminders(userId);

//์˜ค๋ž˜๋œ ๋ฆฌ๋งˆ์ธ๋”์ค‘ reminderQuestion์ด ์—†๋Š” ๊ฒฝ์šฐ ์งˆ๋ฌธ์„ ์ƒ์„ฑํ•˜๊ณ  ์žˆ์œผ๋ฉด ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ์˜จ๋‹ค.
for (Reminder reminder : reminders) {
if (reminder.getReminderQuestion() == null) {
ReminderQuestion question = ReminderQuestion.builder()
.reminder(reminder)
.reminderQuestion(questionRepository.findRandomQuestion().getQuestion())
.build();
}
}

}
//todo: get Older reminer
// private ReminderQuestionResponseDto getOlderReminder(Long userId){
//
// makeReminderQuestionList();
//
// List<ReminderQuestionQueryDto> reminderQuestionQueryDtos = customReminderRepository.findOldestReminderDto(userId);
//
//
//
// }

}
29 changes: 29 additions & 0 deletions src/main/java/goorm/reinput/reminder/util/QuestionInitializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package goorm.reinput.reminder.util;

import goorm.reinput.reminder.domain.Question;
import goorm.reinput.reminder.repository.QuestionRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Component
@RequiredArgsConstructor
public class QuestionInitializer implements CommandLineRunner {
private final QuestionRepository questionRepository;

@Override
public void run(String... args) throws Exception {
List<String> questions = Arrays.asList(
"์ด ์ธ์‚ฌ์ดํŠธ๋ฅผ ์–ด๋–ค ์‚ฌ๋žŒ์—๊ฒŒ ์ถ”์ฒœํ•ด์ฃผ๊ณ  ์‹ถ๋‚˜์š”?",
"์ด ์ธ์‚ฌ์ดํŠธ๋ฅผ ์ฒ˜์Œ ๋ฐœ๊ฒฌํ–ˆ์„ ๋•Œ ๋ฌด์—‡์ด ๊ฐ€์žฅ ์ธ์ƒ ๊นŠ์—ˆ๋‚˜์š”?",
"์ด ์ธ์‚ฌ์ดํŠธ๋ฅผ ์‹ค์ œ๋กœ ์ ์šฉํ•ด ๋ณธ ๊ฒฝํ—˜์ด ์žˆ๋‚˜์š”?",
"์ด ์ธ์‚ฌ์ดํŠธ๋ฅผ ํ†ตํ•ด ๋ฐฐ์šด ๊ฒƒ์„ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์—๊ฒŒ ์–ด๋–ป๊ฒŒ ์„ค๋ช…ํ•˜๊ณ  ์‹ถ๋‚˜์š”?",
"์ด ์ธ์‚ฌ์ดํŠธ๋ฅผ ํ†ตํ•ด ์–ด๋–ค ์ƒˆ๋กœ์šด ๊ด€์ ์ด๋‚˜ ๋ฐฉ๋ฒ•์„ ๋ฐฐ์› ๋‚˜์š”?"
);

questions.forEach(question -> questionRepository.save(new Question(question)));
}
}
Loading

0 comments on commit c13d20d

Please sign in to comment.