| 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 |
1
1. allArticles : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/ArticlesController::allArticles → KILLED |
return articlesRepository.findAll(); |
| 43 | } | |
| 44 | ||
| 45 | /** | |
| 46 | * Get a single article by id. | |
| 47 | * | |
| 48 | * @param id id of the article to get | |
| 49 | * @return the requested article | |
| 50 | */ | |
| 51 | @Operation(summary = "Get a single article") | |
| 52 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 53 | @GetMapping("") | |
| 54 | public Articles getById( | |
| 55 | @Parameter(name = "id", description = "ID of the article to retrieve") @RequestParam | |
| 56 | Long id) { | |
| 57 | Articles article = | |
| 58 | articlesRepository | |
| 59 | .findById(id) | |
| 60 |
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)); |
| 61 | ||
| 62 |
1
1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::getById → KILLED |
return article; |
| 63 | } | |
| 64 | ||
| 65 | /** | |
| 66 | * Create a new article. | |
| 67 | * | |
| 68 | * @param title article title | |
| 69 | * @param url article URL | |
| 70 | * @param explanation short explanation of the article | |
| 71 | * @param email submitter email | |
| 72 | * @param dateAdded the timestamp when the article was added | |
| 73 | * @return the saved article | |
| 74 | */ | |
| 75 | @Operation(summary = "Create a new article") | |
| 76 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 77 | @PostMapping("/post") | |
| 78 | public Articles postArticles( | |
| 79 | @Parameter(name = "title") @RequestParam String title, | |
| 80 | @Parameter(name = "url") @RequestParam String url, | |
| 81 | @Parameter(name = "explanation") @RequestParam String explanation, | |
| 82 | @Parameter(name = "email") @RequestParam String email, | |
| 83 | @Parameter( | |
| 84 | name = "dateAdded", | |
| 85 | description = | |
| 86 | "timestamp in ISO format, e.g. YYYY-mm-ddTHH:MM:SS; see" | |
| 87 | + " https://en.wikipedia.org/wiki/ISO_8601") | |
| 88 | @RequestParam("dateAdded") | |
| 89 | @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) | |
| 90 | LocalDateTime dateAdded) { | |
| 91 | ||
| 92 | Articles article = new Articles(); | |
| 93 |
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED |
article.setTitle(title); |
| 94 |
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED |
article.setUrl(url); |
| 95 |
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED |
article.setExplanation(explanation); |
| 96 |
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED |
article.setEmail(email); |
| 97 |
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED |
article.setDateAdded(dateAdded); |
| 98 | ||
| 99 |
1
1. postArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postArticles → KILLED |
return articlesRepository.save(article); |
| 100 | } | |
| 101 | ||
| 102 | /** | |
| 103 | * Delete an article by id. | |
| 104 | * | |
| 105 | * @param id id of the article to delete | |
| 106 | * @return a message indicating the article was deleted | |
| 107 | */ | |
| 108 | @Operation(summary = "Delete a single article") | |
| 109 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 110 | @DeleteMapping("") | |
| 111 | public Object deleteArticle( | |
| 112 | @Parameter(name = "id", description = "ID of the article to delete") @RequestParam Long id) { | |
| 113 | Articles article = | |
| 114 | articlesRepository | |
| 115 | .findById(id) | |
| 116 |
1
1. lambda$deleteArticle$1 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$deleteArticle$1 → 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 | ||
| 122 | /** | |
| 123 | * Update an existing article by id. | |
| 124 | * | |
| 125 | * @param id id of the article to update | |
| 126 | * @param incoming JSON body with updated article field values | |
| 127 | * @return the updated article | |
| 128 | */ | |
| 129 | @Operation(summary = "Update a single article") | |
| 130 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 131 | @PutMapping("") | |
| 132 | public Articles updateArticles( | |
| 133 | @Parameter(name = "id", description = "ID of the article to update") @RequestParam Long id, | |
| 134 | @RequestBody @Valid Articles incoming) { | |
| 135 | ||
| 136 | Articles article = | |
| 137 | articlesRepository | |
| 138 | .findById(id) | |
| 139 |
1
1. lambda$updateArticles$2 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$updateArticles$2 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Articles.class, id)); |
| 140 | ||
| 141 |
1
1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED |
article.setTitle(incoming.getTitle()); |
| 142 |
1
1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED |
article.setUrl(incoming.getUrl()); |
| 143 |
1
1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED |
article.setExplanation(incoming.getExplanation()); |
| 144 |
1
1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED |
article.setEmail(incoming.getEmail()); |
| 145 |
1
1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED |
article.setDateAdded(incoming.getDateAdded()); |
| 146 | ||
| 147 | articlesRepository.save(article); | |
| 148 | ||
| 149 |
1
1. updateArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateArticles → KILLED |
return article; |
| 150 | } | |
| 151 | } | |
Mutations | ||
| 42 |
1.1 |
|
| 60 |
1.1 |
|
| 62 |
1.1 |
|
| 93 |
1.1 |
|
| 94 |
1.1 |
|
| 95 |
1.1 |
|
| 96 |
1.1 |
|
| 97 |
1.1 |
|
| 99 |
1.1 |
|
| 116 |
1.1 |
|
| 118 |
1.1 |
|
| 119 |
1.1 |
|
| 139 |
1.1 |
|
| 141 |
1.1 |
|
| 142 |
1.1 |
|
| 143 |
1.1 |
|
| 144 |
1.1 |
|
| 145 |
1.1 |
|
| 149 |
1.1 |