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