Skip to content

Commit

Permalink
fix: 팝콘 추천작 random 반환
Browse files Browse the repository at this point in the history
  • Loading branch information
AlmondBreez3 committed Jan 29, 2024
1 parent d8c7939 commit f5856aa
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,36 @@ public class ScheduleService {
// }
// }
//}
//

@Scheduled(cron = "0 0/1 * * * *")
private void notifyReservation() {
LocalDateTime now = LocalDateTime.now().withSecond(0).withNano(0);
//LocalDateTime reservationTime = now.plusDays(1);

//userScreening에서 isBookMarked인 것들 중에서 user id, screening id가져와서 List<User> List<Screening>
//screening에서 startDate가져와서 startDate가 내일이면 알람을 보낼 수 있게 짜봐 fcm이랑 스프링 쓰고 있어

List<UserScreening> bookmarkedUserScreenings = userScreeningAdaptor.findByBookMarked();

for (UserScreening userScreening : bookmarkedUserScreenings) {
LocalDateTime screeningStartDate = userScreening.getScreening().getScreeningStartDate();

// 오늘이 screeningStartDate의 하루 전인 경우 해당 Screening을 가져옴
if (screeningStartDate.toLocalDate().isEqual(now.toLocalDate())) {
Long userId = userScreening.getUser().getId();
NotificationRequest notificationRequests = new NotificationRequest(userScreening.getScreening(), userId, userScreening.getScreening().getTitle());
sendNotifications(notificationRequests);
}
}
}

private void sendNotifications(NotificationRequest requests) {
// FCM을 사용하여 알림을 보내는 로직
fcmService.sendMessageByToken(requests);


}

// private void sendNotifications(List<NotificationRequest> requests) {
// for (NotificationRequest notificationRequest : requests) {
// // FCM을 사용하여 알림을 보내는 로직
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ public List<RecommendedPopcorn> findByThreeIds() {
List<RecommendedPopcorn> thisWeekList = findAllThisWeek();

if(thisWeekList.size()<=3) {
return recommendedPopcornRepository.findAll();
return thisWeekList;
}
Set<Long> numberArray = generate(thisWeekList);
System.out.println(numberArray.size());
for (Long number : numberArray) {
Optional<RecommendedPopcorn> foundItem = recommendedPopcornRepository.findById(number);

Expand Down
4 changes: 4 additions & 0 deletions Domain/src/main/java/com/example/fcm/entity/FCMToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ public static FCMToken createFCMToken(User user, String fcmToken) {
.fcmToken(fcmToken)
.build();
}

public void updateToken(String fcmToken) {
this.fcmToken = fcmToken;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@
import com.example.fcm.entity.FCMToken;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface FcmRepository extends JpaRepository<FCMToken, Long> {
Optional<FCMToken> findByUserId(Long userId);


}
17 changes: 15 additions & 2 deletions Domain/src/main/java/com/example/fcm/service/FcmService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Slf4j
@Service
Expand All @@ -22,12 +25,22 @@ public class FcmService {
private final FirebaseMessaging firebaseMessaging;
private final UserRepository userRepository;
private final FcmRepository fcmRepository;
@Transactional
public void registerFCMToken(Long userId, FcmRegistrationRequest request) {
User user = userRepository.findById(userId)
.orElseThrow(() -> UserNotFoundException.EXCEPTION);
FCMToken fcmToken = FCMToken.createFCMToken(user, request.getFcmToken());

fcmRepository.save(fcmToken);
// 이미 등록된 FCMToken이 있는지 확인
Optional<FCMToken> existingToken = fcmRepository.findByUserId(userId);

if (existingToken.isPresent()) {
// 이미 등록된 FCMToken이 있는 경우 값을 업데이트
existingToken.get().updateToken(request.getFcmToken());
} else {
// 등록된 FCMToken이 없는 경우 새로 생성하여 저장
FCMToken newToken = FCMToken.createFCMToken(user, request.getFcmToken());
fcmRepository.save(newToken);
}
}


Expand Down

0 comments on commit f5856aa

Please sign in to comment.