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.Result;
import test05.pojo.Student;
import test05.service.StudentService;

@RestController
@RequestMapping("/studentAction")
public class studentController {
    private final StudentService studentService;
    @Autowired
    public studentController(StudentService studentService) {
        this.studentService = studentService;
    }

    @RequestMapping("/get")
    public Result get(Student student, Integer page, Integer pageSize) {
        if (pageSize == null) {
            pageSize = 10;
        }
        if (page == null) {
            page = 1;
        }
        int offset = Math.max(0, (page - 1)) * pageSize;
        return studentService.getStudent(student, offset, pageSize);
    }

    @RequestMapping("/add")
    public Result add(Student student) {
        if (student == null || student.getId() != -1) {
            return Result.error("bad request");
        }else{
            return studentService.addStudent(student);
        }
    }

    @RequestMapping("/edit")
    public Result edit(Student student) {
        if (student == null || student.getId() == -1) {
            return Result.error("bad request");
        }
        return studentService.updateStudent(student);
    }

    @RequestMapping("/delete")
    public Result delete(Student student) {
        if (student == null || student.getId() <= 0) {
            return Result.error("bad request");
        }
        return studentService.deleteStudent(student);
    }
}
