| 1 | package edu.ucsb.cs156.example.controllers; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 4 | import edu.ucsb.cs156.example.entities.Articles; | |
| 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 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 | @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 | |
| 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(@Parameter(name = "id") @RequestParam Long id) { | |
| 56 | Articles article = | |
| 57 | articlesRepository | |
| 58 | .findById(id) | |
| 59 |
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)); |
| 60 | ||
| 61 |
1
1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::getById → KILLED |
return article; |
| 62 | } | |
| 63 | ||
| 64 | /** | |
| 65 | * Create a new article | |
| 66 | * | |
| 67 | * @param title the Title of the article | |
| 68 | * @param url the Url of the article | |
| 69 | * @param explanation the explanation of the article | |
| 70 | * @param email the email of the person who submitted | |
| 71 | * @param dateAdded the date added | |
| 72 | * @return the saved article | |
| 73 | */ | |
| 74 | @Operation(summary = "Create a article") | |
| 75 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 76 | @PostMapping("/post") | |
| 77 | public Articles postArticle( | |
| 78 | @Parameter(name = "title") @RequestParam String title, | |
| 79 | @Parameter(name = "url") @RequestParam String url, | |
| 80 | @Parameter(name = "explanation") @RequestParam String explanation, | |
| 81 | @Parameter(name = "email") @RequestParam String email, | |
| 82 | @Parameter( | |
| 83 | name = "dateAdded", | |
| 84 | description = | |
| 85 | "date (in iso format, e.g. YYYY-mm-ddTHH:MM:SS; see https://en.wikipedia.org/wiki/ISO_8601)") | |
| 86 | @RequestParam("dateAdded") | |
| 87 | @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) | |
| 88 | LocalDateTime dateAdded) | |
| 89 | throws JsonProcessingException { | |
| 90 | ||
| 91 | log.info( | |
| 92 | "postArticle: title={}, url={}, explanation={}, email={}, dateAdded={}", | |
| 93 | title, | |
| 94 | url, | |
| 95 | explanation, | |
| 96 | email, | |
| 97 | dateAdded); | |
| 98 | ||
| 99 | Articles article = new Articles(); | |
| 100 |
1
1. postArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED |
article.setTitle(title); |
| 101 |
1
1. postArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED |
article.setUrl(url); |
| 102 |
1
1. postArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED |
article.setExplanation(explanation); |
| 103 |
1
1. postArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED |
article.setEmail(email); |
| 104 |
1
1. postArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED |
article.setDateAdded(dateAdded); |
| 105 | ||
| 106 | Articles savedArticle = articlesRepository.save(article); | |
| 107 | ||
| 108 |
1
1. postArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postArticle → KILLED |
return savedArticle; |
| 109 | } | |
| 110 | ||
| 111 | /** | |
| 112 | * Update a single article | |
| 113 | * | |
| 114 | * @param id id of the article to update | |
| 115 | * @param incoming the new article data | |
| 116 | * @return the updated article object | |
| 117 | */ | |
| 118 | @Operation(summary = "Update a single article") | |
| 119 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 120 | @PutMapping("") | |
| 121 | public Articles updateArticle( | |
| 122 | @Parameter(name = "id") @RequestParam Long id, @RequestBody @Valid Articles incoming) { | |
| 123 | ||
| 124 | Articles article = | |
| 125 | articlesRepository | |
| 126 | .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(Articles.class, id)); |
| 128 | ||
| 129 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED |
article.setTitle(incoming.getTitle()); |
| 130 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED |
article.setUrl(incoming.getUrl()); |
| 131 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED |
article.setExplanation(incoming.getExplanation()); |
| 132 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED |
article.setEmail(incoming.getEmail()); |
| 133 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED |
article.setDateAdded(incoming.getDateAdded()); |
| 134 | ||
| 135 | articlesRepository.save(article); | |
| 136 | ||
| 137 |
1
1. updateArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateArticle → KILLED |
return article; |
| 138 | } | |
| 139 | ||
| 140 | /** | |
| 141 | * Delete an article | |
| 142 | * | |
| 143 | * @param id the id of the article to delete | |
| 144 | * @return a message indicating the article was deleted | |
| 145 | */ | |
| 146 | @Operation(summary = "Delete an article") | |
| 147 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 148 | @DeleteMapping("") | |
| 149 | public Object deleteArticle(@Parameter(name = "id") @RequestParam Long id) { | |
| 150 | Articles article = | |
| 151 | articlesRepository | |
| 152 | .findById(id) | |
| 153 |
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)); |
| 154 | ||
| 155 |
1
1. deleteArticle : removed call to edu/ucsb/cs156/example/repositories/ArticlesRepository::delete → KILLED |
articlesRepository.delete(article); |
| 156 |
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)); |
| 157 | } | |
| 158 | } | |
Mutations | ||
| 43 |
1.1 |
|
| 59 |
1.1 |
|
| 61 |
1.1 |
|
| 100 |
1.1 |
|
| 101 |
1.1 |
|
| 102 |
1.1 |
|
| 103 |
1.1 |
|
| 104 |
1.1 |
|
| 108 |
1.1 |
|
| 127 |
1.1 |
|
| 129 |
1.1 |
|
| 130 |
1.1 |
|
| 131 |
1.1 |
|
| 132 |
1.1 |
|
| 133 |
1.1 |
|
| 137 |
1.1 |
|
| 153 |
1.1 |
|
| 155 |
1.1 |
|
| 156 |
1.1 |