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;
import java.io.PrintWriter;
import java.util.ArrayList;

@WebServlet("/CourseListAction")
public class CourseListAction 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;
        }
        ArrayList<CourseInfo> course_list = CourseDB.getAllCourses();
        StringBuilder json= new StringBuilder("[");
        for (CourseInfo course : course_list) {
            json.append(course.toJSON()).append(",");
        }
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        out.print(json.substring(0, json.length()-1));
        out.print("]");
    }
}
