-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
10646e8
commit c51488d
Showing
2 changed files
with
32 additions
and
31 deletions.
There are no files selected for viewing
61 changes: 31 additions & 30 deletions
61
src/main/java/com/aniversend/services/BirthdayService.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,48 +1,49 @@ | ||
package com.aniversend.services; | ||
|
||
import com.aniversend.models.People; | ||
import com.aniversend.repositories.PeopleRepository; | ||
import com.aniversend.util.MailTemplate; | ||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.logging.Logger; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.logging.Logger; | ||
import com.aniversend.models.People; | ||
import com.aniversend.repositories.PeopleRepository; | ||
import com.aniversend.util.MailTemplate; | ||
|
||
@Service | ||
public class BirthdayService { | ||
|
||
private final PeopleRepository peopleRepository; | ||
private final PeopleRepository peopleRepository; | ||
|
||
private final EmailService emailService; | ||
private final EmailService emailService; | ||
|
||
@Value("${number.email.template}") | ||
Integer numberEmailTemplate; | ||
@Value("${number.email.template}") | ||
Integer numberEmailTemplate; | ||
|
||
public BirthdayService(PeopleRepository peopleRepository, EmailService emailService) { | ||
this.peopleRepository = peopleRepository; | ||
this.emailService = emailService; | ||
} | ||
public BirthdayService(PeopleRepository peopleRepository, EmailService emailService) { | ||
this.peopleRepository = peopleRepository; | ||
this.emailService = emailService; | ||
} | ||
|
||
// @Scheduled(cron = "0 50 6 * * ?") // Executa todos os dias às 06:50 da manhã | ||
@Scheduled(fixedRate = 30000) // Executa a cada 30 segundos | ||
public void printConsoleMessageAndSendEmail() { | ||
Date today = new Date(); | ||
long numberOfPeoples = peopleRepository.count(); | ||
List<People> peoplesWithBirthday = peopleRepository.findByBirthday(today); | ||
// @Scheduled(cron = "0 50 6 * * ?") // Runs every day at 6:50 am | ||
@Scheduled(fixedRate = 30000) // Runs every 30 seconds | ||
public void printConsoleMessageAndSendEmail() { | ||
Date today = new Date(); | ||
List<People> peoplesWithBirthday = peopleRepository.findByBirthday(today); | ||
long numberOfPeoples = peoplesWithBirthday.size(); | ||
|
||
Logger.getGlobal().info("Number of peoples found: " + numberOfPeoples); | ||
Logger.getGlobal().info("Number of peoples celebrating their birthday today: " + peoplesWithBirthday.size()); | ||
Logger.getGlobal().info("Number of peoples found: " + numberOfPeoples); | ||
Logger.getGlobal().info("Number of peoples celebrating their birthday today: " + peoplesWithBirthday.size()); | ||
|
||
for (People people : peoplesWithBirthday) { | ||
String subject = "Happy birthday " + people.getName() + "!"; | ||
String message = MailTemplate.getMessage(people, numberEmailTemplate); | ||
String recipientEmail = people.getEmail(); | ||
for (People people : peoplesWithBirthday) { | ||
String subject = "Happy birthday " + people.getName() + "!"; | ||
String message = MailTemplate.getMessage(people, numberEmailTemplate); | ||
String recipientEmail = people.getEmail(); | ||
|
||
// emailService.sendEmail(recipientEmail, subject, message); | ||
// Logger.getGlobal().info("Email sent to: " + people.getEmail()); | ||
} | ||
} | ||
emailService.sendEmail(recipientEmail, subject, message); | ||
Logger.getGlobal().info("Email sent to: " + people.getEmail()); | ||
} | ||
} | ||
} |
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