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 | * This is a REST controller for Articles | |
31 | */ | |
32 | ||
33 | @Tag(name = "Articles") | |
34 | @RequestMapping("/api/articles") | |
35 | @RestController | |
36 | @Slf4j | |
37 | public class ArticlesController extends ApiController { | |
38 | ||
39 | @Autowired | |
40 | ArticlesRepository articlesRepository; | |
41 | ||
42 | /** | |
43 | * This method returns a list of all articles. | |
44 | * @return a list of all articles | |
45 | */ | |
46 | @Operation(summary = "List all articles") | |
47 | @PreAuthorize("hasRole('ROLE_USER')") | |
48 | @GetMapping("/all") | |
49 | public Iterable<Articles> allArticles() { | |
50 | Iterable<Articles> articles = articlesRepository.findAll(); | |
51 |
1
1. allArticles : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/ArticlesController::allArticles → KILLED |
return articles; |
52 | } | |
53 | ||
54 | /** | |
55 | * Create a new article | |
56 | * | |
57 | * @param title the title of the article | |
58 | * @param url the URL of the article | |
59 | * @param explanation a brief explanation of the article | |
60 | * @param email the email of the person who submitted the article | |
61 | * @param dateAdded the date the article was added | |
62 | * @return the saved Articles object | |
63 | */ | |
64 | @Operation(summary = "Create a new article") | |
65 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
66 | @PostMapping("/post") | |
67 | public Articles postArticle( | |
68 | @Parameter(name = "title") @RequestParam String title, | |
69 | @Parameter(name = "url") @RequestParam String url, | |
70 | @Parameter(name = "explanation") @RequestParam String explanation, | |
71 | @Parameter(name = "email") @RequestParam String email, | |
72 | @Parameter(name = "dateAdded", description="date (in iso format, e.g. YYYY-mm-ddTHH:MM:SS; see https://en.wikipedia.org/wiki/ISO_8601)") @RequestParam LocalDateTime dateAdded) { | |
73 | Articles article = new Articles(); | |
74 | ||
75 |
1
1. postArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED |
article.setTitle(title); |
76 |
1
1. postArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED |
article.setUrl(url); |
77 |
1
1. postArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED |
article.setExplanation(explanation); |
78 |
1
1. postArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED |
article.setEmail(email); |
79 |
1
1. postArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED |
article.setDateAdded(dateAdded); |
80 | ||
81 | Articles savedArticle = articlesRepository.save(article); | |
82 |
1
1. postArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postArticle → KILLED |
return savedArticle; |
83 | } | |
84 | ||
85 | /** | |
86 | * This method returns a single article. | |
87 | * @param id id of the article to get | |
88 | * @return a single article | |
89 | */ | |
90 | @Operation(summary = "Get a single article") | |
91 | @PreAuthorize("hasRole('ROLE_USER')") | |
92 | @GetMapping("") | |
93 | public Articles getById( | |
94 | @Parameter(name = "id") @RequestParam Long id) { | |
95 | Articles article = articlesRepository.findById(id) | |
96 |
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)); |
97 | ||
98 |
1
1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::getById → KILLED |
return article; |
99 | } | |
100 | ||
101 | /** | |
102 | * Update a single article | |
103 | * | |
104 | * @param id id of the article to update | |
105 | * @param incoming the new article | |
106 | * @return the updated article object | |
107 | */ | |
108 | @Operation(summary= "Update a single article") | |
109 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
110 | @PutMapping("") | |
111 | public Articles updateArticle( | |
112 | @Parameter(name="id") @RequestParam Long id, | |
113 | @RequestBody @Valid Articles incoming) { | |
114 | ||
115 | Articles article = articlesRepository.findById(id) | |
116 |
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)); |
117 | ||
118 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED |
article.setTitle(incoming.getTitle()); |
119 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED |
article.setUrl(incoming.getUrl()); |
120 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED |
article.setExplanation(incoming.getExplanation()); |
121 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED |
article.setEmail(incoming.getEmail()); |
122 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED |
article.setDateAdded(incoming.getDateAdded()); |
123 | ||
124 | articlesRepository.save(article); | |
125 | ||
126 |
1
1. updateArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateArticle → KILLED |
return article; |
127 | } | |
128 | ||
129 | /** | |
130 | * Delete an Article | |
131 | * | |
132 | * @param id the id of the article to delete | |
133 | * @return a message indicating the article was deleted | |
134 | */ | |
135 | @Operation(summary= "Delete an Article") | |
136 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
137 | @DeleteMapping("") | |
138 | public Object deleteArticle( | |
139 | @Parameter(name="id") @RequestParam Long id) { | |
140 | Articles article = articlesRepository.findById(id) | |
141 |
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)); |
142 | ||
143 |
1
1. deleteArticle : removed call to edu/ucsb/cs156/example/repositories/ArticlesRepository::delete → KILLED |
articlesRepository.delete(article); |
144 |
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)); |
145 | } | |
146 | } | |
Mutations | ||
51 |
1.1 |
|
75 |
1.1 |
|
76 |
1.1 |
|
77 |
1.1 |
|
78 |
1.1 |
|
79 |
1.1 |
|
82 |
1.1 |
|
96 |
1.1 |
|
98 |
1.1 |
|
116 |
1.1 |
|
118 |
1.1 |
|
119 |
1.1 |
|
120 |
1.1 |
|
121 |
1.1 |
|
122 |
1.1 |
|
126 |
1.1 |
|
141 |
1.1 |
|
143 |
1.1 |
|
144 |
1.1 |