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 javax.validation.Valid; | |
28 | ||
29 | import java.time.LocalDateTime; | |
30 | ||
31 | @Tag(name = "Articles") | |
32 | @RequestMapping("/api/articles") | |
33 | @RestController | |
34 | @Slf4j | |
35 | public class ArticlesController extends ApiController{ | |
36 | @Autowired | |
37 | ArticlesRepository articlesRepository; | |
38 | ||
39 | //Get all records in the table and return as a JSON array | |
40 | //Use the data in the input parameters to create a new row in the table and return the data as JSON | |
41 | | |
42 | @Operation(summary= "List all articles") | |
43 | @PreAuthorize("hasRole('ROLE_USER')") | |
44 | @GetMapping("/all") | |
45 | public Iterable<Articles> allArticles() { | |
46 | Iterable<Articles> articles = articlesRepository.findAll(); | |
47 |
1
1. allArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::allArticles → KILLED |
return articles; |
48 | } | |
49 | ||
50 | @Operation(summary= "Create a new article") | |
51 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
52 | @PostMapping("/post") | |
53 | public Articles postArticles( | |
54 | @Parameter(name="title") @RequestParam String title, | |
55 | @Parameter(name="url") @RequestParam String url, | |
56 | @Parameter(name="explanation") @RequestParam String explanation, | |
57 | @Parameter(name="email") @RequestParam String email, | |
58 | @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) | |
59 | throws JsonProcessingException { | |
60 | ||
61 | // For an explanation of @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) | |
62 | // See: https://www.baeldung.com/spring-date-parameters | |
63 | ||
64 | log.info("dateAdded={}", dateAdded); | |
65 | ||
66 | Articles articles = new Articles(); | |
67 |
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED |
articles.setTitle(title); |
68 |
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED |
articles.setUrl(url); |
69 |
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED |
articles.setExplanation(explanation); |
70 |
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED |
articles.setEmail(email); |
71 |
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED |
articles.setDateAdded(dateAdded); |
72 | ||
73 | Articles savedArticles = articlesRepository.save(articles); | |
74 | ||
75 |
1
1. postArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postArticles → KILLED |
return savedArticles; |
76 | } | |
77 | ||
78 | @Operation(summary= "Get a single article") | |
79 | @PreAuthorize("hasRole('ROLE_USER')") | |
80 | @GetMapping("") | |
81 | public Articles getById( | |
82 | @Parameter(name="id") @RequestParam Long id) { | |
83 | Articles articles = articlesRepository.findById(id) | |
84 |
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)); |
85 | ||
86 |
1
1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::getById → KILLED |
return articles; |
87 | } | |
88 | ||
89 | @Operation(summary= "Update a single article") | |
90 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
91 | @PutMapping("") | |
92 | public Articles updateArticle( | |
93 | @Parameter(name="id") @RequestParam Long id, | |
94 | @RequestBody @Valid Articles incoming) { | |
95 | ||
96 | Articles article = articlesRepository.findById(id) | |
97 |
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)); |
98 | ||
99 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED |
article.setTitle(incoming.getTitle()); |
100 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED |
article.setUrl(incoming.getUrl()); |
101 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED |
article.setExplanation(incoming.getExplanation()); |
102 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED |
article.setEmail(incoming.getEmail()); |
103 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED |
article.setDateAdded(incoming.getDateAdded()); |
104 | ||
105 | articlesRepository.save(article); | |
106 | ||
107 |
1
1. updateArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateArticle → KILLED |
return article; |
108 | } | |
109 | | |
110 | @Operation(summary= "Delete an Article") | |
111 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
112 | @DeleteMapping("") | |
113 | public Object deleteArticle( | |
114 | @Parameter(name="id") @RequestParam Long id) { | |
115 | Articles article = articlesRepository.findById(id) | |
116 |
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)); |
117 | ||
118 |
1
1. deleteArticle : removed call to edu/ucsb/cs156/example/repositories/ArticlesRepository::delete → KILLED |
articlesRepository.delete(article); |
119 |
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)); |
120 | } | |
121 | } | |
Mutations | ||
47 |
1.1 |
|
67 |
1.1 |
|
68 |
1.1 |
|
69 |
1.1 |
|
70 |
1.1 |
|
71 |
1.1 |
|
75 |
1.1 |
|
84 |
1.1 |
|
86 |
1.1 |
|
97 |
1.1 |
|
99 |
1.1 |
|
100 |
1.1 |
|
101 |
1.1 |
|
102 |
1.1 |
|
103 |
1.1 |
|
107 |
1.1 |
|
116 |
1.1 |
|
118 |
1.1 |
|
119 |
1.1 |