CourseController.java

package edu.ucsb.cs156.happiercows.controllers;

import edu.ucsb.cs156.happiercows.entities.Course;
import edu.ucsb.cs156.happiercows.errors.EntityNotFoundException;
import edu.ucsb.cs156.happiercows.models.CourseDTO;
import edu.ucsb.cs156.happiercows.repositories.CourseRepository;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Tag(name = "Course")
@RequestMapping("/api/course")
@RestController
public class CourseController extends ApiController {
    @Autowired
    private CourseRepository courseRepository;

    /**
     * This method returns a list of all courses.
     * 
     * @return a list of all courses
     */
    @Operation(summary = "List all courses")
    @PreAuthorize("hasRole('ROLE_USER')")
    @GetMapping("/all")
    public Iterable<Course> allOrganisations() {
        Iterable<Course> courses = courseRepository.findAll();
        return courses;
    }

    @Operation(summary = "Get a course by id")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @GetMapping("/{id}")
    public Course getCourseById(
            @Parameter(name = "id") @PathVariable Long id) {
        Course course = courseRepository.findById(id)
                .orElseThrow(() -> new EntityNotFoundException(Course.class, id));
        return course;
    }

    @Operation(summary = "Create a course")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @PostMapping("")
    public Course postCourse(
            @Parameter(name = "course") @RequestBody CourseDTO courseDTO) {
        Course savedCourse = courseRepository.save(courseDTO.toCourse());
        return savedCourse;
    }

    /**
     * Update a single course.
     * 
     * @param id id of the course to update
     * @param incoming the new course information
     * @return the updated course object
     */
    @Operation(summary = "Update a single course")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @PutMapping("")
    public Course updateCourse(
            @Parameter(name = "id") @RequestParam Long id,
            @RequestBody @Valid Course incoming) {
        
        Course currCourse = courseRepository
                .findById(id)
                .orElseThrow(() -> new EntityNotFoundException(Course.class, id));

        currCourse.setCode(incoming.getCode());
        currCourse.setName(incoming.getName());
        currCourse.setTerm(incoming.getTerm());

        courseRepository.save(currCourse);

        return currCourse;  

    }


    /**
     * Delete a course
     *
     * @param id the id of the course to delete
     * @return a message indicating the course was deleted
     */
    @Operation(summary = "Delete a Course")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @DeleteMapping("")
    public Object deleteCourse(@Parameter(name = "id") @RequestParam Long id) {
        Course currCourse =
            courseRepository
                .findById(id)
                .orElseThrow(() -> new EntityNotFoundException(Course.class, id));

        courseRepository.delete(currCourse);
        return genericMessage("Course with id %s deleted".formatted(id));
  }
    

}