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