| 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 org.springframework.beans.factory.annotation.Autowired; | |
| 13 | import org.springframework.format.annotation.DateTimeFormat; | |
| 14 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 15 | import org.springframework.web.bind.annotation.*; | |
| 16 | ||
| 17 | import jakarta.validation.Valid; | |
| 18 | import java.time.LocalDateTime; | |
| 19 | ||
| 20 | /** | |
| 21 | * REST controller for managing articles. | |
| 22 | */ | |
| 23 | ||
| 24 | @Tag(name = "Articles") | |
| 25 | @RequestMapping("/api/articles") | |
| 26 | @RestController | |
| 27 | @Slf4j | |
| 28 | public class ArticlesController extends ApiController { | |
| 29 | ||
| 30 | @Autowired | |
| 31 | ArticlesRepository articlesRepository; | |
| 32 | ||
| 33 | /** | |
| 34 | * List all articles. | |
| 35 | * | |
| 36 | * @return iterable of all articles | |
| 37 | */ | |
| 38 | @Operation(summary = "List all articles") | |
| 39 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 40 | @GetMapping("/all") | |
| 41 | public Iterable<Articles> allArticles() { | |
| 42 |
1
1. allArticles : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/ArticlesController::allArticles → KILLED |
return articlesRepository.findAll(); |
| 43 | } | |
| 44 | ||
| 45 | /** | |
| 46 | * Get a single article by ID. | |
| 47 | * | |
| 48 | * @param id ID of the article to retrieve | |
| 49 | * @return the requested article | |
| 50 | */ | |
| 51 | @Operation(summary = "Get a single article by ID") | |
| 52 | @PreAuthorize("hasRole('ROLE_USER')") | |
| 53 | @GetMapping("") | |
| 54 | public Articles getById(@Parameter(name = "id") @RequestParam Long id) { | |
| 55 |
1
1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::getById → KILLED |
return articlesRepository.findById(id) |
| 56 |
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)); |
| 57 | } | |
| 58 | ||
| 59 | /** | |
| 60 | * Create a new article. | |
| 61 | * | |
| 62 | * @param title title of the article | |
| 63 | * @param url URL of the article | |
| 64 | * @param explanation explanation of the article | |
| 65 | * @param email email of the article creator | |
| 66 | * @param dateAdded date the article was added | |
| 67 | * @return the newly created 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 @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime dateAdded) { | |
| 78 | ||
| 79 | Articles article = Articles.builder() | |
| 80 | .title(title) | |
| 81 | .url(url) | |
| 82 | .explanation(explanation) | |
| 83 | .email(email) | |
| 84 | .dateAdded(dateAdded) | |
| 85 | .build(); | |
| 86 | ||
| 87 |
1
1. postArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postArticle → KILLED |
return articlesRepository.save(article); |
| 88 | } | |
| 89 | ||
| 90 | /** | |
| 91 | * Update an existing article. | |
| 92 | * | |
| 93 | * @param id ID of the article to update | |
| 94 | * @param incoming article data to update with | |
| 95 | * @return the updated article | |
| 96 | */ | |
| 97 | @Operation(summary = "Update an existing article") | |
| 98 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 99 | @PutMapping("") | |
| 100 | public Articles updateArticle( | |
| 101 | @Parameter(name = "id") @RequestParam Long id, | |
| 102 | @RequestBody @Valid Articles incoming) { | |
| 103 | ||
| 104 | Articles article = articlesRepository.findById(id) | |
| 105 |
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)); |
| 106 | ||
| 107 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED |
article.setTitle(incoming.getTitle()); |
| 108 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED |
article.setUrl(incoming.getUrl()); |
| 109 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED |
article.setExplanation(incoming.getExplanation()); |
| 110 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED |
article.setEmail(incoming.getEmail()); |
| 111 |
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED |
article.setDateAdded(incoming.getDateAdded()); |
| 112 | ||
| 113 |
1
1. updateArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateArticle → KILLED |
return articlesRepository.save(article); |
| 114 | } | |
| 115 | ||
| 116 | /** | |
| 117 | * Delete an article by ID. | |
| 118 | * | |
| 119 | * @param id ID of the article to delete | |
| 120 | * @return confirmation message | |
| 121 | */ | |
| 122 | @Operation(summary = "Delete an article by ID") | |
| 123 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 124 | @DeleteMapping("") | |
| 125 | public Object deleteArticle(@Parameter(name = "id") @RequestParam Long id) { | |
| 126 | Articles article = articlesRepository.findById(id) | |
| 127 |
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)); |
| 128 | ||
| 129 |
1
1. deleteArticle : removed call to edu/ucsb/cs156/example/repositories/ArticlesRepository::delete → KILLED |
articlesRepository.delete(article); |
| 130 |
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)); |
| 131 | } | |
| 132 | } | |
Mutations | ||
| 42 |
1.1 |
|
| 55 |
1.1 |
|
| 56 |
1.1 |
|
| 87 |
1.1 |
|
| 105 |
1.1 |
|
| 107 |
1.1 |
|
| 108 |
1.1 |
|
| 109 |
1.1 |
|
| 110 |
1.1 |
|
| 111 |
1.1 |
|
| 113 |
1.1 |
|
| 127 |
1.1 |
|
| 129 |
1.1 |
|
| 130 |
1.1 |