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