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 | * This is a REST controller for Articles | |
32 | */ | |
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 author | |
63 | * @param dateAdded the date the article was added | |
64 | * @return the saved article | |
65 | */ | |
66 | @Operation(summary= "Create a new article") | |
67 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
68 | @PostMapping("/post") | |
69 | public Articles postArticles( | |
70 | @Parameter(name="title") @RequestParam String title, | |
71 | @Parameter(name="url") @RequestParam String url, | |
72 | @Parameter(name="explanation") @RequestParam String explanation, | |
73 | @Parameter(name="email") @RequestParam String email, | |
74 | @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) | |
75 | throws JsonProcessingException { | |
76 | ||
77 | // For an explanation of @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) | |
78 | // See: https://www.baeldung.com/spring-date-parameters | |
79 | ||
80 | log.info("dateAdded={}", dateAdded); | |
81 | ||
82 | Articles article = new Articles(); | |
83 |
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED |
article.setTitle(title); |
84 |
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED |
article.setUrl(url); |
85 |
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED |
article.setExplanation(explanation); |
86 |
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED |
article.setEmail(email); |
87 |
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED |
article.setDateAdded(dateAdded); |
88 | ||
89 | Articles savedArticle = articlesRepository.save(article); | |
90 | ||
91 |
1
1. postArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postArticles → KILLED |
return savedArticle; |
92 | } | |
93 | ||
94 | /** | |
95 | * Get a single article by id | |
96 | * | |
97 | * @param id the id of the article | |
98 | * @return a Articles | |
99 | */ | |
100 | @Operation(summary= "Get a single article") | |
101 | @PreAuthorize("hasRole('ROLE_USER')") | |
102 | @GetMapping("") | |
103 | public Articles getById( | |
104 | @Parameter(name="id") @RequestParam Long id) { | |
105 | Articles article = articlesRepository.findById(id) | |
106 |
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)); |
107 | ||
108 |
1
1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::getById → KILLED |
return article; |
109 | } | |
110 | ||
111 | /** | |
112 | * Update a single article | |
113 | * | |
114 | * @param id id of the article to update | |
115 | * @param incoming the new article | |
116 | * @return the updated article object | |
117 | */ | |
118 | @Operation(summary= "Update a single article") | |
119 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
120 | @PutMapping("") | |
121 | public Articles updateArticles( | |
122 | @Parameter(name="id") @RequestParam Long id, | |
123 | @RequestBody @Valid Articles incoming) { | |
124 | ||
125 | Articles article = articlesRepository.findById(id) | |
126 |
1
1. lambda$updateArticles$1 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$updateArticles$1 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Articles.class, id)); |
127 | ||
128 |
1
1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED |
article.setTitle(incoming.getTitle()); |
129 |
1
1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED |
article.setUrl(incoming.getUrl()); |
130 |
1
1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED |
article.setExplanation(incoming.getExplanation()); |
131 |
1
1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED |
article.setEmail(incoming.getEmail()); |
132 |
1
1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED |
article.setDateAdded(incoming.getDateAdded()); |
133 | ||
134 | articlesRepository.save(article); | |
135 | ||
136 |
1
1. updateArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateArticles → KILLED |
return article; |
137 | } | |
138 | ||
139 | /** | |
140 | * Delete a Articles | |
141 | * | |
142 | * @param id the id of the article to delete | |
143 | * @return a message indicating the article was deleted | |
144 | */ | |
145 | @Operation(summary= "Delete an article") | |
146 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
147 | @DeleteMapping("") | |
148 | public Object deleteArticles( | |
149 | @Parameter(name="id") @RequestParam Long id) { | |
150 | Articles article = articlesRepository.findById(id) | |
151 |
1
1. lambda$deleteArticles$2 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$deleteArticles$2 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Articles.class, id)); |
152 | ||
153 |
1
1. deleteArticles : removed call to edu/ucsb/cs156/example/repositories/ArticlesRepository::delete → KILLED |
articlesRepository.delete(article); |
154 |
1
1. deleteArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::deleteArticles → KILLED |
return genericMessage("Articles with id %s deleted".formatted(id)); |
155 | } | |
156 | } | |
Mutations | ||
53 |
1.1 |
|
83 |
1.1 |
|
84 |
1.1 |
|
85 |
1.1 |
|
86 |
1.1 |
|
87 |
1.1 |
|
91 |
1.1 |
|
106 |
1.1 |
|
108 |
1.1 |
|
126 |
1.1 |
|
128 |
1.1 |
|
129 |
1.1 |
|
130 |
1.1 |
|
131 |
1.1 |
|
132 |
1.1 |
|
136 |
1.1 |
|
151 |
1.1 |
|
153 |
1.1 |
|
154 |
1.1 |