| 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 | ||
| 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 | ||
| 34 | @Tag(name = "Articles") | |
| 35 | @RequestMapping("/api/articles") | |
| 36 | @RestController | |
| 37 | @Slf4j | |
| 38 | ||
| 39 | public class ArticlesController extends ApiController { | |
| 40 | ||
| 41 |     @Autowired | |
| 42 |     ArticlesRepository articlesRepository; | |
| 43 | ||
| 44 | ||
| 45 |     /** | |
| 46 |      * List all Articles | |
| 47 |      *  | |
| 48 |      * @return an iterable of Articles | |
| 49 |      */ | |
| 50 |     @Operation(summary= "List all articles") | |
| 51 |     @PreAuthorize("hasRole('ROLE_USER')") | |
| 52 |     @GetMapping("/all") | |
| 53 |     public Iterable<Articles> allArticles() { | |
| 54 |         Iterable<Articles> articles = articlesRepository.findAll(); | |
| 55 | 
1
1. allArticles : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/ArticlesController::allArticles → KILLED | 
        return articles; | 
| 56 |     } | |
| 57 | ||
| 58 | ||
| 59 |     /** | |
| 60 |      * Create a new article | |
| 61 |      *  | |
| 62 |      * @param title  the article title | |
| 63 |      * @param url          the article url | |
| 64 |      * @param explanation the article explanation | |
| 65 |      * @param email  the email | |
| 66 |      * @param dateAdded  the date added | |
| 67 |      * @return the saved article | |
| 68 |      */ | |
| 69 |     @Operation(summary= "Create a new article") | |
| 70 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 71 |     @PostMapping("/post") | |
| 72 |     public Articles postArticle( | |
| 73 |             @Parameter(name="title") @RequestParam String title, | |
| 74 |             @Parameter(name="url") @RequestParam String url, | |
| 75 |             @Parameter(name="explanation") @RequestParam String explanation, | |
| 76 |             @Parameter(name="email") @RequestParam String email, | |
| 77 |             @Parameter(name="dateAdded") @RequestParam("dateAdded") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime dateAdded) | |
| 78 |             throws JsonProcessingException { | |
| 79 | ||
| 80 |         // For an explanation of @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) | |
| 81 |         // See: https://www.baeldung.com/spring-date-parameters | |
| 82 | ||
| 83 |         log.info("dateAdded={}", dateAdded); | |
| 84 | ||
| 85 |         Articles article = new Articles(); | |
| 86 | 
1
1. postArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED | 
        article.setTitle(title); | 
| 87 | 
1
1. postArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED | 
        article.setUrl(url); | 
| 88 | 
1
1. postArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED | 
        article.setExplanation(explanation); | 
| 89 | 
1
1. postArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED | 
        article.setEmail(email); | 
| 90 | 
1
1. postArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED | 
        article.setDateAdded(dateAdded); | 
| 91 | ||
| 92 |         Articles savedArticle = articlesRepository.save(article); | |
| 93 | ||
| 94 | 
1
1. postArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postArticle → KILLED | 
        return savedArticle; | 
| 95 |     } | |
| 96 | ||
| 97 | ||
| 98 |     /** | |
| 99 |      * Get a single article by id | |
| 100 |      *  | |
| 101 |      * @param id the id of the article | |
| 102 |      * @return a Article | |
| 103 |      */ | |
| 104 |     @Operation(summary= "Get a single article") | |
| 105 |     @PreAuthorize("hasRole('ROLE_USER')") | |
| 106 |     @GetMapping("") | |
| 107 |     public Articles getById( | |
| 108 |             @Parameter(name="id") @RequestParam Long id) { | |
| 109 |         Articles article = articlesRepository.findById(id) | |
| 110 | 
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)); | 
| 111 | ||
| 112 | 
1
1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::getById → KILLED | 
        return article; | 
| 113 |     } | |
| 114 | ||
| 115 | ||
| 116 | ||
| 117 |     /** | |
| 118 |      * Update a single article | |
| 119 |      *  | |
| 120 |      * @param id       id of the article to update | |
| 121 |      * @param incoming the new article | |
| 122 |      * @return the updated article object | |
| 123 |      */ | |
| 124 |     @Operation(summary= "Update a single article") | |
| 125 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 126 |     @PutMapping("") | |
| 127 |     public Articles updateArticle( | |
| 128 |             @Parameter(name="id") @RequestParam Long id, | |
| 129 |             @RequestBody @Valid Articles incoming) { | |
| 130 | ||
| 131 |         Articles article = articlesRepository.findById(id) | |
| 132 | 
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)); | 
| 133 | ||
| 134 | 
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED | 
        article.setTitle(incoming.getTitle()); | 
| 135 | 
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED | 
        article.setUrl(incoming.getUrl()); | 
| 136 | 
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED | 
        article.setExplanation(incoming.getExplanation()); | 
| 137 | 
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED | 
        article.setEmail(incoming.getEmail()); | 
| 138 | 
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED | 
        article.setDateAdded(incoming.getDateAdded()); | 
| 139 | ||
| 140 |         articlesRepository.save(article); | |
| 141 | ||
| 142 | 
1
1. updateArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateArticle → KILLED | 
        return article; | 
| 143 |     } | |
| 144 | ||
| 145 | ||
| 146 | ||
| 147 | ||
| 148 | ||
| 149 |     /** | |
| 150 |      * Delete a Article | |
| 151 |      *  | |
| 152 |      * @param id the id of the article to delete | |
| 153 |      * @return a message indicating the article was deleted | |
| 154 |      */ | |
| 155 |     @Operation(summary= "Delete a Article") | |
| 156 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 157 |     @DeleteMapping("") | |
| 158 |     public Object deleteArticle( | |
| 159 |             @Parameter(name="id") @RequestParam Long id) { | |
| 160 |         Articles article = articlesRepository.findById(id) | |
| 161 | 
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)); | 
| 162 | ||
| 163 | 
1
1. deleteArticle : removed call to edu/ucsb/cs156/example/repositories/ArticlesRepository::delete → KILLED | 
        articlesRepository.delete(article); | 
| 164 | 
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)); | 
| 165 |     } | |
| 166 | ||
| 167 | ||
| 168 | } | |
Mutations | ||
| 55 | 
 
 1.1  | 
|
| 86 | 
 
 1.1  | 
|
| 87 | 
 
 1.1  | 
|
| 88 | 
 
 1.1  | 
|
| 89 | 
 
 1.1  | 
|
| 90 | 
 
 1.1  | 
|
| 94 | 
 
 1.1  | 
|
| 110 | 
 
 1.1  | 
|
| 112 | 
 
 1.1  | 
|
| 132 | 
 
 1.1  | 
|
| 134 | 
 
 1.1  | 
|
| 135 | 
 
 1.1  | 
|
| 136 | 
 
 1.1  | 
|
| 137 | 
 
 1.1  | 
|
| 138 | 
 
 1.1  | 
|
| 142 | 
 
 1.1  | 
|
| 161 | 
 
 1.1  | 
|
| 163 | 
 
 1.1  | 
|
| 164 | 
 
 1.1  |