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