ArticlesController.java

  1. package edu.ucsb.cs156.example.controllers;

  2. import edu.ucsb.cs156.example.entities.Articles;
  3. import edu.ucsb.cs156.example.errors.EntityNotFoundException;
  4. import edu.ucsb.cs156.example.repositories.ArticlesRepository;

  5. import io.swagger.v3.oas.annotations.Operation;
  6. import io.swagger.v3.oas.annotations.Parameter;
  7. import io.swagger.v3.oas.annotations.tags.Tag;
  8. import lombok.extern.slf4j.Slf4j;

  9. import com.fasterxml.jackson.core.JsonProcessingException;

  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.format.annotation.DateTimeFormat;
  12. import org.springframework.security.access.prepost.PreAuthorize;
  13. import org.springframework.web.bind.annotation.DeleteMapping;
  14. import org.springframework.web.bind.annotation.GetMapping;
  15. import org.springframework.web.bind.annotation.PostMapping;
  16. import org.springframework.web.bind.annotation.PutMapping;
  17. import org.springframework.web.bind.annotation.RequestBody;
  18. import org.springframework.web.bind.annotation.RequestMapping;
  19. import org.springframework.web.bind.annotation.RequestParam;
  20. import org.springframework.web.bind.annotation.RestController;

  21. import jakarta.validation.Valid;

  22. import java.time.LocalDateTime;

  23. /**
  24.  * This is a REST controller for Articles
  25.  */

  26. @Tag(name = "articles")
  27. @RequestMapping("/api/articles")
  28. @RestController
  29. @Slf4j
  30. public class ArticlesController extends ApiController {

  31.     @Autowired
  32.     ArticlesRepository articlesRepository;

  33.     /**
  34.      * List all Articles
  35.      *
  36.      * @return an iterable of Articles
  37.      */
  38.     @Operation(summary= "List all articles")
  39.     @PreAuthorize("hasRole('ROLE_USER')")
  40.     @GetMapping("/all")
  41.     public Iterable<Articles> allArticles() {
  42.         Iterable<Articles> articles = articlesRepository.findAll();
  43.         return articles;
  44.     }

  45.     /**
  46.      * Get a single article by id
  47.      *
  48.      * @param id the id of the article
  49.      * @return an article
  50.      */
  51.     @Operation(summary= "Get a single article")
  52.     @PreAuthorize("hasRole('ROLE_USER')")
  53.     @GetMapping("")
  54.     public Articles getById(
  55.             @Parameter(name="id") @RequestParam Long id) {
  56.         Articles article = articlesRepository.findById(id)
  57.                 .orElseThrow(() -> new EntityNotFoundException(Articles.class, id));

  58.         return article;
  59.     }

  60.     /**
  61.      * Create a new article
  62.      *
  63.      * @param title         the title of the article
  64.      * @param url           the url of the article
  65.      * @param explanation   an explanation of the article
  66.      * @param email         the email of the article creator
  67.      * @param dateAdded     the date the article was added
  68.      * @return the saved article
  69.      */
  70.     @Operation(summary= "Create a new article")
  71.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  72.     @PostMapping("/post")
  73.     public Articles postArticle(
  74.             @Parameter(name="title") @RequestParam String title,
  75.             @Parameter(name="url") @RequestParam String url,
  76.             @Parameter(name="explanation") @RequestParam String explanation,
  77.             @Parameter(name="email") @RequestParam String email,
  78.             @Parameter(name="dateAdded") @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime dateAdded)
  79.             throws JsonProcessingException {

  80.         // For an explanation of @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
  81.         // See: https://www.baeldung.com/spring-date-parameters

  82.         log.info("localDateTime={}", dateAdded);

  83.         Articles article = new Articles();
  84.         article.setTitle(title);
  85.         article.setUrl(url);
  86.         article.setExplanation(explanation);
  87.         article.setEmail(email);
  88.         article.setDateAdded(dateAdded);

  89.         Articles savedArticle = articlesRepository.save(article);

  90.         return savedArticle;
  91.     }
  92.     /**
  93.      * Update a single article
  94.      *
  95.      * @param id       id of the date to update
  96.      * @param incoming the new article
  97.      * @return the updated article object
  98.      */
  99.     @Operation(summary= "Update a single article")
  100.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  101.     @PutMapping("")
  102.     public Articles updateArticle(
  103.             @Parameter(name="id") @RequestParam Long id,
  104.             @RequestBody @Valid Articles incoming) {

  105.         Articles article = articlesRepository.findById(id)
  106.                 .orElseThrow(() -> new EntityNotFoundException(Articles.class, id));

  107.         article.setTitle(incoming.getTitle());
  108.         article.setUrl(incoming.getUrl());
  109.         article.setExplanation(incoming.getExplanation());
  110.         article.setEmail(incoming.getEmail());
  111.         article.setDateAdded(incoming.getDateAdded());

  112.         articlesRepository.save(article);

  113.         return article;
  114.     }

  115.     /**
  116.      * Delete an article
  117.      *
  118.      * @param id the id of the article to delete
  119.      * @return a message indicating the article was deleted
  120.      */
  121.     @Operation(summary= "Delete an article")
  122.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  123.     @DeleteMapping("")
  124.     public Object deleteArticle(
  125.             @Parameter(name="id") @RequestParam Long id) {
  126.         Articles article = articlesRepository.findById(id)
  127.                 .orElseThrow(() -> new EntityNotFoundException(Articles.class, id));

  128.         articlesRepository.delete(article);
  129.         return genericMessage("Articles with id %s deleted".formatted(id));
  130.     }
  131. }