1 | package edu.ucsb.cs156.example.controllers; | |
2 | ||
3 | import edu.ucsb.cs156.example.entities.Articles; | |
4 | import edu.ucsb.cs156.example.errors.EntityNotFoundException; | |
5 | import edu.ucsb.cs156.example.repositories.ArticlesRepository; | |
6 | ||
7 | import io.swagger.v3.oas.annotations.Operation; | |
8 | import io.swagger.v3.oas.annotations.Parameter; | |
9 | import io.swagger.v3.oas.annotations.tags.Tag; | |
10 | import lombok.extern.slf4j.Slf4j; | |
11 | ||
12 | import com.fasterxml.jackson.core.JsonProcessingException; | |
13 | ||
14 | import org.springframework.beans.factory.annotation.Autowired; | |
15 | import org.springframework.format.annotation.DateTimeFormat; | |
16 | import org.springframework.security.access.prepost.PreAuthorize; | |
17 | import org.springframework.web.bind.annotation.DeleteMapping; | |
18 | import org.springframework.web.bind.annotation.GetMapping; | |
19 | import org.springframework.web.bind.annotation.PostMapping; | |
20 | import org.springframework.web.bind.annotation.PutMapping; | |
21 | import org.springframework.web.bind.annotation.RequestBody; | |
22 | import org.springframework.web.bind.annotation.RequestMapping; | |
23 | import org.springframework.web.bind.annotation.RequestParam; | |
24 | import org.springframework.web.bind.annotation.RestController; | |
25 | ||
26 | import jakarta.validation.Valid; | |
27 | ||
28 | import java.time.LocalDateTime; | |
29 | ||
30 | ||
31 | /** | |
32 | * This is a REST controller for Articles | |
33 | */ | |
34 | @Tag(name = "Articles") | |
35 | @RequestMapping("/api/articles") | |
36 | @RestController | |
37 | @Slf4j | |
38 | public class ArticlesController extends ApiController { | |
39 | ||
40 | @Autowired | |
41 | ArticlesRepository articlesRepository; | |
42 | ||
43 | /** | |
44 | * List all Articles | |
45 | * | |
46 | * @return an iterable of Articles | |
47 | */ | |
48 | @Operation(summary= "List all Articles") | |
49 | @PreAuthorize("hasRole('ROLE_USER')") | |
50 | @GetMapping("/all") | |
51 | public Iterable<Articles> allArticles() { | |
52 | Iterable<Articles> articles = articlesRepository.findAll(); | |
53 |
1
1. allArticles : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/ArticlesController::allArticles → KILLED |
return articles; |
54 | } | |
55 | ||
56 | /** | |
57 | * Create a new article | |
58 | * | |
59 | * @param title the title of the article | |
60 | * @param url the URL of the article | |
61 | * @param explanation the explanation of the article | |
62 | * @param email the email of the user who posted the article | |
63 | * @param dateAdded the date the article was added | |
64 | * @return the saved article | |
65 | * @throws JsonProcessingException on error in processing JSON | |
66 | */ | |
67 | @Operation(summary= "Create a new article") | |
68 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
69 | @PostMapping("/post") | |
70 | public Articles postArticles( | |
71 | @Parameter(name="title") @RequestParam String title, | |
72 | @Parameter(name="url") @RequestParam String url, | |
73 | @Parameter(name="explanation") @RequestParam String explanation, | |
74 | @Parameter(name="email") @RequestParam String email, | |
75 | @Parameter(name="dateAdded", description="date (in iso format, e.g. YYYY-mm-ddTHH:MM:SS; see https://en.wikipedia.org/wiki/ISO_8601)") @RequestParam("dateAdded") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime dateAdded) | |
76 | throws JsonProcessingException { | |
77 | ||
78 | // For an explanation of @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) | |
79 | // See: https://www.baeldung.com/spring-date-parameters | |
80 | ||
81 | log.info("dateAdded={}", dateAdded); | |
82 | ||
83 | Articles articles = new Articles(); | |
84 |
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED |
articles.setTitle(title); |
85 |
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED |
articles.setUrl(url); |
86 |
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED |
articles.setExplanation(explanation); |
87 |
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED |
articles.setEmail(email); |
88 |
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED |
articles.setDateAdded(dateAdded); |
89 | ||
90 | Articles savedArticles = articlesRepository.save(articles); | |
91 | ||
92 |
1
1. postArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postArticles → KILLED |
return savedArticles; |
93 | } | |
94 | ||
95 | /** | |
96 | * Get a single article by id | |
97 | * | |
98 | * @param id the id of the article | |
99 | * @return the article | |
100 | * @throws EntityNotFoundException if the article is not found | |
101 | */ | |
102 | ||
103 | // … | |
104 | ||
105 | @Operation(summary = "Get a single article") | |
106 | @PreAuthorize("hasRole('USER')") | |
107 | @GetMapping("") | |
108 | public Articles getById(@RequestParam Long id) { | |
109 |
1
1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::getById → KILLED |
return articlesRepository.findById(id) |
110 |
1
1. lambda$getById$0 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$getById$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Articles.class, id)); |
111 | } | |
112 | ||
113 | /** | |
114 | * Update an existing article | |
115 | * | |
116 | * @param id the id of the article to update | |
117 | * @param incoming the new data for the article (JSON) | |
118 | * @return the updated article | |
119 | * @throws EntityNotFoundException if the article with the given id is not found | |
120 | */ | |
121 | @Operation(summary = "Update an existing article") | |
122 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
123 | @PutMapping("") | |
124 | public Articles updateArticle( | |
125 | @Parameter(name = "id", description = "ID of the article to update") @RequestParam Long id, | |
126 | @RequestBody @Valid Articles incoming) { | |
127 | ||
128 | Articles articles = articlesRepository.findById(id) | |
129 |
1
1. lambda$updateArticle$1 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$updateArticle$1 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Articles.class, id)); |
130 | ||
131 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED |
articles.setTitle(incoming.getTitle()); |
132 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED |
articles.setUrl(incoming.getUrl()); |
133 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED |
articles.setExplanation(incoming.getExplanation()); |
134 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED |
articles.setEmail(incoming.getEmail()); |
135 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED |
articles.setDateAdded(incoming.getDateAdded()); |
136 | ||
137 | Articles updatedArticles = articlesRepository.save(articles); | |
138 | ||
139 |
1
1. updateArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateArticle → KILLED |
return updatedArticles; |
140 | } | |
141 | ||
142 | @Operation(summary = "Delete an article by id") | |
143 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
144 | @DeleteMapping("") | |
145 | public String deleteArticle( | |
146 | @Parameter(name = "id", description = "ID of article to delete") @RequestParam Long id) { | |
147 | ||
148 | Articles article = articlesRepository.findById(id) | |
149 |
1
1. lambda$deleteArticle$2 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$deleteArticle$2 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Articles.class, id)); |
150 | ||
151 |
1
1. deleteArticle : removed call to edu/ucsb/cs156/example/repositories/ArticlesRepository::delete → KILLED |
articlesRepository.delete(article); |
152 |
1
1. deleteArticle : replaced return value with "" for edu/ucsb/cs156/example/controllers/ArticlesController::deleteArticle → KILLED |
return String.format("record %d deleted", id); |
153 | } | |
154 | ||
155 | ||
156 | } | |
Mutations | ||
53 |
1.1 |
|
84 |
1.1 |
|
85 |
1.1 |
|
86 |
1.1 |
|
87 |
1.1 |
|
88 |
1.1 |
|
92 |
1.1 |
|
109 |
1.1 |
|
110 |
1.1 |
|
129 |
1.1 |
|
131 |
1.1 |
|
132 |
1.1 |
|
133 |
1.1 |
|
134 |
1.1 |
|
135 |
1.1 |
|
139 |
1.1 |
|
149 |
1.1 |
|
151 |
1.1 |
|
152 |
1.1 |