package com.lion.lionwebsite.Filter;

import com.lion.lionwebsite.Dao.normal.UserMapper;
import com.lion.lionwebsite.Util.CustomUtil;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;

import java.io.IOException;

@WebFilter(filterName = "AccessFilter", urlPatterns = {"/validate"})
public class AccessFilter implements Filter {

    UserMapper userMapper;

    public AccessFilter(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        String authCode = servletRequest.getParameter("AuthCode");

        if(authCode == null)
            return;

        userMapper.updateLastAccessTime(CustomUtil.now(), authCode);
        filterChain.doFilter(servletRequest, servletResponse);
    }
}
