package com.landaiqing.servlet.system;

import com.alibaba.fastjson.JSONObject;
import com.landaiqing.entity.AdminUserEntity;
import com.landaiqing.service.AdminUserService;
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.List;

@WebServlet("/adminList")
public class AllAdmin extends HttpServlet {
    private AdminUserService adminUserService;

    {
        try {
            adminUserService = new AdminUserService();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<AdminUserEntity> adminUserEntityList=adminUserService.selectAllAdmin();
        PrintWriter writer=resp.getWriter();
        String jsonString = JSONObject.toJSONString(adminUserEntityList);
        System.out.println(jsonString);
        writer.println(jsonString);
        writer.close();
//        req.setAttribute("allAdmin",adminUserEntityList);
//        req.getRequestDispatcher("./System/index.jsp").forward(req,resp);

    }
}
