package course;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/CourseEditAction")
public class CourseEditAction extends HttpServlet {
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String path = req.getContextPath();
        if (req.getSession().getAttribute("user") == null){
            resp.setStatus(403);
            return;
        }
        int id = Integer.parseInt(req.getParameter("id"));
        CourseInfo course = CourseDB.getCourseInfo(id);
        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);

        if (CourseDB.editCourse(course)>=1){
            resp.sendRedirect(path+"/course/course.jsp");
        }else {
            resp.sendRedirect(req.getContextPath()+"/course/course.jsp?flag='update-failed'");
        }
    }
}
