package course;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import login.UserInfo;

import java.io.IOException;

@WebServlet("/CourseAddAction")
public class CourseAddAction extends HttpServlet {
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String path = req.getContextPath();
        if (req.getSession().getAttribute("user") == null){
            resp.sendRedirect(path+"/login/login.jsp");
            return;
        }
        CourseInfo course = new CourseInfo();
        String name = req.getParameter("name");
        int type = Integer.parseInt(req.getParameter("type"));
        float credit = Float.parseFloat(req.getParameter("credit"));
        int grade = Integer.parseInt(req.getParameter("grade"));
        int major = Integer.parseInt(req.getParameter("major"));
        String detail = req.getParameter("detail");
        course.setName(name);
        course.setType(type);
        course.setCredit(credit);
        course.setGrade(grade);
        course.setMajor(major);
        course.setDetail(detail);
        CourseDB.insertCourse(course);
        resp.sendRedirect(path+"/course/course.jsp");
    }
}
