Bladeren bron

web 模块更改为 restful 服务

reghao 3 maanden geleden
bovenliggende
commit
2c3e9f6955
1 gewijzigde bestanden met toevoegingen van 80 en 107 verwijderingen
  1. 80 107
      web/src/main/java/cn/reghao/bnt/web/blog/controller/ForegroundController.java

+ 80 - 107
web/src/main/java/cn/reghao/bnt/web/blog/controller/ForegroundController.java

@@ -1,32 +1,28 @@
 package cn.reghao.bnt.web.blog.controller;
 
-import cn.reghao.bnt.web.admin.service.FileService;
 import cn.reghao.bnt.web.blog.hibernate.HibernateQuery;
 import cn.reghao.bnt.web.blog.model.po.AboutView;
 import cn.reghao.bnt.web.blog.model.vo.*;
 import cn.reghao.bnt.web.util.JarFileResources;
+import cn.reghao.jutil.jdk.web.db.PageList;
 import cn.reghao.jutil.web.ServletUtil;
 import cn.reghao.bnt.web.blog.service.ArticleQuery;
 import cn.reghao.bnt.web.blog.model.CategoryType;
-import cn.reghao.bnt.web.blog.model.vo.BlogChannel;
 import cn.reghao.bnt.web.blog.service.ArticleViewService;
 import cn.reghao.bnt.web.blog.service.CategoryService;
+import cn.reghao.jutil.web.WebResult;
 import io.swagger.v3.oas.annotations.tags.Tag;
 import io.swagger.v3.oas.annotations.Operation;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.PageRequest;
-import org.springframework.data.domain.Pageable;
 import org.springframework.data.domain.Sort;
 import org.springframework.http.MediaType;
 import org.springframework.http.ResponseEntity;
-import org.springframework.stereotype.Controller;
 import org.springframework.ui.ModelMap;
-import org.springframework.web.bind.ServletRequestUtils;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.*;
 
 import jakarta.servlet.http.HttpServletRequest;
+
 import java.io.IOException;
 import java.net.URLDecoder;
 import java.nio.charset.StandardCharsets;
@@ -37,148 +33,125 @@ import java.util.*;
  * @date 2023-04-16 14:27:07
  */
 @Tag(name = "博客前台页面")
