| 1 | package edu.ucsb.cs156.dining.services; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.dining.models.Meal; | |
| 4 | import java.time.LocalDateTime; | |
| 5 | import java.time.format.DateTimeFormatter; | |
| 6 | import java.util.List; | |
| 7 | import java.util.Map; | |
| 8 | import lombok.extern.slf4j.Slf4j; | |
| 9 | import org.springframework.beans.factory.annotation.Value; | |
| 10 | import org.springframework.boot.web.client.RestTemplateBuilder; | |
| 11 | import org.springframework.http.HttpEntity; | |
| 12 | import org.springframework.http.HttpHeaders; | |
| 13 | import org.springframework.http.HttpMethod; | |
| 14 | import org.springframework.http.ResponseEntity; | |
| 15 | import org.springframework.stereotype.Service; | |
| 16 | import org.springframework.web.client.RestTemplate; | |
| 17 | import org.springframework.web.client.HttpClientErrorException; | |
| 18 | import org.springframework.web.server.ResponseStatusException; | |
| 19 | import org.springframework.http.HttpStatus; | |
| 20 | ||
| 21 | /** Service class to interact with Meal */ | |
| 22 | @Service | |
| 23 | @Slf4j | |
| 24 | public class MealService { | |
| 25 | ||
| 26 | @Value("${app.ucsb.api.consumer_key}") | |
| 27 | private String apiKey; | |
| 28 | ||
| 29 | private final RestTemplate restTemplate; | |
| 30 | ||
| 31 | public MealService(RestTemplateBuilder restTemplateBuilder) { | |
| 32 | this.restTemplate = restTemplateBuilder.build(); | |
| 33 | } | |
| 34 | ||
| 35 | private static final String MEAL_ENDPOINT = | |
| 36 | "https://api.ucsb.edu/dining/menu/v1/"; | |
| 37 | ||
| 38 | private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd"); | |
| 39 | ||
| 40 | /** | |
| 41 | * Fetches all meals served in a particular dining commons on a specific date. | |
| 42 | * | |
| 43 | * @param dateTime the date and time as a LocalDateTime | |
| 44 | * @param diningCommonsCode the dining commons code | |
| 45 | * @return a list of meals (e.g., breakfast, lunch, dinner) | |
| 46 | * @throws Exception if the API request fails | |
| 47 | */ | |
| 48 | public List<Meal> getMeals(LocalDateTime dateTime, String diningCommonsCode) throws Exception { | |
| 49 | // Convert LocalDateTime to YYYY-MM-DD format | |
| 50 | String formattedDate = dateTime.format(DATE_FORMATTER); | |
| 51 | ||
| 52 | String url = String.format("%s%s/%s", MEAL_ENDPOINT, formattedDate, diningCommonsCode); | |
| 53 | ||
| 54 | HttpHeaders headers = new HttpHeaders(); | |
| 55 |
1
1. getMeals : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
| 56 |
1
1. getMeals : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("accept", "application/json"); |
| 57 | ||
| 58 | HttpEntity<String> entity = new HttpEntity<>("body", headers); | |
| 59 | ||
| 60 | log.info("Fetching meals for date: {}, dining commons: {}", formattedDate, diningCommonsCode); | |
| 61 | ||
| 62 | try { | |
| 63 | ResponseEntity<Meal[]> response = restTemplate.exchange( | |
| 64 | url, HttpMethod.GET, entity, Meal[].class); | |
| 65 | ||
| 66 | Meal[] mealsArray = response.getBody(); | |
| 67 |
1
1. getMeals : negated conditional → KILLED |
if (mealsArray == null) { |
| 68 | throw new Exception("Failed to fetch meals data from API"); | |
| 69 | } | |
| 70 | ||
| 71 |
1
1. getMeals : replaced return value with Collections.emptyList for edu/ucsb/cs156/dining/services/MealService::getMeals → KILLED |
return List.of(mealsArray); |
| 72 | } catch (HttpClientErrorException.NotFound e) { | |
| 73 | log.error("API returned 404 for date: {} and dining commons: {}", formattedDate, diningCommonsCode); | |
| 74 | throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Meals not found for given date and dining commons"); | |
| 75 | } | |
| 76 | } | |
| 77 | } | |
Mutations | ||
| 55 |
1.1 |
|
| 56 |
1.1 |
|
| 67 |
1.1 |
|
| 71 |
1.1 |