-
Notifications
You must be signed in to change notification settings - Fork 0
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
[#20] Task 등록, 수정, 삭제 기능에 Reminder 설정 기능 추가 #21
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5e63ce0
refactor: Task에 reminderEnabled 속성 추가
olivejua 091034f
refactor: Task에 reminderEnabled 속성 추가
olivejua 9de7ecf
feat: ReminderSettingsService CRUD 구현
olivejua ee4a254
refactor: Task delete 에 Transactional 추가
olivejua d601ded
refactor: ReminderSettings 업데이트 구현 및 테스트
olivejua c5fc51c
refactor: UserService 생성
olivejua 14b59f0
refactor: ReminderNotificationService 서비스 로직 추가
olivejua File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...c/main/java/com/taskbuddy/core/database/repository/DefaultReminderSettingsRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.taskbuddy.core.database.repository; | ||
|
||
import com.taskbuddy.core.domain.ReminderSettings; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.Optional; | ||
|
||
//임시생성 | ||
@Repository | ||
public class DefaultReminderSettingsRepository implements ReminderSettingsRepository { | ||
|
||
@Override | ||
public Optional<ReminderSettings> findByTaskId(Long taskId) { | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public void save(ReminderSettings reminderSettings) { | ||
|
||
} | ||
|
||
@Override | ||
public void deleteById(Long id) { | ||
|
||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
core/src/main/java/com/taskbuddy/core/database/repository/ReminderSettingsRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.taskbuddy.core.database.repository; | ||
|
||
import com.taskbuddy.core.domain.ReminderSettings; | ||
|
||
import java.util.Optional; | ||
|
||
public interface ReminderSettingsRepository { | ||
|
||
Optional<ReminderSettings> findByTaskId(Long taskId); | ||
|
||
void save(ReminderSettings reminderSettings); | ||
|
||
void deleteById(Long id); | ||
} |
66 changes: 66 additions & 0 deletions
66
core/src/main/java/com/taskbuddy/core/domain/ReminderSettings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.taskbuddy.core.domain; | ||
|
||
import com.taskbuddy.core.service.port.ClockHolder; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
import java.time.Duration; | ||
import java.time.LocalDateTime; | ||
|
||
@Getter | ||
public class ReminderSettings { | ||
private final Long id; | ||
private final Task task; | ||
private LocalDateTime lastReminderSentTime; | ||
private Duration reminderInterval; | ||
private final LocalDateTime createdAt; | ||
private LocalDateTime updatedAt; | ||
|
||
@Builder | ||
public ReminderSettings(Long id, Task task, LocalDateTime lastReminderSentTime, Duration reminderInterval, LocalDateTime createdAt, LocalDateTime updatedAt) { | ||
this.id = id; | ||
this.task = task; | ||
this.lastReminderSentTime = lastReminderSentTime; | ||
this.reminderInterval = reminderInterval; | ||
this.createdAt = createdAt; | ||
this.updatedAt = updatedAt; | ||
} | ||
|
||
public static ReminderSettings from(Task task, Duration reminderInterval, ClockHolder clockHolder) { | ||
final LocalDateTime currentDateTime = clockHolder.currentDateTime(); | ||
|
||
return ReminderSettings.builder() | ||
.task(task) | ||
.reminderInterval(reminderInterval) | ||
.createdAt(currentDateTime) | ||
.updatedAt(currentDateTime) | ||
.build(); | ||
} | ||
|
||
public void updateLastReminderSentTime(LocalDateTime lastReminderSentTime, ClockHolder clockHolder) { | ||
this.lastReminderSentTime = lastReminderSentTime; | ||
this.updatedAt = clockHolder.currentDateTime(); | ||
} | ||
|
||
public boolean isReminderDue(ClockHolder clockHolder) { | ||
LocalDateTime currentDateTime = clockHolder.currentDateTime(); | ||
LocalDateTime taskStartDateTime = task.getTimeFrame().startDateTime(); | ||
|
||
if (currentDateTime.isBefore(taskStartDateTime)) { | ||
return false; | ||
} | ||
|
||
Duration timeSinceStart = Duration.between(taskStartDateTime, currentDateTime); | ||
|
||
return timeSinceStart.toMinutes() % reminderInterval.toMinutes() == 0; | ||
} | ||
|
||
public void updateReminderInterval(Duration reminderInterval, ClockHolder clockHolder) { | ||
this.reminderInterval = reminderInterval; | ||
this.updatedAt = clockHolder.currentDateTime(); | ||
} | ||
|
||
public Long getTaskId() { | ||
return task.getId(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
core/src/main/java/com/taskbuddy/core/domain/TaskContentUpdate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
package com.taskbuddy.core.domain; | ||
|
||
import java.time.Duration; | ||
import java.time.LocalDateTime; | ||
|
||
public record TaskContentUpdate( | ||
Long id, | ||
Long userId, | ||
String title, | ||
String description, | ||
Boolean reminderEnabled, | ||
Duration reminderInterval, | ||
LocalDateTime startDateTime, | ||
LocalDateTime endDateTime | ||
) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
package com.taskbuddy.core.domain; | ||
|
||
import java.time.Duration; | ||
import java.time.LocalDateTime; | ||
|
||
public record TaskCreate ( | ||
Long userId, | ||
String title, | ||
String description, | ||
Boolean reminderEnabled, | ||
Duration reminderInterval, | ||
LocalDateTime startDateTime, | ||
LocalDateTime endDateTime | ||
) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,14 @@ | ||
package com.taskbuddy.core.domain; | ||
|
||
public record User(Long id) { | ||
import lombok.Getter; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Getter | ||
public class User { | ||
private Long id; | ||
private boolean loggedIn; | ||
private boolean reminderEnabled; | ||
private LocalDateTime createdAt; | ||
private LocalDateTime updatedAt; | ||
} |
48 changes: 48 additions & 0 deletions
48
core/src/main/java/com/taskbuddy/core/service/ReminderNotificationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.taskbuddy.core.service; | ||
|
||
import com.taskbuddy.core.domain.ReminderSettings; | ||
import com.taskbuddy.core.domain.Task; | ||
import com.taskbuddy.core.domain.User; | ||
import com.taskbuddy.core.service.port.ClockHolder; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class ReminderNotificationService { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 로직은 좀 더 고민해보고, 어떻게 할지 생각해보죠. :-) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵 지난시간에 말씀해주신 방법과 함께 고민해보겠습니다 ㅎㅎ |
||
private final ReminderSettingsService reminderSettingsService; | ||
private final UserService userService; | ||
// private final NotificationService notificationService; | ||
private final ClockHolder clockHolder; | ||
|
||
public void sendNotification(Task task) { | ||
User user = task.getUser(); | ||
|
||
// 1. 유저가 리마인드 알림설정이 켜져있는지 확인 | ||
if (!user.isReminderEnabled()) { | ||
return; // 유저가 알림을 원하지 않으면 스킵 | ||
} | ||
|
||
// 2. Task의 리마인드 설정이 켜져 있는지 확인 | ||
if (!task.isReminderEnabled()) { | ||
return; // Task의 리마인드 설정이 꺼져 있으면 스킵 | ||
} | ||
|
||
// 3. 리마인더주기에 맞는지 확인 | ||
ReminderSettings reminderSettings = reminderSettingsService.getByTaskId(task.getId()); | ||
|
||
// 마지막으로 알림을 보낼 시간이 리마인드 주기 이상일 경우에만 알림 전송 | ||
if (!reminderSettings.isReminderDue(clockHolder)) { | ||
return; // 아직 리마인드 주기가 지나지 않았으므로 스킵 | ||
} | ||
|
||
// 4. 유저가 현재 접속 중인지 확인 (접속 중이 아니면 리마인드) | ||
if (!userService.isUserLoggedIn(user)) { | ||
//리마인더 알림 전송 | ||
// notificationService.sendReminder(user, task); | ||
reminderSettingsService.updateLastSentTime(reminderSettings, LocalDateTime.now()); | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
core/src/main/java/com/taskbuddy/core/service/ReminderSettingsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.taskbuddy.core.service; | ||
|
||
import com.taskbuddy.core.database.repository.ReminderSettingsRepository; | ||
import com.taskbuddy.core.domain.ReminderSettings; | ||
import com.taskbuddy.core.domain.Task; | ||
import com.taskbuddy.core.service.port.ClockHolder; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.Duration; | ||
import java.time.LocalDateTime; | ||
import java.util.Optional; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class ReminderSettingsService { | ||
private final ReminderSettingsRepository reminderSettingsRepository; | ||
private final ClockHolder clockHolder; | ||
|
||
public ReminderSettings getByTaskId(Long taskId) { | ||
return reminderSettingsRepository.findByTaskId(taskId) | ||
.orElseThrow(() -> new IllegalArgumentException("Task Settings with given task id does not exist.")); | ||
} | ||
|
||
public void initialize(Task task, Duration reminderInterval) { | ||
if (!task.isReminderEnabled()) { | ||
return; | ||
} | ||
|
||
ReminderSettings reminderSettings = ReminderSettings.from(task, reminderInterval, clockHolder); | ||
reminderSettingsRepository.save(reminderSettings); | ||
} | ||
|
||
public void update(Task task, Duration reminderInterval) { | ||
Optional<ReminderSettings> optionalReminderSettings = reminderSettingsRepository.findByTaskId(task.getId()); | ||
|
||
if (optionalReminderSettings.isEmpty()) { | ||
initialize(task, reminderInterval); | ||
return; | ||
} | ||
|
||
ReminderSettings reminderSettings = optionalReminderSettings.get(); | ||
|
||
if (!task.isReminderEnabled()) { | ||
reminderSettingsRepository.deleteById(reminderSettings.getId()); | ||
} else { | ||
reminderSettings.updateReminderInterval(reminderInterval, clockHolder); | ||
reminderSettingsRepository.save(reminderSettings); | ||
} | ||
} | ||
|
||
public void updateLastSentTime(ReminderSettings reminderSettings, LocalDateTime lastSentTime) { | ||
reminderSettings.updateLastReminderSentTime(lastSentTime, clockHolder); | ||
} | ||
|
||
public void deleteByTaskId(Long taskId) { | ||
reminderSettingsRepository.findByTaskId(taskId) | ||
.ifPresent(reminderSettings -> reminderSettingsRepository.deleteById(reminderSettings.getId())); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저 예전부터 궁금하던게 있는데요. clockHolder는 어떤 역할을 하는 것인가요?