-@Controller
+@RestController
+@RequestMapping("/api/blog")
 public class ForegroundController extends BaseController {
     private final CategoryService categoryService;
     private final ArticleQuery articleQuery;
-    private final HibernateQuery hibernateQuery;
     private final ArticleViewService articleViewService;
-    private final FileService fileService;
     private final JarFileResources jarFileResources;
+    private final HibernateQuery hibernateQuery;
+    private final int pageSize = 10;
 
     public ForegroundController(CategoryService categoryService, ArticleQuery articleQuery,
-                                HibernateQuery hibernateQuery, ArticleViewService articleViewService,
-                                FileService fileService, JarFileResources jarFileResources) {
+                                ArticleViewService articleViewService, JarFileResources jarFileResources,
+                                HibernateQuery hibernateQuery) {
         this.categoryService = categoryService;
         this.articleQuery = articleQuery;
-        this.hibernateQuery = hibernateQuery;
         this.articleViewService = articleViewService;
-        this.fileService = fileService;
         this.jarFileResources = jarFileResources;
+        this.hibernateQuery = hibernateQuery;
     }
 
     @Operation(summary = "前台首页", description = "N")
-    @GetMapping(value = "/")
-    public String index(ModelMap model, HttpServletRequest request) {
-        // 调用 ContentsDirective
-        String order = ServletRequestUtils.getStringParameter(request, "order", "newest");
-        int pageNo = ServletRequestUtils.getIntParameter(request, "pageNo", 1);
-        model.put("order", order);
-        model.put("pageNo", pageNo);
-
-        setBlogChannels(model);
-        return "/classic/index";
+    @GetMapping(value = "/post/list")
+    public String index(@RequestParam("pn") int pageNumber) {
+        Page<UserArticle> page = articleQuery.findUserArticleByPage(pageSize, pageNumber);
+        PageList<UserArticle> pageList = getPageList(page);
+        return WebResult.success(pageList);
+    }
+
+    @Operation(summary = "博客文章内容页面", description = "N")
+    @GetMapping("/post/detail")
+    public String article(@RequestParam("articleId") String articleId) {
+        ArticleVO articleVO = articleQuery.getArticle(articleId);
+        if (articleVO == null) {
+            return WebResult.failWithMsg("not found");
+        }
+
+        articleViewService.incr(articleId);
+        return WebResult.success(articleVO);
+    }
+
+    private PageList<UserArticle> getPageList(Page<UserArticle> page) {
+        int pageNumber = page.getNumber() + 1;
+        int pageSize = page.getSize();
+        long total = page.getTotalElements();
+        List<UserArticle> list = page.getContent();
+        return PageList.pageList(pageNumber, pageSize, (int) total, list);
     }
 
     @Operation(summary = "文章分类列表页面", description = "N")
     @GetMapping("/category")
-    public String category(ModelMap model) {
-        int pn = 1;
-        int ps = 1000;
+    public String category() {
         List<CategoryCount> list = categoryService.findCategoryCountByPage(CategoryType.Category.getValue(), false);
-        model.put("results", list);
-
-        setBlogChannels(model);
-        return "/classic/category/index";
+        return WebResult.success(list);
     }
 
     @Operation(summary = "某个分类下的文章列表页面", description = "N")
-    @GetMapping("/category/{name}")
-    public String category1(@PathVariable("name") String name, ModelMap model) {
-        int pageNo = ServletRequestUtils.getIntParameter(ServletUtil.getRequest(), "pageNo", 1);
-        Page<UserArticle> page = articleQuery.findByCategory(10, pageNo, name, CategoryType.Category.getValue());
-        model.put("results", page);
-        model.put("categoryName", name);
-
-        setBlogChannels(model);
-        return "/classic/category/view";
+    @GetMapping("/category/post")
+    public String category1(@RequestParam("category") String category, @RequestParam("pn") int pageNumber) {
+        Page<UserArticle> page = articleQuery.findByCategory(pageSize, pageNumber, category, CategoryType.Category.getValue());
+        PageList<UserArticle> pageList = getPageList(page);
+        return WebResult.success(pageList);
     }
 
     @Operation(summary = "文章标签列表页面", description = "N")
     @GetMapping("/tag")
-    public String tag(ModelMap model) {
-        int pn = 1;
-        int ps = 1000;
-        List<CategoryCount> list = categoryService.findCategoryCountByPage(CategoryType.Tag.getValue(), false);
-        model.put("results", list);
-
-        setBlogChannels(model);
-        return "/classic/tag/index";
+    public String tag(@RequestParam("type") String type) {
+        int typeInt = 0;
+        if (type.equals("tag")) {
+            typeInt = CategoryType.Tag.getValue();
+        } else if (type.equals("category")) {
+            typeInt = CategoryType.Category.getValue();
+        } else {
+            WebResult.failWithMsg("type invalid");
+        }
+
+        List<CategoryCount> list = categoryService.findCategoryCountByPage(typeInt, false);
+        return WebResult.success(list);
     }
 
     @Operation(summary = "某个标签下的文章列表页面", description = "N")
-    @GetMapping("/tag/{name}")
-    public String tag1(@PathVariable String name, ModelMap model) {
-        int pageNo = ServletRequestUtils.getIntParameter(ServletUtil.getRequest(), "pageNo", 1);
-        Page<UserArticle> page = articleQuery.findByCategory(10, pageNo, name, CategoryType.Tag.getValue());
-        model.put("results", page);
-        model.put("tagName", name);
-
-        setBlogChannels(model);
-        return "/classic/tag/view";
+    @GetMapping("/tag/post")
+    public String tagPost(@RequestParam("type") String type,
+                          @RequestParam("name") String name,
+                          @RequestParam("pn") int pageNumber) {
+        int typeInt = 0;
+        if (type.equals("tag")) {
+            typeInt = CategoryType.Tag.getValue();
+        } else if (type.equals("category")) {
+            typeInt = CategoryType.Category.getValue();
+        } else {
+            WebResult.failWithMsg("type invalid");
+        }
+
+        Page<UserArticle> page = articleQuery.findByCategory(pageSize, pageNumber, name, typeInt);
+        PageList<UserArticle> pageList = getPageList(page);
+        return WebResult.success(pageList);
     }
 
     @Operation(summary = "文章归档列表页面", description = "N")
     @GetMapping("/archive")
-    public String archive(ModelMap model) {
+    public String archive() {
         List<ArchiveArticle> results = articleQuery.getArchiveArticles();
-        model.put("results", results);
-
-        setBlogChannels(model);
-        return "/classic/archive";
-    }
-
-    @Operation(summary = "博客文章内容页面", description = "N")
-    @GetMapping("/post/{articleId}")
-    public String article(@PathVariable("articleId") String articleId, ModelMap model) {
-        ArticleVO articleVO = articleQuery.getArticle(articleId);
-        if (articleVO == null) {
-            model.put("title", "Not Found");
-            model.put("content", "文章不存在");
-            setBlogChannels(model);
-            return "/classic/zzz/notfound";
-        }
-
-        articleViewService.incr(articleId);
-        model.put("view", articleVO);
-        setBlogChannels(model);
-        return "/classic/channel/view";
+        return WebResult.success(results);
     }
 
     @Operation(summary = "文章搜索结果列表页面", description = "N")
     @GetMapping("/search")
-    public String search(String kw, Integer pageNo, ModelMap model) {
-        if (pageNo == null) {
-            pageNo = 1;
-        }
-
-        int pn = pageNo - 1;
-        int ps = 10;
-        Pageable pageable = PageRequest.of(pn, ps, Sort.by(Sort.Direction.ASC, "createTime"));
-        Page<UserArticle> page = hibernateQuery.search(kw, pageable);
-        model.put("results", page);
-        model.put("kw", kw);
-        setBlogChannels(model);
-        return "/classic/search";
+    public String search(@RequestParam("kw") String kw, @RequestParam("pn") int pageNumber) {
+        PageRequest pageRequest = PageRequest.of(pageNumber, pageSize, Sort.by(Sort.Direction.ASC, "createTime"));
+        /*Page<UserArticle> page = hibernateQuery.searchArticle(kw, pageRequest);
+        PageList<UserArticle> pageList = getPageList(page);*/
+        return WebResult.success("pageList");
     }
 
     @Operation(summary = "关于页面", description = "N")
     @GetMapping("/about")
     public String about(ModelMap model) {
         AboutView aboutView = articleQuery.getAboutView();
-        model.put("view", aboutView);
-        setBlogChannels(model);
-        return "/classic/zzz/about";
-    }
-
-    private void setBlogChannels(ModelMap model) {
-        List<BlogChannel> blogChannels = new ArrayList<>();
-        blogChannels.add(new BlogChannel("分类", "category"));
-        blogChannels.add(new BlogChannel("标签", "tag"));
-        blogChannels.add(new BlogChannel("归档", "archive"));
-        blogChannels.add(new BlogChannel("关于", "about"));
-        model.put("searchBox", true);
-        model.put("channels", blogChannels);
+        return WebResult.success(aboutView);
     }
 
     @Operation(summary = "文件访问接口", description = "N")
@@ -189,7 +162,7 @@ public class ForegroundController extends BaseController {
         String uri = servletRequest.getRequestURI();
         String uri1 = URLDecoder.decode(uri, StandardCharsets.UTF_8);
         String objectName =  uri1.replaceFirst("/", "");
-        fileService.getFile(objectName);
+        //fileService.getFile(objectName);
     }
 
     @Operation(summary = "默认头像", description = "N")