| 1 | package edu.ucsb.cs156.courses.services; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 4 | import com.fasterxml.jackson.core.type.TypeReference; | |
| 5 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 6 | import edu.ucsb.cs156.courses.entities.UCSBSubject; | |
| 7 | import edu.ucsb.cs156.courses.repositories.UCSBSubjectRepository; | |
| 8 | import java.util.ArrayList; | |
| 9 | import java.util.List; | |
| 10 | import lombok.extern.slf4j.Slf4j; | |
| 11 | import org.springframework.beans.factory.annotation.Autowired; | |
| 12 | import org.springframework.beans.factory.annotation.Value; | |
| 13 | import org.springframework.boot.web.client.RestTemplateBuilder; | |
| 14 | import org.springframework.http.HttpEntity; | |
| 15 | import org.springframework.http.HttpHeaders; | |
| 16 | import org.springframework.http.HttpMethod; | |
| 17 | import org.springframework.http.MediaType; | |
| 18 | import org.springframework.http.ResponseEntity; | |
| 19 | import org.springframework.stereotype.Service; | |
| 20 | import org.springframework.web.client.RestTemplate; | |
| 21 | ||
| 22 | @Slf4j | |
| 23 | @Service("UCSBSubjects") | |
| 24 | public class UCSBSubjectsService { | |
| 25 | ||
| 26 | @Autowired private ObjectMapper mapper; | |
| 27 | @Autowired private UCSBSubjectRepository subjectRepository; | |
| 28 | ||
| 29 | @Value("${app.ucsb.api.consumer_key}") | |
| 30 | private String apiKey; | |
| 31 | ||
| 32 | public static final String ENDPOINT = | |
| 33 | "https://api.ucsb.edu/students/lookups/v1/subjects?includeInactive=false"; | |
| 34 | ||
| 35 | private final RestTemplate restTemplate; | |
| 36 | ||
| 37 | public UCSBSubjectsService(RestTemplateBuilder restTemplateBuilder) { | |
| 38 | restTemplate = restTemplateBuilder.build(); | |
| 39 | } | |
| 40 | ||
| 41 | public List<UCSBSubject> get() throws JsonProcessingException { | |
| 42 | ||
| 43 | HttpHeaders headers = new HttpHeaders(); | |
| 44 |
1
1. get : removed call to org/springframework/http/HttpHeaders::setAccept → KILLED |
headers.setAccept(List.of(MediaType.APPLICATION_JSON)); |
| 45 |
1
1. get : removed call to org/springframework/http/HttpHeaders::setContentType → KILLED |
headers.setContentType(MediaType.APPLICATION_JSON); |
| 46 |
1
1. get : removed call to org/springframework/http/HttpHeaders::set → KILLED |
headers.set("ucsb-api-key", this.apiKey); |
| 47 | ||
| 48 | HttpEntity<String> entity = new HttpEntity<>(headers); | |
| 49 | ResponseEntity<String> re = | |
| 50 | restTemplate.exchange(ENDPOINT, HttpMethod.GET, entity, String.class); | |
| 51 | ||
| 52 | String retBody = re.getBody(); | |
| 53 | List<UCSBSubject> subjects = | |
| 54 | mapper.readValue(retBody, new TypeReference<List<UCSBSubject>>() {}); | |
| 55 | ||
| 56 |
1
1. get : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBSubjectsService::get → KILLED |
return subjects; |
| 57 | } | |
| 58 | ||
| 59 | public List<UCSBSubject> loadAllSubjects() throws JsonProcessingException { | |
| 60 | List<UCSBSubject> subjects = this.get(); | |
| 61 | List<UCSBSubject> savedSubjects = new ArrayList<UCSBSubject>(); | |
| 62 | ||
| 63 |
1
1. loadAllSubjects : removed call to java/util/List::forEach → KILLED |
subjects.forEach( |
| 64 | (ucsbSubject) -> { | |
| 65 | subjectRepository.save(ucsbSubject); | |
| 66 | savedSubjects.add(ucsbSubject); | |
| 67 | }); | |
| 68 | log.info("subjects={}", subjects); | |
| 69 |
1
1. loadAllSubjects : replaced return value with Collections.emptyList for edu/ucsb/cs156/courses/services/UCSBSubjectsService::loadAllSubjects → KILLED |
return savedSubjects; |
| 70 | } | |
| 71 | } | |
Mutations | ||
| 44 |
1.1 |
|
| 45 |
1.1 |
|
| 46 |
1.1 |
|
| 56 |
1.1 |
|
| 63 |
1.1 |
|
| 69 |
1.1 |