1 | package edu.ucsb.cs156.example.controllers; | |
2 | ||
3 | import edu.ucsb.cs156.example.entities.Articles; | |
4 | import edu.ucsb.cs156.example.entities.UCSBDate; | |
5 | import edu.ucsb.cs156.example.errors.EntityNotFoundException; | |
6 | import edu.ucsb.cs156.example.repositories.ArticlesRepository; | |
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 | ||
31 | /** | |
32 | * This is a REST controller for Articles | |
33 | */ | |
34 | ||
35 | @Tag(name = "Articles", description = "Endpoints for managing articles") | |
36 | @RequestMapping("/api/articles") | |
37 | @RestController | |
38 | @Slf4j | |
39 | public class ArticlesController extends ApiController { | |
40 | ||
41 | @Autowired | |
42 | ArticlesRepository ArticlesRepository; | |
43 | ||
44 | /** | |
45 | * List all articles | |
46 | * | |
47 | * @return an iterable of Articles | |
48 | */ | |
49 | @Operation(summary= "List all articles", description = "Retrieve all articles from the database") | |
50 | @PreAuthorize("hasRole('ROLE_USER')") | |
51 | @GetMapping("/all") | |
52 | public Iterable<Articles> allArticles() { | |
53 | Iterable<Articles> articles = ArticlesRepository.findAll(); | |
54 |
1
1. allArticles : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/ArticlesController::allArticles → KILLED |
return articles; |
55 | } | |
56 | ||
57 | /** | |
58 | * Create a new article | |
59 | * | |
60 | * @param title the title of the article | |
61 | * @param url the URL of the article | |
62 | * @param explanation the explanation of the article | |
63 | * @param email the associated email | |
64 | * @param dateAdded the date the article was added | |
65 | * @return the saved Article | |
66 | */ | |
67 | @Operation(summary= "Create a new article", description = "Create a new article by providing the necessary details") | |
68 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
69 | @PostMapping("/post") | |
70 | public Articles postArticle( | |
71 | @Parameter(description = "Title of the article", example = "First Article") @RequestParam String title, | |
72 | @Parameter(description = "URL of the article", example = "https://first.com") @RequestParam String url, | |
73 | @Parameter(description = "Explanation of the article", example = "This is a sample explanation.") @RequestParam String explanation, | |
74 | @Parameter(description = "Email associated with the article", example = "first@example.com") @RequestParam String email, | |
75 | @Parameter(description = "Date when the article was added", example = "2024-10-23T00:00:00") | |
76 | @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime dateAdded) | |
77 | throws JsonProcessingException { | |
78 | ||
79 | log.info("dateAdded={}", dateAdded); | |
80 | ||
81 | Articles article = new Articles(); | |
82 |
1
1. postArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED |
article.setTitle(title); |
83 |
1
1. postArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED |
article.setUrl(url); |
84 |
1
1. postArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED |
article.setExplanation(explanation); |
85 |
1
1. postArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED |
article.setEmail(email); |
86 |
1
1. postArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED |
article.setDateAdded(dateAdded); |
87 | ||
88 | Articles savedArticle = ArticlesRepository.save(article); | |
89 | | |
90 |
1
1. postArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postArticle → KILLED |
return savedArticle; |
91 | } | |
92 | ||
93 | ||
94 | /** | |
95 | * Get a single article by id | |
96 | * | |
97 | * @param id the id of the article | |
98 | * @return an article | |
99 | */ | |
100 | @Operation(summary= "Get a single article", description = "Retrieve a single article by providing the ID") | |
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 object | |
116 | * @return the updated article object | |
117 | */ | |
118 | @Operation(summary= "Update a single article", description = "Update a single article by providing the ID and the new article data") | |
119 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
120 | @PutMapping("") | |
121 | public Articles updateArticle( | |
122 | @Parameter(name="id") @RequestParam Long id, | |
123 | @RequestBody @Valid Articles incoming) { | |
124 | ||
125 | Articles article = ArticlesRepository.findById(id) | |
126 |
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)); |
127 | ||
128 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED |
article.setTitle(incoming.getTitle()); |
129 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED |
article.setUrl(incoming.getUrl()); |
130 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED |
article.setExplanation(incoming.getExplanation()); |
131 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED |
article.setEmail(incoming.getEmail()); |
132 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED |
article.setDateAdded(incoming.getDateAdded()); |
133 | ||
134 | ArticlesRepository.save(article); | |
135 | ||
136 |
1
1. updateArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateArticle → KILLED |
return article; |
137 | } | |
138 | ||
139 | /** | |
140 | * Delete an article | |
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 a single article", description = "Delete a single article by providing the ID") | |
146 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
147 | @DeleteMapping("") | |
148 | public Object deleteArticle( | |
149 | @Parameter(name="id") @RequestParam Long id) { | |
150 | Articles article = ArticlesRepository.findById(id) | |
151 |
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)); |
152 | ||
153 |
1
1. deleteArticle : removed call to edu/ucsb/cs156/example/repositories/ArticlesRepository::delete → KILLED |
ArticlesRepository.delete(article); |
154 |
1
1. deleteArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::deleteArticle → KILLED |
return genericMessage("Article with id %s deleted".formatted(id)); |
155 | } | |
156 | ||
157 | } | |
Mutations | ||
54 |
1.1 |
|
82 |
1.1 |
|
83 |
1.1 |
|
84 |
1.1 |
|
85 |
1.1 |
|
86 |
1.1 |
|
90 |
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 |