| 1 | package edu.ucsb.cs156.example.controllers; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.example.entities.Article; | |
| 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.http.HttpStatus; | |
| 17 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 18 | import org.springframework.web.bind.annotation.DeleteMapping; | |
| 19 | import org.springframework.web.bind.annotation.GetMapping; | |
| 20 | import org.springframework.web.bind.annotation.PostMapping; | |
| 21 | import org.springframework.web.bind.annotation.PutMapping; | |
| 22 | import org.springframework.web.bind.annotation.RequestBody; | |
| 23 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 24 | import org.springframework.web.bind.annotation.RequestParam; | |
| 25 | import org.springframework.web.bind.annotation.RestController; | |
| 26 | import org.springframework.web.server.ResponseStatusException; | |
| 27 | ||
| 28 | import jakarta.validation.Valid; | |
| 29 | ||
| 30 | import java.time.LocalDateTime; | |
| 31 | import java.time.format.DateTimeFormatter; | |
| 32 | import java.time.format.DateTimeParseException; | |
| 33 | import java.util.List; | |
| 34 | ||
| 35 | /** | |
| 36 |  * This is a REST controller for Articles | |
| 37 |  */ | |
| 38 | ||
| 39 | @Tag(name = "Articles") | |
| 40 | @RequestMapping("/api/articles") | |
| 41 | @RestController | |
| 42 | @Slf4j | |
| 43 | public class ArticlesController extends ApiController { | |
| 44 | ||
| 45 |     @Autowired | |
| 46 |     ArticlesRepository articlesRepository; | |
| 47 | ||
| 48 |     /** | |
| 49 |      * List all articles | |
| 50 |      *  | |
| 51 |      * @return an iterable of Articles | |
| 52 |      */ | |
| 53 |     @Operation(summary= "List all articles") | |
| 54 |     @PreAuthorize("hasRole('ROLE_USER')") | |
| 55 |     @GetMapping("/all") | |
| 56 |     public Iterable<Article> allArticles() { | |
| 57 | 
1
1. allArticles : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/ArticlesController::allArticles → KILLED | 
        return articlesRepository.findAll(); | 
| 58 |     } | |
| 59 | ||
| 60 |     /** | |
| 61 |      * Get a single article by id | |
| 62 |      *  | |
| 63 |      * @param id the id of the article | |
| 64 |      * @return an Article | |
| 65 |      */ | |
| 66 |     @Operation(summary= "Get a single article") | |
| 67 |     @PreAuthorize("hasRole('ROLE_USER')") | |
| 68 |     @GetMapping("") | |
| 69 |     public Article getById( | |
| 70 |             @Parameter(name="id") @RequestParam Long id) { | |
| 71 |         Article article = articlesRepository.findById(id) | |
| 72 | 
1
1. lambda$getById$0 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$getById$0 → KILLED | 
                .orElseThrow(() -> new EntityNotFoundException(Article.class, id)); | 
| 73 | ||
| 74 | 
1
1. getById : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::getById → KILLED | 
        return article; | 
| 75 |     } | |
| 76 | ||
| 77 |     /** | |
| 78 |      * Create a new article | |
| 79 |      *  | |
| 80 |      * @param title the title of the article | |
| 81 |      * @param url the url of the article | |
| 82 |      * @param explanation the explanation of the article | |
| 83 |      * @param email the email of the person who added the article | |
| 84 |      * @param dateAdded the date the article was added | |
| 85 |      * @return the saved article | |
| 86 |      */ | |
| 87 |     @Operation(summary= "Create a new article") | |
| 88 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 89 |     @PostMapping("/post") | |
| 90 |     public Article postArticle( | |
| 91 |             @Parameter(name="title") @RequestParam String title, | |
| 92 |             @Parameter(name="url") @RequestParam String url, | |
| 93 |             @Parameter(name="explanation") @RequestParam String explanation, | |
| 94 |             @Parameter(name="email") @RequestParam String email, | |
| 95 |             @Parameter(name="dateAdded") @RequestParam String dateAdded) | |
| 96 |             throws JsonProcessingException { | |
| 97 | ||
| 98 |         LocalDateTime dateTime; | |
| 99 |         try { | |
| 100 |             dateTime = parseDateTime(dateAdded); | |
| 101 |         } catch (DateTimeParseException e) { | |
| 102 |             throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid date format. Use ISO date format (YYYY-MM-DD) or ISO date-time format (YYYY-MM-DDThh:mm:ss)"); | |
| 103 |         } | |
| 104 | ||
| 105 |         log.info("dateAdded={}", dateTime); | |
| 106 | ||
| 107 |         Article article = Article.builder() | |
| 108 |                 .title(title) | |
| 109 |                 .url(url) | |
| 110 |                 .explanation(explanation) | |
| 111 |                 .email(email) | |
| 112 |                 .dateAdded(dateTime) | |
| 113 |                 .build(); | |
| 114 | ||
| 115 |         Article savedArticle = articlesRepository.save(article); | |
| 116 | ||
| 117 | 
1
1. postArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::postArticle → KILLED | 
        return savedArticle; | 
| 118 |     } | |
| 119 | ||
| 120 |     /** | |
| 121 |      * Delete an Article | |
| 122 |      *  | |
| 123 |      * @param id the id of the article to delete | |
| 124 |      * @return a message indicating the article was deleted | |
| 125 |      */ | |
| 126 |     @Operation(summary= "Delete an Article") | |
| 127 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 128 |     @DeleteMapping("") | |
| 129 |     public Object deleteArticle( | |
| 130 |             @Parameter(name="id") @RequestParam Long id) { | |
| 131 |         Article article = articlesRepository.findById(id) | |
| 132 | 
1
1. lambda$deleteArticle$1 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$deleteArticle$1 → KILLED | 
                .orElseThrow(() -> new EntityNotFoundException(Article.class, id)); | 
| 133 | ||
| 134 | 
1
1. deleteArticle : removed call to edu/ucsb/cs156/example/repositories/ArticlesRepository::delete → KILLED | 
        articlesRepository.delete(article); | 
| 135 | 
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)); | 
| 136 |     } | |
| 137 | ||
| 138 |     /** | |
| 139 |      * Update a single article | |
| 140 |      *  | |
| 141 |      * @param id the id of the article to update | |
| 142 |      * @param incoming the new article data | |
| 143 |      * @return the updated article | |
| 144 |      */ | |
| 145 |     @Operation(summary= "Update a single article") | |
| 146 |     @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 147 |     @PutMapping("") | |
| 148 |     public Article updateArticle( | |
| 149 |             @Parameter(name="id") @RequestParam Long id, | |
| 150 |             @RequestBody @Valid Article incoming) { | |
| 151 | ||
| 152 |         Article article = articlesRepository.findById(id) | |
| 153 | 
1
1. lambda$updateArticle$2 : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::lambda$updateArticle$2 → KILLED | 
                .orElseThrow(() -> new EntityNotFoundException(Article.class, id)); | 
| 154 | ||
| 155 | 
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setTitle → KILLED | 
        article.setTitle(incoming.getTitle()); | 
| 156 | 
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setUrl → KILLED | 
        article.setUrl(incoming.getUrl()); | 
| 157 | 
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setExplanation → KILLED | 
        article.setExplanation(incoming.getExplanation()); | 
| 158 | 
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setEmail → KILLED | 
        article.setEmail(incoming.getEmail()); | 
| 159 | 
1
1. updateArticle : removed call to edu/ucsb/cs156/example/entities/Article::setDateAdded → KILLED | 
        article.setDateAdded(incoming.getDateAdded()); | 
| 160 | ||
| 161 |         articlesRepository.save(article); | |
| 162 | ||
| 163 | 
1
1. updateArticle : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::updateArticle → KILLED | 
        return article; | 
| 164 |     } | |
| 165 | ||
| 166 |     /** | |
| 167 |      * Get articles within a date range | |
| 168 |      *  | |
| 169 |      * @param startDate the start date in ISO format (yyyy-MM-dd or yyyy-MM-ddTHH:mm:ss) | |
| 170 |      * @param endDate the end date in ISO format (yyyy-MM-dd or yyyy-MM-ddTHH:mm:ss) | |
| 171 |      * @return a list of Articles within the date range | |
| 172 |      */ | |
| 173 |     @Operation(summary= "Get articles within a date range") | |
| 174 |     @PreAuthorize("hasRole('ROLE_USER')") | |
| 175 |     @GetMapping("/bydate") | |
| 176 |     public List<Article> getArticlesByDateRange( | |
| 177 |             @Parameter(name="startDate") @RequestParam String startDate, | |
| 178 |             @Parameter(name="endDate") @RequestParam String endDate) { | |
| 179 |          | |
| 180 |         LocalDateTime start, end; | |
| 181 |         try { | |
| 182 |             start = parseDateTime(startDate); | |
| 183 |             end = parseDateTime(endDate); | |
| 184 |         } catch (DateTimeParseException e) { | |
| 185 |             throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid date format. Use ISO date format (YYYY-MM-DD) or ISO date-time format (YYYY-MM-DDThh:mm:ss)"); | |
| 186 |         } | |
| 187 |          | |
| 188 | 
1
1. getArticlesByDateRange : replaced return value with Collections.emptyList for edu/ucsb/cs156/example/controllers/ArticlesController::getArticlesByDateRange → KILLED | 
        return articlesRepository.findByDateAddedBetween(start, end); | 
| 189 |     } | |
| 190 |      | |
| 191 |     /** | |
| 192 |      * Helper method to parse date strings in either date-only or date-time format | |
| 193 |      */ | |
| 194 |     private LocalDateTime parseDateTime(String dateString) { | |
| 195 | 
1
1. parseDateTime : negated conditional → KILLED | 
        if (dateString.contains("T")) { | 
| 196 |             // Full date-time format | |
| 197 | 
1
1. parseDateTime : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::parseDateTime → KILLED | 
            return LocalDateTime.parse(dateString); | 
| 198 |         } else { | |
| 199 |             // Date-only format, append T00:00:00 for midnight | |
| 200 | 
1
1. parseDateTime : replaced return value with null for edu/ucsb/cs156/example/controllers/ArticlesController::parseDateTime → KILLED | 
            return LocalDateTime.parse(dateString + "T00:00:00"); | 
| 201 |         } | |
| 202 |     } | |
| 203 | } | |
Mutations | ||
| 57 | 
 
 1.1  | 
|
| 72 | 
 
 1.1  | 
|
| 74 | 
 
 1.1  | 
|
| 117 | 
 
 1.1  | 
|
| 132 | 
 
 1.1  | 
|
| 134 | 
 
 1.1  | 
|
| 135 | 
 
 1.1  | 
|
| 153 | 
 
 1.1  | 
|
| 155 | 
 
 1.1  | 
|
| 156 | 
 
 1.1  | 
|
| 157 | 
 
 1.1  | 
|
| 158 | 
 
 1.1  | 
|
| 159 | 
 
 1.1  | 
|
| 163 | 
 
 1.1  | 
|
| 188 | 
 
 1.1  | 
|
| 195 | 
 
 1.1  | 
|
| 197 | 
 
 1.1  | 
|
| 200 | 
 
 1.1  |