| 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 | @Tag(name = "Articles") | |
| 31 | @RequestMapping("/api/articles") | |
| 32 | @RestController | |
| 33 | @Slf4j | |
| 34 | ||
| 35 | public class ArticlesController extends ApiController { | |
| 36 |     @Autowired | |
| 37 |     ArticlesRepository articlesRepository; | |
| 38 | ||
| 39 |     @Operation(summary = "List all articles") | |
| 40 |     @PreAuthorize("hasRole('ROLE_USER')") | |
| 41 |     @GetMapping("/all") | |
| 42 |     public Iterable<Articles> allArticles() { | |
| 43 |         Iterable<Articles> records = articlesRepository.findAll(); | |
| 44 | 
1
1. allArticles : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/ArticlesController::allArticles → KILLED | 
        return records; | 
| 45 |     } | |
| 46 | ||
| 47 |     @Operation(summary = "Create a new article") | |
| 48 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 49 |     @PostMapping("/post") | |
| 50 |     public Articles postArticles( | |
| 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") @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("dateAdded={}", dateAdded); | |
| 62 | ||
| 63 |         Articles records = new Articles(); | |
| 64 | 
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED | 
        records.setTitle(title); | 
| 65 | 
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED | 
        records.setUrl(url); | 
| 66 | 
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED | 
        records.setExplanation(explanation); | 
| 67 | 
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED | 
        records.setEmail(email); | 
| 68 | 
1
1. postArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED | 
        records.setDateAdded(dateAdded); | 
| 69 | ||
| 70 |         Articles savedArticles = articlesRepository.save(records); | |
| 71 | ||
| 72 | 
1
1. postArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postArticles → KILLED | 
        return savedArticles; | 
| 73 |     } | |
| 74 | ||
| 75 |     @Operation(summary = "Get a single article") | |
| 76 |     @PreAuthorize("hasRole('ROLE_USER')") | |
| 77 |     @GetMapping("") | |
| 78 |     public Articles getById( | |
| 79 |             @Parameter(name = "id") @RequestParam Long id) { | |
| 80 |         Articles article = articlesRepository.findById(id) | |
| 81 | 
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)); | 
| 82 | ||
| 83 | 
1
1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::getById → KILLED | 
        return article; | 
| 84 |     } | |
| 85 | ||
| 86 |     @Operation(summary = "Update a single article") | |
| 87 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 88 |     @PutMapping("") | |
| 89 |     public Articles updateArticles( | |
| 90 |             @Parameter(name = "id") @RequestParam Long id, | |
| 91 |             @RequestBody @Valid Articles incoming) { | |
| 92 | ||
| 93 |         Articles article = articlesRepository.findById(id) | |
| 94 | 
1
1. lambda$updateArticles$1 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$updateArticles$1 → KILLED | 
                .orElseThrow(() -> new EntityNotFoundException(Articles.class, id)); | 
| 95 | ||
| 96 | 
1
1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setTitle → KILLED | 
        article.setTitle(incoming.getTitle()); | 
| 97 | 
1
1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setUrl → KILLED | 
        article.setUrl(incoming.getUrl()); | 
| 98 | 
1
1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setExplanation → KILLED | 
        article.setExplanation(incoming.getExplanation()); | 
| 99 | 
1
1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setEmail → KILLED | 
        article.setEmail(incoming.getEmail()); | 
| 100 | 
1
1. updateArticles : removed call to edu/ucsb/cs156/example/entities/Articles::setDateAdded → KILLED | 
        article.setDateAdded(incoming.getDateAdded()); | 
| 101 | ||
| 102 |         articlesRepository.save(article); | |
| 103 | ||
| 104 | 
1
1. updateArticles : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateArticles → KILLED | 
        return article; | 
| 105 |     } | |
| 106 | ||
| 107 |     @Operation(summary = "Delete an article") | |
| 108 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 109 |     @DeleteMapping("") | |
| 110 |     public Object deleteArticle( | |
| 111 |             @Parameter(name = "id") @RequestParam Long id) { | |
| 112 |         Articles article = articlesRepository.findById(id) | |
| 113 | 
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)); | 
| 114 | ||
| 115 | 
1
1. deleteArticle : removed call to edu/ucsb/cs156/example/repositories/ArticlesRepository::delete → KILLED | 
        articlesRepository.delete(article); | 
| 116 | 
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)); | 
| 117 |     } | |
| 118 | ||
| 119 | } | |
Mutations | ||
| 44 | 
 
 1.1  | 
|
| 64 | 
 
 1.1  | 
|
| 65 | 
 
 1.1  | 
|
| 66 | 
 
 1.1  | 
|
| 67 | 
 
 1.1  | 
|
| 68 | 
 
 1.1  | 
|
| 72 | 
 
 1.1  | 
|
| 81 | 
 
 1.1  | 
|
| 83 | 
 
 1.1  | 
|
| 94 | 
 
 1.1  | 
|
| 96 | 
 
 1.1  | 
|
| 97 | 
 
 1.1  | 
|
| 98 | 
 
 1.1  | 
|
| 99 | 
 
 1.1  | 
|
| 100 | 
 
 1.1  | 
|
| 104 | 
 
 1.1  | 
|
| 113 | 
 
 1.1  | 
|
| 115 | 
 
 1.1  | 
|
| 116 | 
 
 1.1  |