Untitled

mail@pastecode.io avatar
unknown
java
6 days ago
1.9 kB
1
Indexable
Never
package com.capgemini.wsb.fitnesstracker.notification;

import com.capgemini.wsb.fitnesstracker.mail.api.EmailDto;
import com.capgemini.wsb.fitnesstracker.mail.api.EmailSender;
import com.capgemini.wsb.fitnesstracker.mail.internal.EmailService;
import com.capgemini.wsb.fitnesstracker.training.api.Training;
import com.capgemini.wsb.fitnesstracker.training.api.TrainingProvider;
import com.capgemini.wsb.fitnesstracker.user.api.User;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@RequiredArgsConstructor
@EnableScheduling
public class TrainingReportGenerator {

    private final TrainingProvider trainingProvider;
    private final EmailSender emailSender;


    @Scheduled(fixedDelay = 60000)
    public void generateTrainingReport() {
        List<Training> allTrainings = trainingProvider.getAllTrainings();
        Map<User, List<Training>> userTrainings = allTrainings.stream().collect(Collectors.groupingBy(Training::getUser));
        userTrainings.forEach((user, trainings) -> {
            int count = trainings.size();
            String result = buildContent(user, count);
            emailSender.send(new EmailDto(user.getEmail(), "Raport tygodniowy", result));
        });
    }

    private static String buildContent(User user, int count) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("Dzien dobry, " + user.getFirstName() + " " + user.getLastName() + "\n");
        stringBuilder.append("Oto twoje podsumowanie:");
        stringBuilder.append("Wykonales " + count + " treningow.");
        String result = stringBuilder.toString();
        return result;
    }


}
Leave a Comment