package projet;

import jakarta.servlet.RequestDispatcher;
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 jakarta.servlet.http.HttpSession;

import java.io.IOException;

@WebServlet("/ModifBloc")
public class ModifBloc extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ModifBloc() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Utilisateur u = (Utilisateur) session.getAttribute("utilisateur");
		
		if(u != null) {
			RequestDispatcher rd = request.getRequestDispatcher("AfficherPage.jsp");
			rd.forward(request, response);
		}else {
			response.sendRedirect("/Projet/");
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Utilisateur u = (Utilisateur) session.getAttribute("utilisateur");
		
		if(u != null) {
			String contenu = request.getParameter("contenu");
			String type = request.getParameter("type");
			String metadata = request.getParameter("metadata");
			
	        int blocId = Integer.parseInt(request.getParameter("blocId"));

	        Bloc.updateBloc(blocId, contenu, type, metadata);
	        response.sendRedirect("AfficherPage");	
		} else {
			response.sendRedirect("/Projet/");
		}
	}
}
