Skip to content

Commit

Permalink
feat: 스케쥴러 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
qormoon committed Jun 27, 2024
1 parent 1dff7eb commit f1e1c50
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/example/fiurinee/FiurineeApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.example.fiurinee.domain.flower.service.FlowerService;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class FiurineeApplication implements CommandLineRunner {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.example.fiurinee.domain.mail;

import com.example.fiurinee.domain.anniversary.entity.Anniversary;
import com.example.fiurinee.domain.anniversary.service.AnniversaryService;
import com.example.fiurinee.domain.member.entity.Member;
import com.example.fiurinee.domain.member.service.MemberService;
import jakarta.mail.MessagingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class AnniversarySchedular {
@Autowired
private AnniversaryService anniversaryService;

@Autowired
private MailService mailService;

@Autowired
private MemberService memberService;

@Scheduled(cron = "0 27 23 * * *")
public void sendDDayZeroAnniversaryEmails() {
List<Member> members = memberService.findAll();
for (Member member : members) {
List<Anniversary> anniversaries = member.getAnniversaries();
for (Anniversary anniversary : anniversaries) {
List<Map<String, Integer>> allDDays = anniversaryService.calculateDDay(anniversary);
for (Map<String, Integer> dDay : allDDays) {
for (Map.Entry<String, Integer> entry : dDay.entrySet()) {
if (entry.getValue() == 0) {
try {
mailService.sendAnniversaryEmail(anniversary.getMember(), anniversary);
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
Expand Down Expand Up @@ -37,5 +39,9 @@ public MemberResponseDTO getMemberDtoById(Long id) {
return MemberResponseDTO.of(member);
}

public List<Member> findAll() {
return memberRepository.findAll();
}


}

0 comments on commit f1e1c50

Please sign in to comment.