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

[Feature] 리마인드 관련 API 개발 #26

Merged
merged 12 commits into from
Jan 9, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,10 @@ public ApiResponse deleteTimer(
timerService.deleteTimer(userId,timerId);
return ApiResponse.success(Success.UPDATE_TIMER_DATETIME_SUCCESS);
}

@GetMapping("/main")
@ResponseStatus(HttpStatus.OK)
public ApiResponse getTimerPage(
@RequestHeader("userId") Long userId){
return ApiResponse.success(Success.GET_TIMER_PAGE_SUCCESS, timerService.getTimerPage(userId));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.app.toaster.controller.response.timer;

import lombok.Builder;

import java.util.List;

@Builder
public record GetTimerPageResponseDto(List<CompletedTimerDto> completedTimerList, List<WaitingTimerDto> waitingTimerList) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public record GetTimerResponseDto (String categoryName,
String remindTime,
ArrayList<String> remindDates) {
ArrayList<Integer> remindDates) {
public static GetTimerResponseDto of(Reminder reminder){
return new GetTimerResponseDto(reminder.getCategory().getTitle(), reminder.getRemindTime().toString(), reminder.getRemindDates());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.app.toaster.controller.response.timer;

import com.app.toaster.domain.Reminder;

import java.util.ArrayList;
import java.util.List;

public record WaitingTimerDto(Long timerId, String remindTime, String remindDates, Boolean isAlarm) {
public static WaitingTimerDto of(Reminder timer, String remindTime, String remindDates) {
return new WaitingTimerDto(timer.getId(), remindTime, remindDates, timer.getIsAlarm());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public enum Success {
GET_CATEORIES_SUCCESS(HttpStatus.OK, "전체 카테고리 조회 성공"),
GET_CATEORY_SUCCESS(HttpStatus.OK, "세부 카테고리 조회 성공"),
GET_TIMER_SUCCESS(HttpStatus.OK, "타이머 조회 성공"),
GET_TIMER_PAGE_SUCCESS(HttpStatus.OK, "타이머 페이지 조회 성공"),

LOGIN_SUCCESS(HttpStatus.OK, "로그인 성공"),
RE_ISSUE_TOKEN_SUCCESS(HttpStatus.OK, "토큰 재발급 성공"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import com.app.toaster.controller.request.timer.CreateTimerRequestDto;
import com.app.toaster.controller.request.timer.UpdateTimerCommentDto;
import com.app.toaster.controller.request.timer.UpdateTimerDateTimeDto;
import com.app.toaster.controller.response.timer.CompletedTimerDto;
import com.app.toaster.controller.response.timer.GetTimerPageResponseDto;
import com.app.toaster.controller.response.timer.GetTimerResponseDto;
import com.app.toaster.controller.response.timer.WaitingTimerDto;
import com.app.toaster.domain.Category;
import com.app.toaster.domain.Reminder;
import com.app.toaster.domain.User;
Expand All @@ -18,9 +20,14 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
Expand All @@ -29,6 +36,8 @@ public class TimerService {
private final CategoryRepository categoryRepository;
private final TimerRepository timerRepository;

private LocalDateTime now;

@Transactional
public void createTimer(Long userId, CreateTimerRequestDto createTimerRequestDto){
User presentUser = findUser(userId);
Expand Down Expand Up @@ -102,14 +111,25 @@ public void deleteTimer(Long userId, Long timerId){
timerRepository.delete(reminder);
}

// public GetTimerPageResponseDto getTimerPage(Long userId){
// User presentUser = findUser(userId);
//
// ArrayList<Reminder> reminders = timerRepository.findAllByUser(presentUser);
//
//
//
// }
public GetTimerPageResponseDto getTimerPage(Long userId){
User presentUser = findUser(userId);
ArrayList<Reminder> reminders = timerRepository.findAllByUser(presentUser);

List<CompletedTimerDto> completedTimerList = reminders.stream()
.filter(this::isCompletedTimer)
.map(this::createCompletedTimerDto)
.collect(Collectors.toList());

List<WaitingTimerDto> waitingTimerList = reminders.stream()
.filter(reminder -> !isCompletedTimer(reminder))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 필터까지 깔끔하게 적용한 것 같네용!! 좋습니당! ☺️

.map(this::createWaitingTimerDto)
.collect(Collectors.toList());

return GetTimerPageResponseDto.builder()
.completedTimerList(completedTimerList)
.waitingTimerList(waitingTimerList)
.build();
}

//해당 유저 탐색
private User findUser(Long userId){
Expand All @@ -118,12 +138,44 @@ private User findUser(Long userId){
);
}

// private boolean isCompletedTimer(Reminder reminder){
// // 현재 시간
// LocalDateTime now = LocalDateTime.now();
//
//
//
//
// }
// 완료된 타이머이고 알람이 켜져있는지 식별
private boolean isCompletedTimer(Reminder reminder){
// 현재 시간
now = LocalDateTime.now();

LocalTime futureDateTime = LocalTime.from(now.plusHours(1));
LocalTime pastDateTime = LocalTime.from(now.minusHours(1));

if (reminder.getRemindDates().contains(now.getDayOfWeek().getValue())) {
LocalTime reminderTime = reminder.getRemindTime();
return !reminderTime.isBefore(pastDateTime) && !reminderTime.isAfter(futureDateTime) && reminder.getIsAlarm();
}

return false;
}

// 완료된 타이머 날짜/시간 포맷
private CompletedTimerDto createCompletedTimerDto(Reminder reminder) {
String time = reminder.getRemindTime().format(DateTimeFormatter.ofPattern("a hh:mm"));
String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("E요일"));
return CompletedTimerDto.of(reminder, time, date);
}

// 대기중인 타이머 날짜/시간 포맷
private WaitingTimerDto createWaitingTimerDto(Reminder reminder) {
String time = reminder.getRemindTime().format(DateTimeFormatter.ofPattern("a h시"));
String dates = reminder.getRemindDates().stream()
.map(this::mapIndexToDayString)
.collect(Collectors.joining(", "));
return WaitingTimerDto.of(reminder, time, dates);
}

// 인덱스로 요일 알아내기
private String mapIndexToDayString(int index) {
DayOfWeek dayOfWeek = DayOfWeek.of(index);
String dayName = dayOfWeek.getDisplayName(java.time.format.TextStyle.FULL, Locale.getDefault());

return dayName.substring(0, 1);
}

}