package top.awinx.stu01.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.awinx.stu01.common.Result;
import top.awinx.stu01.dto.StudentAddDTO;
import top.awinx.stu01.dto.StudentQueryDTO;
import top.awinx.stu01.entity.Student;
import top.awinx.stu01.service.StudentService;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;

import java.util.List;

/**
 * 学生CRUD控制器
 */
@RestController
@RequestMapping("/student")
public class StudentController {

    @Resource
    private StudentService studentService;

    /**
     * 添加学生接口
     * 请求方式：POST
     * 接口路径：/api/student/add
     */
    @PostMapping("/add")
    public Result<Student> addStudent(@Valid @RequestBody StudentAddDTO addDTO) {
        Student student = studentService.addStudent(addDTO);
        return Result.success("添加学生成功", student);
    }

    /**
     * 查询学生接口（兼容GET/POST，支持模糊查询）
     * 请求方式：GET/POST
     * 接口路径：/api/student/query
     */
    @GetMapping("/query")
    public Result<List<Student>> queryStudentByGet(StudentQueryDTO queryDTO) {
        List<Student> studentList = studentService.queryStudent(queryDTO);
        return Result.success("查询成功", studentList);
    }

    @PostMapping("/query")
    public Result<List<Student>> queryStudentByPost(@RequestBody StudentQueryDTO queryDTO) {
        List<Student> studentList = studentService.queryStudent(queryDTO);
        return Result.success("查询成功", studentList);
    }
}
