| 1 | package edu.ucsb.cs156.example.controllers; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 4 | import edu.ucsb.cs156.example.entities.Article; | |
| 5 | import edu.ucsb.cs156.example.errors.EntityNotFoundException; | |
| 6 | import edu.ucsb.cs156.example.repositories.ArticleRepository; | |
| 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 jakarta.validation.Valid; | |
| 11 | import java.time.LocalDateTime; | |
| 12 | import lombok.extern.slf4j.Slf4j; | |
| 13 | import org.springframework.beans.factory.annotation.Autowired; | |
| 14 | import org.springframework.format.annotation.DateTimeFormat; | |
| 15 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 16 | import org.springframework.web.bind.annotation.DeleteMapping; | |
| 17 | import org.springframework.web.bind.annotation.GetMapping; | |
| 18 | import org.springframework.web.bind.annotation.PostMapping; | |
| 19 | import org.springframework.web.bind.annotation.PutMapping; | |
| 20 | import org.springframework.web.bind.annotation.RequestBody; | |
| 21 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 22 | import org.springframework.web.bind.annotation.RequestParam; | |
| 23 | import org.springframework.web.bind.annotation.RestController; | |
| 24 | ||
| 25 | /** This is a REST controller for Articles */ | |
| 26 | @Tag(name = "Articles") | |
| 27 | @RequestMapping("/api/articles") | |
| 28 | @RestController | |
| 29 | @Slf4j | |
| 30 | public class ArticlesController extends ApiController { | |
| 31 | ||
| 32 | @Autowired ArticleRepository articleRepository; | |
| 33 | ||
| 34 | /** | |
| 35 | * List all Articles | |
| 36 | * | |
| 37 | * @return an iterable of Article | |
| 38 | */ | |
| 39 | @Operation(summary = "List all articles") | |
| 40 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 41 | @GetMapping("/all") | |
| 42 | public Iterable<Article> allArticles() { | |
| 43 | Iterable<Article> articles = articleRepository.findAll(); | |
| 44 |
1
1. allArticles : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/ArticlesController::allArticles → KILLED |
return articles; |
| 45 | } | |
| 46 | ||
| 47 | /** | |
| 48 | * Create a new article | |
| 49 | * | |
| 50 | * @param title the article title | |
| 51 | * @param url the article url | |
| 52 | * @param explanation explanation of the article | |
| 53 | * @param email email of the article | |
| 54 | * @param dateAdded date of the article | |
| 55 | * @return the saved article | |
| 56 | */ | |
| 57 | @Operation(summary = "Create a new article") | |
| 58 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 59 | @PostMapping("/post") | |
| 60 | public Article postArticle( | |
| 61 | @Parameter(name = "title") @RequestParam String title, | |
| 62 | @Parameter(name = "url") @RequestParam String url, | |
| 63 | @Parameter(name = "explanation") @RequestParam String explanation, | |
| 64 | @Parameter(name = "email") @RequestParam String email, | |
| 65 | @Parameter( | |
| 66 | name = "dateAdded", | |
| 67 | description = | |
| 68 | "date (in iso format, e.g. YYYY-mm-ddTHH:MM:SS; see https://en.wikipedia.org/wiki/ISO_8601)") | |
| 69 | @RequestParam("dateAdded") | |
| 70 | @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) | |
| 71 | LocalDateTime dateAdded) | |
| 72 | throws JsonProcessingException { | |
| 73 | log.info("dateAdded={}", dateAdded); | |
| 74 | ||
| 75 | Article article = new Article(); | |
| 76 |
1
1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setTitle → KILLED |
article.setTitle(title); |
| 77 |
1
1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setUrl → KILLED |
article.setUrl(url); |
| 78 |
1
1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setExplanation → KILLED |
article.setExplanation(explanation); |
| 79 |
1
1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setEmail → KILLED |
article.setEmail(email); |
| 80 |
1
1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setDateAdded → KILLED |
article.setDateAdded(dateAdded); |
| 81 | ||
| 82 | Article savedArticle = articleRepository.save(article); | |
| 83 | ||
| 84 |
1
1. postArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postArticle → KILLED |
return savedArticle; |
| 85 | } | |
| 86 | ||
| 87 | /** | |
| 88 | * Get a single article by id | |
| 89 | * | |
| 90 | * @param id the id of the article | |
| 91 | * @return an Article | |
| 92 | */ | |
| 93 | @Operation(summary = "Get a single article") | |
| 94 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 95 | @GetMapping("") | |
| 96 | public Article getById(@Parameter(name = "id") @RequestParam Long id) { | |
| 97 | Article article = | |
| 98 | articleRepository | |
| 99 | .findById(id) | |
| 100 |
1
1. lambda$getById$0 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$getById$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Article.class, id)); |
| 101 | ||
| 102 |
1
1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::getById → KILLED |
return article; |
| 103 | } | |
| 104 | ||
| 105 | /** | |
| 106 | * Update a single article | |
| 107 | * | |
| 108 | * @param id id of the article to update | |
| 109 | * @param incoming the new article | |
| 110 | * @return the updated article object | |
| 111 | */ | |
| 112 | @Operation(summary = "Update a single article") | |
| 113 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 114 | @PutMapping("") | |
| 115 | public Article updateArticle( | |
| 116 | @Parameter(name = "id") @RequestParam Long id, @RequestBody @Valid Article incoming) { | |
| 117 | ||
| 118 | Article article = | |
| 119 | articleRepository | |
| 120 | .findById(id) | |
| 121 |
1
1. lambda$updateArticle$1 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$updateArticle$1 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Article.class, id)); |
| 122 | ||
| 123 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setTitle → KILLED |
article.setTitle(incoming.getTitle()); |
| 124 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setUrl → KILLED |
article.setUrl(incoming.getUrl()); |
| 125 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setExplanation → KILLED |
article.setExplanation(incoming.getExplanation()); |
| 126 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setEmail → KILLED |
article.setEmail(incoming.getEmail()); |
| 127 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setDateAdded → KILLED |
article.setDateAdded(incoming.getDateAdded()); |
| 128 | ||
| 129 | articleRepository.save(article); | |
| 130 | ||
| 131 |
1
1. updateArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateArticle → KILLED |
return article; |
| 132 | } | |
| 133 | ||
| 134 | /** | |
| 135 | * Delete an Article | |
| 136 | * | |
| 137 | * @param id the id of the article to delete | |
| 138 | * @return a message indicating the article was deleted | |
| 139 | */ | |
| 140 | @Operation(summary = "Delete an Article") | |
| 141 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 142 | @DeleteMapping("") | |
| 143 | public Object deleteArticle(@Parameter(name = "id") @RequestParam Long id) { | |
| 144 | Article article = | |
| 145 | articleRepository | |
| 146 | .findById(id) | |
| 147 |
1
1. lambda$deleteArticle$2 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$deleteArticle$2 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Article.class, id)); |
| 148 | ||
| 149 |
1
1. deleteArticle : removed call to edu/ucsb/cs156/example/repositories/ArticleRepository::delete → KILLED |
articleRepository.delete(article); |
| 150 |
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)); |
| 151 | } | |
| 152 | } | |
Mutations | ||
| 44 |
1.1 |
|
| 76 |
1.1 |
|
| 77 |
1.1 |
|
| 78 |
1.1 |
|
| 79 |
1.1 |
|
| 80 |
1.1 |
|
| 84 |
1.1 |
|
| 100 |
1.1 |
|
| 102 |
1.1 |
|
| 121 |
1.1 |
|
| 123 |
1.1 |
|
| 124 |
1.1 |
|
| 125 |
1.1 |
|
| 126 |
1.1 |
|
| 127 |
1.1 |
|
| 131 |
1.1 |
|
| 147 |
1.1 |
|
| 149 |
1.1 |
|
| 150 |
1.1 |