| 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 | import io.swagger.v3.oas.annotations.Operation; | |
| 7 | import io.swagger.v3.oas.annotations.Parameter; | |
| 8 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 9 | import jakarta.validation.Valid; | |
| 10 | import java.time.LocalDateTime; | |
| 11 | import lombok.extern.slf4j.Slf4j; | |
| 12 | import org.springframework.beans.factory.annotation.Autowired; | |
| 13 | import org.springframework.format.annotation.DateTimeFormat; | |
| 14 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 15 | import org.springframework.web.bind.annotation.DeleteMapping; | |
| 16 | import org.springframework.web.bind.annotation.GetMapping; | |
| 17 | import org.springframework.web.bind.annotation.PostMapping; | |
| 18 | import org.springframework.web.bind.annotation.PutMapping; | |
| 19 | import org.springframework.web.bind.annotation.RequestBody; | |
| 20 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 21 | import org.springframework.web.bind.annotation.RequestParam; | |
| 22 | import org.springframework.web.bind.annotation.RestController; | |
| 23 | ||
| 24 | /** This is a REST controller for Articles */ | |
| 25 | @Tag(name = "Articles") | |
| 26 | @RequestMapping("/api/articles") | |
| 27 | @RestController | |
| 28 | @Slf4j | |
| 29 | public class ArticlesController extends ApiController { | |
| 30 | ||
| 31 | @Autowired ArticlesRepository articlesRepository; | |
| 32 | ||
| 33 | /** | |
| 34 | * List all articles | |
| 35 | * | |
| 36 | * @return an iterable of Articles | |
| 37 | */ | |
| 38 | @Operation(summary = "List all articles") | |
| 39 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 40 | @GetMapping("/all") | |
| 41 | public Iterable<Articles> allArticles() { | |
| 42 | Iterable<Articles> articles = articlesRepository.findAll(); | |
| 43 |
1
1. allArticles : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/ArticlesController::allArticles → KILLED |
return articles; |
| 44 | } | |
| 45 | ||
| 46 | /** | |
| 47 | * Get a single article by id | |
| 48 | * | |
| 49 | * @param id the id of the article | |
| 50 | * @return an Articles | |
| 51 | */ | |
| 52 | @Operation(summary = "Get a single article") | |
| 53 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 54 | @GetMapping("") | |
| 55 | public Articles getById( | |
| 56 | @Parameter(name = "id", description = "id of the article", example = "1") @RequestParam | |
| 57 | Long id) { | |
| 58 | Articles article = | |
| 59 | articlesRepository | |
| 60 | .findById(id) | |
| 61 |
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)); |
| 62 | ||
| 63 |
1
1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::getById → KILLED |
return article; |
| 64 | } | |
| 65 | ||
| 66 | /** | |
| 67 | * Create a new article | |
| 68 | * | |
| 69 | * @param title the title of the article | |
| 70 | * @param url the url of the article | |
| 71 | * @param explanation why the article is useful | |
| 72 | * @param email the email of the person that submitted the article | |
| 73 | * @param dateAdded the date and time the article was added | |
| 74 | * @return the saved Articles | |
| 75 | */ | |
| 76 | @Operation(summary = "Create a new article") | |
| 77 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 78 | @PostMapping("/post") | |
| 79 | public Articles postArticles( | |
| 80 | @Parameter( | |
| 81 | name = "title", | |
| 82 | description = "title of the article", | |
| 83 | example = "Using testing-playground with React Testing Library") | |
| 84 | @RequestParam | |
| 85 | String title, | |
| 86 | @Parameter( | |
| 87 | name = "url", | |
| 88 | description = "url of the article", | |
| 89 | example = | |
| 90 | "https://dev.to/katieraby/using-testing-playground-with-react-testing-library-26j7") | |
| 91 | @RequestParam | |
| 92 | String url, | |
| 93 | @Parameter( | |
| 94 | name = "explanation", | |
| 95 | description = "why the article is useful", | |
| 96 | example = "Helpful when we get to front end development") | |
| 97 | @RequestParam | |
| 98 | String explanation, | |
| 99 | @Parameter( | |
| 100 | name = "email", | |
| 101 | description = "email of the person who submitted the article", | |
| 102 | example = "phtcon@ucsb.edu") | |
| 103 | @RequestParam | |
| 104 | String email, | |
| 105 | @Parameter( | |
| 106 | name = "dateAdded", | |
| 107 | description = | |
| 108 | "date and time the article was added (in iso format, e.g. YYYY-mm-ddTHH:MM:SS; see https://en.wikipedia.org/wiki/ISO_8601)", | |
| 109 | example = "2022-04-20T00:00:00") | |
| 110 | @RequestParam("dateAdded") | |
| 111 | @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) | |
| 112 | LocalDateTime dateAdded) { | |
| 113 | ||
| 114 | Articles article = new Articles(); | |
| 115 |
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED |
article.setTitle(title); |
| 116 |
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED |
article.setUrl(url); |
| 117 |
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED |
article.setExplanation(explanation); |
| 118 |
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED |
article.setEmail(email); |
| 119 |
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED |
article.setDateAdded(dateAdded); |
| 120 | ||
| 121 | Articles savedArticle = articlesRepository.save(article); | |
| 122 | ||
| 123 |
1
1. postArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postArticles → KILLED |
return savedArticle; |
| 124 | } | |
| 125 | ||
| 126 | /** | |
| 127 | * Update a single article | |
| 128 | * | |
| 129 | * @param id id of the article to update | |
| 130 | * @param incoming the new article | |
| 131 | * @return the updated article object | |
| 132 | */ | |
| 133 | @Operation(summary = "Update a single article") | |
| 134 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 135 | @PutMapping("") | |
| 136 | public Articles updateArticles( | |
| 137 | @Parameter(name = "id", description = "id of the article to update", example = "1") | |
| 138 | @RequestParam | |
| 139 | Long id, | |
| 140 | @RequestBody @Valid Articles incoming) { | |
| 141 | ||
| 142 | Articles article = | |
| 143 | articlesRepository | |
| 144 | .findById(id) | |
| 145 |
1
1. lambda$updateArticles$0 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$updateArticles$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Articles.class, id)); |
| 146 | ||
| 147 |
1
1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED |
article.setTitle(incoming.getTitle()); |
| 148 |
1
1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED |
article.setUrl(incoming.getUrl()); |
| 149 |
1
1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED |
article.setExplanation(incoming.getExplanation()); |
| 150 |
1
1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED |
article.setEmail(incoming.getEmail()); |
| 151 |
1
1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED |
article.setDateAdded(incoming.getDateAdded()); |
| 152 | ||
| 153 | articlesRepository.save(article); | |
| 154 | ||
| 155 |
1
1. updateArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateArticles → KILLED |
return article; |
| 156 | } | |
| 157 | ||
| 158 | /** | |
| 159 | * Delete an article | |
| 160 | * | |
| 161 | * @param id the id of the article to delete | |
| 162 | * @return a message indicating the article was deleted | |
| 163 | */ | |
| 164 | @Operation( | |
| 165 | summary = "Delete an article", | |
| 166 | description = "Deletes the article with the given id; 404 if the id does not exist") | |
| 167 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 168 | @DeleteMapping("") | |
| 169 | public Object deleteArticles( | |
| 170 | @Parameter(name = "id", description = "id of the article to delete", example = "1") | |
| 171 | @RequestParam | |
| 172 | Long id) { | |
| 173 | Articles article = | |
| 174 | articlesRepository | |
| 175 | .findById(id) | |
| 176 |
1
1. lambda$deleteArticles$0 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$deleteArticles$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Articles.class, id)); |
| 177 | ||
| 178 |
1
1. deleteArticles : removed call to edu/ucsb/cs156/example/repositories/ArticlesRepository::delete → KILLED |
articlesRepository.delete(article); |
| 179 |
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)); |
| 180 | } | |
| 181 | } | |
Mutations | ||
| 43 |
1.1 |
|
| 61 |
1.1 |
|
| 63 |
1.1 |
|
| 115 |
1.1 |
|
| 116 |
1.1 |
|
| 117 |
1.1 |
|
| 118 |
1.1 |
|
| 119 |
1.1 |
|
| 123 |
1.1 |
|
| 145 |
1.1 |
|
| 147 |
1.1 |
|
| 148 |
1.1 |
|
| 149 |
1.1 |
|
| 150 |
1.1 |
|
| 151 |
1.1 |
|
| 155 |
1.1 |
|
| 176 |
1.1 |
|
| 178 |
1.1 |
|
| 179 |
1.1 |