package cn.ftebox.controller;

import cn.ftebox.entity.BlogType;
import cn.ftebox.service.BlogTypeService;
import cn.ftebox.util.Const;
import cn.ftebox.util.ResponseUtil;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping({"/admin/system"})
public class SystemAdminController {
    @Resource
    private BlogTypeService blogTypeService;
    /**
     * 刷新系统缓存
     */
    @RequestMapping({"refreshSystem.do"})
    public String refreshSystem(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletContext application = Objects.requireNonNull(RequestContextUtils.findWebApplicationContext(request)).getServletContext();
        List<BlogType> list = blogTypeService.countList();
        assert application != null;
        application.setAttribute(Const.BLOG_TYPE_COUNT_LIST,list);
        JSONObject result = new JSONObject();
        result.put("success",Boolean.TRUE);
        ResponseUtil.write(response,result);
        return null;
    }
}
