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

사진찍기 알림 정보 조회 API #34

Merged
merged 5 commits into from
Jan 19, 2025
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.fourcut.diary.user;

import com.fourcut.diary.user.dto.response.PhotoCaptureInfoResponse;
import com.fourcut.diary.user.mapper.NotificationTimeResponseMapper;
import com.fourcut.diary.user.service.dto.PhotoCaptureInfoDto;
import com.fourcut.diary.user.service.notification.NotificationTimeService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/notification-time")
@RequiredArgsConstructor
public class NotificationTimeController implements NotificationTimeControllerSwagger {

private final NotificationTimeService notificationTimeService;

private final NotificationTimeResponseMapper notificationTimeResponseMapper;

@GetMapping("/photo")
public ResponseEntity<PhotoCaptureInfoResponse> getTakePhotoInfo(String socialId) {

PhotoCaptureInfoDto photoCaptureInfoDto = notificationTimeService.getTakePhotoInfoByUser(socialId);
return ResponseEntity.status(HttpStatus.OK).body(notificationTimeResponseMapper.toPhotoCaptureInfoResponse(photoCaptureInfoDto));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.fourcut.diary.user;

import com.fourcut.diary.config.resolver.UserAuthentication;
import com.fourcut.diary.user.dto.response.PhotoCaptureInfoResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;

@Tag(name = "Notification Time", description = "알림 관련 API 명세서")
public interface NotificationTimeControllerSwagger {

@Operation(summary = "사진찍기 정보 조회")
ResponseEntity<PhotoCaptureInfoResponse> getTakePhotoInfo(@UserAuthentication String socialId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.fourcut.diary.user.dto.response;

import java.time.LocalTime;

public record PhotoCaptureInfoResponse(

Boolean isPossiblePhotoCapture,

Integer currentPhotoIndex,

LocalTime photoCaptureExpiration
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.fourcut.diary.user.mapper;

import com.fourcut.diary.user.dto.response.PhotoCaptureInfoResponse;
import com.fourcut.diary.user.service.dto.PhotoCaptureInfoDto;
import org.springframework.stereotype.Component;

@Component
public class NotificationTimeResponseMapper {

public PhotoCaptureInfoResponse toPhotoCaptureInfoResponse(PhotoCaptureInfoDto dto) {
boolean isCapturePossible = dto.currentPhotoIndex() != -1;
return new PhotoCaptureInfoResponse(
isCapturePossible,
isCapturePossible ? dto.currentPhotoIndex() : null,
isCapturePossible ? dto.photoCaptureExpiration() : null
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public static List<LocalDateTime> generateRandomDateTimes(LocalDateTime start, L
return dateTimes;
}

public static Boolean getIsPossiblePhotoCapture(LocalDateTime beginningTime, LocalDateTime now) {
LocalDateTime expirationTime = beginningTime.plusMinutes(20);
return !now.isBefore(beginningTime) && !now.isAfter(expirationTime);
}

private static boolean isValidDateTime(LocalDateTime newDateTime, List<LocalDateTime> existingDateTimes, Duration minInterval) {
for (LocalDateTime dateTime : existingDateTimes) {
if (Duration.between(dateTime, newDateTime).abs().compareTo(minInterval) < 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.fourcut.diary.user.domain.User;
import com.fourcut.diary.user.domain.notification.NotificationTime;
import com.fourcut.diary.user.service.notification.NotificationTimeRetriever;
import com.fourcut.diary.user.service.notification.NotificationTimeUpdater;
import com.fourcut.diary.user.service.notification.NotificationTimeModifier;
import com.fourcut.diary.util.LocalDateTimeUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
Expand All @@ -24,7 +24,7 @@ public class UserScheduler {
private final UserRetriever userRetriever;

private final NotificationTimeRetriever notificationTimeRetriever;
private final NotificationTimeUpdater notificationTimeUpdater;
private final NotificationTimeModifier notificationTimeUpdater;

private final SnsService snsService;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.fourcut.diary.user.service.dto;

import java.time.LocalTime;

public record PhotoCaptureInfoDto(

Integer currentPhotoIndex,

LocalTime photoCaptureExpiration
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.List;

@Component
public class NotificationTimeUpdater {
public class NotificationTimeModifier {

public void setUserDailyNotificationTime(NotificationTime notificationTime, List<LocalDateTime> randomTimes) {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.fourcut.diary.user.service.notification;

import com.fourcut.diary.user.domain.User;
import com.fourcut.diary.user.domain.notification.NotificationTime;
import com.fourcut.diary.user.service.UserRetriever;
import com.fourcut.diary.user.service.dto.PhotoCaptureInfoDto;
import com.fourcut.diary.util.LocalDateTimeUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@RequiredArgsConstructor
public class NotificationTimeService {

private static final int EXPIRATION_MINUTES = 20;

private final UserRetriever userRetriever;

private final NotificationTimeRetriever notificationTimeRetriever;

@Transactional(readOnly = true)
public PhotoCaptureInfoDto getTakePhotoInfoByUser(String socialId) {
User user = userRetriever.getUserBySocialId(socialId);
NotificationTime notificationTime = notificationTimeRetriever.findNotificationTimeByUser(user);
LocalDateTime now = LocalDateTime.now();
return getTakePhotoInfo(notificationTime, now);
}

private PhotoCaptureInfoDto getTakePhotoInfo(NotificationTime notificationTime, LocalDateTime now) {
List<LocalDateTime> timeSlots = List.of(
notificationTime.getFirstTimeSlot(),
notificationTime.getSecondTimeSlot(),
notificationTime.getThirdTimeSlot(),
notificationTime.getFourthTimeSlot()
);

for (int i = 0; i < timeSlots.size(); i++) {
LocalDateTime slot = timeSlots.get(i);
if (LocalDateTimeUtil.getIsPossiblePhotoCapture(slot, now)) {
return new PhotoCaptureInfoDto(i + 1, slot.plusMinutes(EXPIRATION_MINUTES).toLocalTime());
}
}
return new PhotoCaptureInfoDto(-1, null);
}
}
Loading