package test05.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import test05.pojo.Course;
import test05.pojo.Result;
import test05.service.CourseService;

@RestController
@RequestMapping("/courseAction")
public class CourseController {
    private final CourseService courseService;

    @Autowired
    public CourseController(CourseService courseService) {
        this.courseService = courseService;
    }

    @RequestMapping("/get")
    public Result getCourse(String keyword, Integer type, Integer grade, Float credit, Integer page, Integer pageSize) {
        Course course = new Course();
        course.setName(keyword==null?"":keyword);
        course.setDetail(keyword==null?"":keyword);
        course.setType((type==null?-1:type));
        course.setGrade((grade==null?-1:grade));
        course.setCredit(credit==null?-1:credit);
        if (page == null || page < 0) {
            page = 0;
        }
        if (pageSize == null || pageSize < 0) {
            pageSize = 10;
        }
        int offset = Math.max(0, (page - 1)) * pageSize;
        return courseService.getCourse(course, offset, pageSize);
    }

    @RequestMapping("/edit")
    public Result editCourse(Course course) {
        return courseService.updateCourse(course);
    }

    @RequestMapping("/add")
    public Result addCourse(Course course) {
        return courseService.addCourse(course);
    }

    @RequestMapping("/delete")
    public Result deleteCourse(Integer id) {
        Course course = new Course();
        course.setId(id);
        return courseService.deleteCourse(course);
    }
}
