CourseController.java

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

  2. import edu.ucsb.cs156.happiercows.entities.Course;
  3. import com.fasterxml.jackson.core.JsonProcessingException;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException;
  6. import edu.ucsb.cs156.happiercows.models.HealthUpdateStrategyList;
  7. import edu.ucsb.cs156.happiercows.repositories.CourseRepository;
  8. import io.swagger.v3.oas.annotations.Operation;
  9. import io.swagger.v3.oas.annotations.Parameter;
  10. import io.swagger.v3.oas.annotations.tags.Tag;
  11. import lombok.extern.slf4j.Slf4j;

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

  21. import java.time.LocalDateTime;

  22. @Tag(name = "courses")
  23. @RequestMapping("/api/courses")
  24. @RestController
  25. @Slf4j
  26. public class CourseController extends ApiController {

  27.     @Autowired
  28.     CourseRepository courseRepository;

  29.     // Get all records in the table and return as a JSON array
  30.     @Operation(summary= "List all courses")
  31.     @PreAuthorize("hasRole('ROLE_USER')")
  32.     @GetMapping("/all")
  33.     public Iterable<Course> allCourses() {
  34.         Iterable<Course> courses = courseRepository.findAll();
  35.         return courses;
  36.     }

  37.     // Use the data in the input parameters to create a new row in the table and return the data as JSON
  38.     @Operation(summary= "Create a new course")
  39.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  40.     @PostMapping("/post")
  41.     public Course postCourse(
  42.             @Parameter(name = "name") @RequestParam String name,
  43.             @Parameter(name = "school") @RequestParam String school,
  44.             @Parameter(name = "term") @RequestParam String term,
  45.             @Parameter(name = "startDate", description = "in iso format, i.e. YYYY-MM-DDTHH:MM:SS")
  46.             @RequestParam("startDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
  47.             @Parameter(name = "endDate", description = "in iso format, i.e. YYYY-MM-DDTHH:MM:SS")
  48.             @RequestParam("endDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate) {
  49.    
  50.         Course course = Course.builder()
  51.                 .name(name)
  52.                 .school(school)
  53.                 .term(term)
  54.                 .startDate(startDate)
  55.                 .endDate(endDate)
  56.                 .build();
  57.    
  58.         return courseRepository.save(course);
  59.     }

  60.     @Operation(summary = "Delete a course")
  61.     @PreAuthorize("hasRole('ROLE_ADMIN')")
  62.     @DeleteMapping("")
  63.     public Object deleteCourse(
  64.             @Parameter(description = "ID of the Course to delete") @RequestParam Long id) {
  65.         Course course = courseRepository.findById(id)
  66.                 .orElseThrow(() -> new EntityNotFoundException(Course.class, id));

  67.         courseRepository.delete(course);
  68.         return genericMessage(String.format("Course with id %s deleted", id));
  69.     }

  70. }