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