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 rsp) throws ServletException, IOException {
        courseDB coursedb = new courseDB();
        ArrayList<courseInfo> course_list = coursedb.getAllCourses();
        StringBuilder json= new StringBuilder("[");
        for (courseInfo course : course_list) {
            json.append(course.toJSON()).append(",");
        }
        rsp.setContentType("application/json;charset=utf-8");
        PrintWriter out = rsp.getWriter();
        out.print(json.substring(0, json.length()-1));
        out.print("]");
    }
}
