| 1 | package edu.ucsb.cs156.dining.startup; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.dining.entities.Admin; | |
| 4 | import edu.ucsb.cs156.dining.repositories.AdminRepository; | |
| 5 | import edu.ucsb.cs156.dining.services.MenuItemsLoader; | |
| 6 | import java.util.List; | |
| 7 | import java.util.concurrent.CompletableFuture; | |
| 8 | import lombok.extern.slf4j.Slf4j; | |
| 9 | import org.springframework.beans.factory.annotation.Autowired; | |
| 10 | import org.springframework.beans.factory.annotation.Value; | |
| 11 | import org.springframework.boot.context.event.ApplicationReadyEvent; | |
| 12 | import org.springframework.context.event.EventListener; | |
| 13 | import org.springframework.stereotype.Component; | |
| 14 | ||
| 15 | /** This class contains a method that is called once at application startup time. */ | |
| 16 | @Slf4j | |
| 17 | @Component | |
| 18 | public class DiningStartup { | |
| 19 | ||
| 20 | @Value("#{'${app.admin.emails}'.split(',')}") | |
| 21 | List<String> adminEmails; | |
| 22 | ||
| 23 | @Autowired AdminRepository adminRepository; | |
| 24 | ||
| 25 | @Autowired MenuItemsLoader menuItemsLoader; | |
| 26 | ||
| 27 | /** | |
| 28 | * Called once at application startup time. Loads all emails in ADMIN_EMAILS into the Admin table, | |
| 29 | * then starts loading the current month's menu items in the background. | |
| 30 | */ | |
| 31 | @EventListener(ApplicationReadyEvent.class) | |
| 32 | public void alwaysRunOnStartup() { | |
| 33 | log.info("alwaysRunOnStartup called"); | |
| 34 | ||
| 35 |
1
1. alwaysRunOnStartup : removed call to java/util/List::forEach → KILLED |
adminEmails.forEach( |
| 36 | (email) -> { | |
| 37 | try { | |
| 38 |
1
1. lambda$alwaysRunOnStartup$0 : negated conditional → KILLED |
if (!adminRepository.existsByEmail(email)) { |
| 39 | Admin admin = new Admin(email); | |
| 40 | adminRepository.save(admin); | |
| 41 | } | |
| 42 | } catch (Exception e) { | |
| 43 | log.error("Error saving admin email {}: {}", email, e.getMessage()); | |
| 44 | } | |
| 45 | }); | |
| 46 | ||
| 47 | // Runs in the background so a slow or rate-limited API never delays startup. | |
| 48 | CompletableFuture.runAsync(menuItemsLoader::loadCurrentMonth); | |
| 49 | } | |
| 50 | } | |
Mutations | ||
| 35 |
1.1 |
|
| 38 |
1.1 |