package com.hotelmanagement.controller;

import com.hotel.bean.Customer;
import com.hotelmanagement.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/login")

public class LoginServlet extends HttpServlet{
	private static final long serialVersionUID = 1L;
    private UserService userService = new UserService();

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String CustomerId = request.getParameter("CustomerId");
        String password = request.getParameter("password");

   		Customer user = userService.validateUser(CustomerId, password);

        if (user != null) {
            HttpSession session = request.getSession();
            session.setAttribute("user", user);
            response.sendRedirect("home");
        } else {
            request.setAttribute("errorMessage", "Invalid User ID or Password");
            request.getRequestDispatcher("login.jsp").forward(request, response);
        }
    }
}
