| 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 java.util.Map; | |
| 13 | import lombok.extern.slf4j.Slf4j; | |
| 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 | /** This is a REST controller for Articles */ | |
| 27 | @Tag(name = "Articles") | |
| 28 | @RequestMapping("/api/articles") | |
| 29 | @RestController | |
| 30 | @Slf4j | |
| 31 | public class ArticlesController extends ApiController { | |
| 32 | ||
| 33 | @Autowired ArticlesRepository articlesRepository; | |
| 34 | ||
| 35 | /** | |
| 36 | * List all Articles | |
| 37 | * | |
| 38 | * @return an iterable of Articles | |
| 39 | */ | |
| 40 | @Operation(summary = "List all articles") | |
| 41 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 42 | @GetMapping("/all") | |
| 43 | public Iterable<Articles> allArticles() { | |
| 44 |
1
1. allArticles : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/ArticlesController::allArticles → KILLED |
return articlesRepository.findAll(); |
| 45 | } | |
| 46 | ||
| 47 | /** | |
| 48 | * Create a new article | |
| 49 | * | |
| 50 | * @param title the title of the article | |
| 51 | * @param url the url of the article | |
| 52 | * @param explanation the explanation of the article | |
| 53 | * @param email the email of the user who added the article | |
| 54 | * @param dateAdded the date the article was added | |
| 55 | * @return the saved article | |
| 56 | */ | |
| 57 | @Operation(summary = "Create a new article") | |
| 58 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 59 | @PostMapping("/post") | |
| 60 | public Articles postArticles( | |
| 61 | @Parameter(name = "title") @RequestParam String title, | |
| 62 | @Parameter(name = "url") @RequestParam String url, | |
| 63 | @Parameter(name = "explanation") @RequestParam String explanation, | |
| 64 | @Parameter(name = "email") @RequestParam String email, | |
| 65 | @Parameter( | |
| 66 | name = "dateAdded", | |
| 67 | description = | |
| 68 | "date (in iso format, e.g. YYYY-mm-ddTHH:MM:SS; see https://en.wikipedia.org/wiki/ISO_8601)") | |
| 69 | @RequestParam("dateAdded") | |
| 70 | @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) | |
| 71 | LocalDateTime dateAdded) | |
| 72 | throws JsonProcessingException { | |
| 73 | ||
| 74 | // For an explanation of @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) | |
| 75 | // See: https://www.baeldung.com/spring-date-parameters | |
| 76 | log.info("dateAdded={}", dateAdded); | |
| 77 | ||
| 78 | Articles article = new Articles(); | |
| 79 |
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED |
article.setTitle(title); |
| 80 |
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED |
article.setUrl(url); |
| 81 |
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED |
article.setExplanation(explanation); |
| 82 |
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED |
article.setEmail(email); |
| 83 |
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED |
article.setDateAdded(dateAdded); |
| 84 | ||
| 85 |
1
1. postArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postArticles → KILLED |
return articlesRepository.save(article); |
| 86 | } | |
| 87 | ||
| 88 | @Operation(summary = "Get a single article") | |
| 89 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 90 | @GetMapping("") | |
| 91 | public Articles getById(@Parameter(name = "id") @RequestParam Long id) { | |
| 92 |
1
1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::getById → KILLED |
return articlesRepository |
| 93 | .findById(id) | |
| 94 |
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)); |
| 95 | } | |
| 96 | ||
| 97 | /** | |
| 98 | * Update a single article | |
| 99 | * | |
| 100 | * @param id id of the article to update | |
| 101 | * @param incoming the new article | |
| 102 | * @return the updated article object | |
| 103 | */ | |
| 104 | @Operation(summary = "Update a single article") | |
| 105 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 106 | @PutMapping("") | |
| 107 | public Articles updateArticle( | |
| 108 | @Parameter(name = "id") @RequestParam Long id, @RequestBody @Valid Articles incoming) { | |
| 109 | ||
| 110 | Articles articles = | |
| 111 | articlesRepository | |
| 112 | .findById(id) | |
| 113 |
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)); |
| 114 | ||
| 115 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED |
articles.setTitle(incoming.getTitle()); |
| 116 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED |
articles.setUrl(incoming.getUrl()); |
| 117 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED |
articles.setExplanation(incoming.getExplanation()); |
| 118 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED |
articles.setEmail(incoming.getEmail()); |
| 119 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED |
articles.setDateAdded(incoming.getDateAdded()); |
| 120 | ||
| 121 |
1
1. updateArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateArticle → KILLED |
return articlesRepository.save(articles); |
| 122 | } | |
| 123 | ||
| 124 | @Operation(summary = "Delete a single article") | |
| 125 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 126 | @DeleteMapping("") | |
| 127 | public Map<String, String> deleteArticle(@Parameter(name = "id") @RequestParam Long id) { | |
| 128 | Articles articles = | |
| 129 | articlesRepository | |
| 130 | .findById(id) | |
| 131 |
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)); |
| 132 | ||
| 133 |
1
1. deleteArticle : removed call to edu/ucsb/cs156/example/repositories/ArticlesRepository::delete → KILLED |
articlesRepository.delete(articles); |
| 134 |
1
1. deleteArticle : replaced return value with Collections.emptyMap for edu/ucsb/cs156/example/controllers/ArticlesController::deleteArticle → KILLED |
return Map.of("message", String.format("Articles with id %d deleted", id)); |
| 135 | } | |
| 136 | } | |
Mutations | ||
| 44 |
1.1 |
|
| 79 |
1.1 |
|
| 80 |
1.1 |
|
| 81 |
1.1 |
|
| 82 |
1.1 |
|
| 83 |
1.1 |
|
| 85 |
1.1 |
|
| 92 |
1.1 |
|
| 94 |
1.1 |
|
| 113 |
1.1 |
|
| 115 |
1.1 |
|
| 116 |
1.1 |
|
| 117 |
1.1 |
|
| 118 |
1.1 |
|
| 119 |
1.1 |
|
| 121 |
1.1 |
|
| 131 |
1.1 |
|
| 133 |
1.1 |
|
| 134 |
1.1 |