| 1 | package edu.ucsb.cs156.example.controllers; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.example.entities.Article; | |
| 4 | import edu.ucsb.cs156.example.errors.EntityNotFoundException; | |
| 5 | import edu.ucsb.cs156.example.repositories.ArticleRepository; | |
| 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 | @Tag(name = "Articles") | |
| 31 | @RequestMapping("/api/articles") | |
| 32 | @RestController | |
| 33 | @Slf4j | |
| 34 | ||
| 35 | public class ArticlesController extends ApiController{ | |
| 36 |     @Autowired | |
| 37 |     ArticleRepository articleRepository; | |
| 38 | ||
| 39 |     @Operation(summary= "List all articles") | |
| 40 |     @PreAuthorize("hasRole('ROLE_USER')") | |
| 41 |     @GetMapping("/all") | |
| 42 |     public Iterable<Article> allArticle() { | |
| 43 |         Iterable<Article> article = articleRepository.findAll(); | |
| 44 | 
1
1. allArticle : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/ArticlesController::allArticle → KILLED | 
        return article; | 
| 45 |     } | |
| 46 | ||
| 47 |     @Operation(summary= "Create a new article") | |
| 48 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 49 |     @PostMapping("/post") | |
| 50 |     public Article postArticle( | |
| 51 |             @Parameter(name="title") @RequestParam String title, | |
| 52 |             @Parameter(name="url") @RequestParam String url, | |
| 53 |             @Parameter(name="explanation") @RequestParam String explanation, | |
| 54 |             @Parameter(name="email") @RequestParam String email, | |
| 55 |             @Parameter(name="dateAdded", description="date (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) | |
| 56 |             throws JsonProcessingException { | |
| 57 | ||
| 58 |         // For an explanation of @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) | |
| 59 |         // See: https://www.baeldung.com/spring-date-parameters | |
| 60 | ||
| 61 |         // log.info("localDateTime={}", dateAdded); | |
| 62 | ||
| 63 |         Article article = new Article(); | |
| 64 | 
1
1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setTitle → KILLED | 
        article.setTitle(title); | 
| 65 | 
1
1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setUrl → KILLED | 
        article.setUrl(url); | 
| 66 | 
1
1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setExplanation → KILLED | 
        article.setExplanation(explanation); | 
| 67 | 
1
1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setEmail → KILLED | 
        article.setEmail(email); | 
| 68 | 
1
1. postArticle : removed call to edu/ucsb/cs156/example/entities/Article::setDateAdded → KILLED | 
        article.setDateAdded(dateAdded); | 
| 69 | ||
| 70 |         Article savedArticle = articleRepository.save(article); | |
| 71 | ||
| 72 | 
1
1. postArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postArticle → KILLED | 
        return savedArticle; | 
| 73 |     } | |
| 74 | ||
| 75 |     /** | |
| 76 |      * Delete an Article | |
| 77 |      *  | |
| 78 |      * @param id the id of the article to delete | |
| 79 |      * @return a message indicating the article was deleted | |
| 80 |      */ | |
| 81 |     @Operation(summary= "Delete an Article") | |
| 82 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 83 |     @DeleteMapping("") | |
| 84 |     public Object deleteArticle( | |
| 85 |             @Parameter(name="id") @RequestParam String id) { | |
| 86 | 
1
1. lambda$deleteArticle$0 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$deleteArticle$0 → KILLED | 
        Article article = articleRepository.findById(id).orElseThrow(() -> new EntityNotFoundException(Article.class, id)); | 
| 87 | ||
| 88 | 
1
1. deleteArticle : removed call to edu/ucsb/cs156/example/repositories/ArticleRepository::delete → KILLED | 
        articleRepository.delete(article); | 
| 89 | 
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)); | 
| 90 |     } | |
| 91 | ||
| 92 |     /** | |
| 93 |      * Get a single article by id | |
| 94 |      *  | |
| 95 |      * @param id the id of the article | |
| 96 |      * @return an article | |
| 97 |      */ | |
| 98 |     @Operation(summary= "Get a single article") | |
| 99 |     @PreAuthorize("hasRole('ROLE_USER')") | |
| 100 |     @GetMapping("") | |
| 101 |     public Article getById( | |
| 102 |             @Parameter(name="id") @RequestParam String id) { | |
| 103 | 
1
1. lambda$getById$1 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$getById$1 → KILLED | 
        Article article = articleRepository.findById(id).orElseThrow(() -> new EntityNotFoundException(Article.class, id)); | 
| 104 | ||
| 105 | 
1
1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::getById → KILLED | 
        return article; | 
| 106 |     } | |
| 107 | ||
| 108 |     /** | |
| 109 |      * Update a single article | |
| 110 |      *  | |
| 111 |      * @param id       id of the article to update | |
| 112 |      * @param incoming the new article | |
| 113 |      * @return the updated article object | |
| 114 |      */ | |
| 115 |     @Operation(summary= "Update a single article") | |
| 116 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 117 |     @PutMapping("") | |
| 118 |     public Article updateArticle( | |
| 119 |             @Parameter(name="id") @RequestParam String id, | |
| 120 |             @RequestBody @Valid Article incoming) { | |
| 121 | ||
| 122 | 
1
1. lambda$updateArticle$2 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$updateArticle$2 → KILLED | 
        Article article = articleRepository.findById(id).orElseThrow(() -> new EntityNotFoundException(Article.class, id)); | 
| 123 | ||
| 124 | 
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setTitle → KILLED | 
        article.setTitle(incoming.getTitle()); | 
| 125 | 
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setUrl → KILLED | 
        article.setUrl(incoming.getUrl()); | 
| 126 | 
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setExplanation → KILLED | 
        article.setExplanation(incoming.getExplanation()); | 
| 127 | 
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setEmail → KILLED | 
        article.setEmail(incoming.getEmail()); | 
| 128 | 
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setDateAdded → KILLED | 
        article.setDateAdded(incoming.getDateAdded()); | 
| 129 | ||
| 130 |         articleRepository.save(article); | |
| 131 | ||
| 132 | 
1
1. updateArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateArticle → KILLED | 
        return article; | 
| 133 |     } | |
| 134 | } | |
Mutations | ||
| 44 | 
 
 1.1  | 
|
| 64 | 
 
 1.1  | 
|
| 65 | 
 
 1.1  | 
|
| 66 | 
 
 1.1  | 
|
| 67 | 
 
 1.1  | 
|
| 68 | 
 
 1.1  | 
|
| 72 | 
 
 1.1  | 
|
| 86 | 
 
 1.1  | 
|
| 88 | 
 
 1.1  | 
|
| 89 | 
 
 1.1  | 
|
| 103 | 
 
 1.1  | 
|
| 105 | 
 
 1.1  | 
|
| 122 | 
 
 1.1  | 
|
| 124 | 
 
 1.1  | 
|
| 125 | 
 
 1.1  | 
|
| 126 | 
 
 1.1  | 
|
| 127 | 
 
 1.1  | 
|
| 128 | 
 
 1.1  | 
|
| 132 | 
 
 1.1  |