Parcourir la source

web 模块更改为 restful 服务

reghao il y a 3 mois
Parent
commit
414f284f70

+ 22 - 49
web/src/main/java/cn/reghao/bnt/web/blog/controller/AdminArticleController.java

@@ -6,16 +6,13 @@ import cn.reghao.jutil.jdk.web.result.Result;
 import cn.reghao.jutil.web.WebResult;
 import cn.reghao.bnt.web.blog.service.ArticleQuery;
 import cn.reghao.bnt.web.blog.model.dto.ArticleDto;
-import cn.reghao.bnt.web.blog.model.po.Category;
 import cn.reghao.bnt.web.blog.model.vo.AdminArticle;
 import cn.reghao.bnt.web.blog.model.vo.EditArticle;
 import cn.reghao.bnt.web.blog.service.ArticleService;
-import cn.reghao.bnt.web.blog.service.CategoryService;
 import io.swagger.v3.oas.annotations.tags.Tag;
 import io.swagger.v3.oas.annotations.Operation;
 import org.springframework.data.domain.Page;
 import org.springframework.http.MediaType;
-import org.springframework.ui.ModelMap;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 
@@ -31,27 +28,36 @@ import java.util.List;
 public class AdminArticleController {
     private final ArticleService articleService;
     private final ArticleQuery articleQuery;
-    private final CategoryService categoryService;
     private final HibernateLucene hibernateLucene;
     private int pageSize = 10;
 
     public AdminArticleController(ArticleService articleService, ArticleQuery articleQuery,
-                                  CategoryService categoryService, HibernateLucene hibernateLucene) {
+                                  HibernateLucene hibernateLucene) {
         this.articleService = articleService;
         this.articleQuery = articleQuery;
-        this.categoryService = categoryService;
         this.hibernateLucene = hibernateLucene;
     }
 
     @Operation(summary = "博客文章列表页面", description = "N")
     @GetMapping("/list")
     public String list(@RequestParam("pn") int pageNumber,
-                       @RequestParam("categoryId") int categoryId,
-                       @RequestParam("published") int published,
-                       @RequestParam("title") String title) {
+                       @RequestParam(value = "categoryId", required = false) Integer categoryId,
+                       @RequestParam(value = "published", required = false) Integer published,
+                       @RequestParam(value = "title", required = false) String title) {
+        if (categoryId == null) {
+            categoryId = 0;
+        }
+
+        if (published == null) {
+            published = 0;
+        }
+
+        if (title == null) {
+            title = "";
+        }
+
         Page<AdminArticle> page1 = articleQuery.findAdminArticleByPage(pageSize, pageNumber, categoryId, published, title);
         PageList<AdminArticle> pageList = getPageList(page1);
-        List<Category> list = categoryService.findAllCategory();
         return WebResult.success(pageList);
     }
 
@@ -63,42 +69,15 @@ public class AdminArticleController {
         return PageList.pageList(pageNumber, pageSize, (int) total, list);
     }
 
-    @Operation(summary = "新建/编辑文章页面", description = "N")
+    @Operation(summary = "编辑文章", description = "N")
     @GetMapping("/edit")
-    public String edit(String editor, String articleId, ModelMap model) {
-        if (editor == null) {
-            editor = "markdown";
-        }
-        List<Category> list = categoryService.findAllCategory();
-
-        String pageTitle = "新建文章";
-        String submitTitle = "发布文章";
-        boolean newArticle = true;
-        EditArticle editArticle = null;
-        if (articleId != null) {
-            pageTitle = "编辑文章";
-            submitTitle = "更新文章";
-            newArticle = false;
-            editArticle = articleQuery.getEditArticle(articleId);
-            editor = editArticle.getEditor();
-        }
-
-        model.put("pageTitle", pageTitle);
-        model.put("submitTitle", submitTitle);
-        model.put("newArticle", newArticle);
-        if (newArticle) {
-            model.put("editors", List.of("markdown", "tinymce"));
-        } else {
-            model.put("view", editArticle);
-        }
-        model.put("editor", editor);
-        model.put("categories", list);
-        return "/admin/blog/post/edit";
+    public String edit(@RequestParam("articleId") String articleId) {
+        EditArticle editArticle = articleQuery.getEditArticle(articleId);
+        return WebResult.success(editArticle);
     }
 
     @Operation(summary = "新增/更新文章接口", description = "N")
     @PostMapping(value = "/edit", produces = MediaType.APPLICATION_JSON_VALUE)
-    @ResponseBody
     public String editArticle(@Validated ArticleDto articleDto) {
         String articleId = articleDto.getArticleId();
         Result result;
@@ -113,19 +92,13 @@ public class AdminArticleController {
 
     @Operation(summary = "删除文章", description = "N")
     @PostMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_VALUE)
-    @ResponseBody
-    public String delete(@RequestParam("articleId") List<String> articleIds) {
-        if (articleIds.size() != 1) {
-            return WebResult.failWithMsg("只能删除一篇文章");
-        }
-
-        articleService.deleteArticle(articleIds.get(0));
+    public String delete(@RequestParam("articleId") String articleId) {
+        articleService.deleteArticle(articleId);
         return WebResult.success();
     }
 
     @Operation(summary = "重置文章索引", description = "N")
     @PostMapping(value = "/reset_indexes", produces = MediaType.APPLICATION_JSON_VALUE)
-    @ResponseBody
     public String resetIndexes() {
         hibernateLucene.resetIndexes();
         return WebResult.success();

+ 13 - 15
web/src/main/java/cn/reghao/bnt/web/blog/controller/AdminCategoryController.java

@@ -26,17 +26,19 @@ public class AdminCategoryController {
         this.categoryService = categoryService;
     }
 
-    @Operation(summary = "文章标签列表页面", description = "N")
-    @GetMapping("/tag/list")
-    public String tagList() {
-        List<CategoryCount> list = categoryService.findCategoryCountByPage(CategoryType.Tag.getValue(), true);
-        return WebResult.success(list);
-    }
-
     @Operation(summary = "文章分类列表页面", description = "N")
     @GetMapping("/category/list")
-    public String categoryList() {
-        List<CategoryCount> list = categoryService.findCategoryCountByPage(CategoryType.Category.getValue(), true);
+    public String categoryList(@RequestParam("type") String type) {
+        int type1 = 0;
+        if ("category".equals(type)) {
+            type1 = CategoryType.Category.getValue();
+        } else if ("tag".equals(type)) {
+            type1 = CategoryType.Tag.getValue();
+        } else {
+            return WebResult.failWithMsg("type invalide");
+        }
+
+        List<CategoryCount> list = categoryService.findCategoryCountByPage(type1, true);
         return WebResult.success(list);
     }
 
@@ -49,12 +51,8 @@ public class AdminCategoryController {
 
     @Operation(summary = "删除文章分类接口", description = "N")
     @PostMapping(value = "/category/delete", produces = MediaType.APPLICATION_JSON_VALUE)
-    public String delete(@RequestParam("id") List<Integer> ids) {
-        if (ids.size() != 1) {
-            return WebResult.failWithMsg("删除的分类/标签数量不为 1");
-        }
-
-        Result result = categoryService.deleteCategory(ids.get(0));
+    public String delete(@RequestParam("id") int id) {
+        Result result = categoryService.deleteCategory(id);
         return WebResult.result(result);
     }
 }

+ 5 - 75
web/src/main/java/cn/reghao/bnt/web/blog/controller/AdminQuestionController.java

@@ -1,21 +1,13 @@
 package cn.reghao.bnt.web.blog.controller;
 
-import cn.reghao.bnt.web.blog.model.dto.QuestionUpdateDto;
 import cn.reghao.jutil.jdk.web.db.PageList;
-import cn.reghao.jutil.jdk.web.result.Result;
 import cn.reghao.jutil.jdk.web.result.WebResult;
-import cn.reghao.bnt.web.blog.model.dto.QuestionDto;
-import cn.reghao.bnt.web.blog.model.po.Category;
-import cn.reghao.bnt.web.blog.model.po.Question;
 import cn.reghao.bnt.web.blog.model.vo.QuestionView;
-import cn.reghao.bnt.web.blog.service.CategoryService;
 import cn.reghao.bnt.web.blog.service.QuestionService;
 import io.swagger.v3.oas.annotations.tags.Tag;
 import io.swagger.v3.oas.annotations.Operation;
 import org.springframework.data.domain.Page;
 import org.springframework.http.MediaType;
-import org.springframework.ui.ModelMap;
-import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 
 import java.util.List;
@@ -28,20 +20,20 @@ import java.util.List;
 @RestController
 @RequestMapping("/api/blog/v2/question")
 public class AdminQuestionController {
-    private final CategoryService categoryService;
     private final QuestionService questionService;
     private int pageSize = 100;
 
-    public AdminQuestionController(CategoryService categoryService, QuestionService questionService) {
-        this.categoryService = categoryService;
+    public AdminQuestionController(QuestionService questionService) {
         this.questionService = questionService;
     }
 
     @Operation(summary = "面试题列表页面", description = "N")
     @GetMapping("/list")
     public String list(@RequestParam("pn") int pageNumber,
-                       @RequestParam("categoryId") int categoryId,
-                       @RequestParam("title") String title) {
+                       @RequestParam(value = "categoryId", required = false) Integer categoryId) {
+        if (categoryId == null) {
+            categoryId = 0;
+        }
         Page<QuestionView> page0 = questionService.findQuestionByPage(pageSize, pageNumber, categoryId);
         PageList<QuestionView> pageList = getPageList(page0);
         return WebResult.success(pageList);
@@ -55,47 +47,8 @@ public class AdminQuestionController {
         return PageList.pageList(pageNumber, pageSize, (int) total, list);
     }
 
-    @Operation(summary = "添加面试题页面", description = "N")
-    @GetMapping("/new")
-    public String addArticlePage(String editor, ModelMap model) {
-        if (editor == null) {
-            editor = "markdown";
-        }
-
-        List<Category> list = categoryService.findAllCategory();
-        model.put("editor", editor);
-        model.put("editors", List.of("markdown", "tinymce"));
-        model.put("categories", list);
-        return "/admin/blog/new";
-    }
-
-    @Operation(summary = "添加面试题接口", description = "N")
-    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_VALUE)
-    @ResponseBody
-    public String addArticle(@Validated QuestionDto questionDto) {
-        Result result = questionService.addQuestion(questionDto);
-        return WebResult.result(result);
-    }
-
-    @Operation(summary = "修改面试题页面", description = "N")
-    @GetMapping("/edit")
-    public String edit(String questionId, ModelMap model) {
-        Question question = questionService.findByQuestionId(questionId);
-        model.put("editor", "markdown");
-        model.put("view", question);
-        return "/admin/blog/edit";
-    }
-
-    @Operation(summary = "修改面试题接口", description = "N")
-    @PostMapping(value = "/update")
-    public String updateArticle(@Validated QuestionUpdateDto questionUpdateDto) {
-        questionService.updateQuestion(questionUpdateDto);
-        return "redirect:/bg/blog/interview/list";
-    }
-
     @Operation(summary = "面试题加权重接口", description = "N")
     @PostMapping(value = "/weight/add", produces = MediaType.APPLICATION_JSON_VALUE)
-    @ResponseBody
     public String addWeight(String questionId) {
         questionService.addWeight(questionId);
         return WebResult.success();
@@ -103,31 +56,8 @@ public class AdminQuestionController {
 
     @Operation(summary = "面试题减权重接口", description = "N")
     @PostMapping(value = "/weight/minus", produces = MediaType.APPLICATION_JSON_VALUE)
-    @ResponseBody
     public String minusWeight(String questionId) {
         questionService.minusWeight(questionId);
         return WebResult.success();
     }
-
-    @Operation(summary = "删除面试题接口", description = "N")
-    @PostMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_VALUE)
-    @ResponseBody
-    public String delete(@RequestParam("questionId") List<String> questionIds) {
-        questionService.deleteQuestions(questionIds);
-        return WebResult.success();
-    }
-
-    @Operation(summary = "面试题答案页面", description = "N")
-    @GetMapping("/answer")
-    public String article(@RequestParam("questionId") String questionId, ModelMap model) {
-        QuestionView questionView = questionService.getQuestionView(questionId);
-        if (questionView == null) {
-            model.put("title", "Not Found");
-            model.put("content", "文章不存在");
-            return "/classic/zzz/notfound";
-        }
-
-        model.put("view", questionView);
-        return "/admin/blog/view";
-    }
 }

+ 3 - 6
web/src/main/java/cn/reghao/bnt/web/blog/model/dto/ArticleDto.java

@@ -2,13 +2,10 @@ package cn.reghao.bnt.web.blog.model.dto;
 
 import lombok.Getter;
 import lombok.Setter;
-import org.springframework.format.annotation.DateTimeFormat;
 
 import jakarta.validation.constraints.NotBlank;
 import jakarta.validation.constraints.NotNull;
 import jakarta.validation.constraints.Size;
-import java.io.Serializable;
-import java.time.LocalDateTime;
 import java.util.List;
 
 /**
@@ -17,9 +14,7 @@ import java.util.List;
  */
 @Setter
 @Getter
-public class ArticleDto implements Serializable {
-    private static final long serialVersionUID = 1L;
-
+public class ArticleDto {
     private String articleId;
     private String editor;
     @NotBlank
@@ -32,4 +27,6 @@ public class ArticleDto implements Serializable {
     private List<String> tags;
     @NotNull
     private Boolean published;
+    @NotNull
+    private Integer type;
 }

+ 0 - 29
web/src/main/java/cn/reghao/bnt/web/blog/model/dto/QuestionDto.java

@@ -1,29 +0,0 @@
-package cn.reghao.bnt.web.blog.model.dto;
-
-import lombok.AllArgsConstructor;
-import lombok.Getter;
-import lombok.NoArgsConstructor;
-import lombok.Setter;
-
-import jakarta.validation.constraints.NotBlank;
-import jakarta.validation.constraints.NotNull;
-import java.io.Serializable;
-
-/**
- * @author reghao
- * @date 2023-04-11 11:39:36
- */
-@AllArgsConstructor
-@NoArgsConstructor
-@Setter
-@Getter
-public class QuestionDto implements Serializable {
-    private static final long serialVersionUID = 1L;
-
-    @NotBlank
-    private String title;
-    @NotBlank
-    private String content;
-    @NotNull
-    private String category;
-}

+ 2 - 0
web/src/main/java/cn/reghao/bnt/web/blog/model/po/Article.java

@@ -49,6 +49,7 @@ public class Article extends BaseEntity {
     @Column(nullable = false)
     private LocalDateTime publishAt;
     private transient List<String> tagIds;
+    private Integer weight;
     private Integer owner;
 
     public Article(String articleId, ArticleDto articleDto, String excerpt) {
@@ -60,6 +61,7 @@ public class Article extends BaseEntity {
         this.categoryId = articleDto.getCategoryId();
         this.published = articleDto.getPublished();
         this.publishAt = LocalDateTime.now();
+        this.weight = 0;
         this.owner = UserContext.getUserId();
     }
 }

+ 4 - 4
web/src/main/java/cn/reghao/bnt/web/blog/model/po/Question.java

@@ -1,7 +1,7 @@
 package cn.reghao.bnt.web.blog.model.po;
 
 import cn.reghao.bnt.web.admin.service.UserContext;
-import cn.reghao.bnt.web.blog.model.dto.QuestionDto;
+import cn.reghao.bnt.web.blog.model.dto.ArticleDto;
 import cn.reghao.bnt.web.util.BaseEntity;
 import lombok.Getter;
 import lombok.NoArgsConstructor;
@@ -46,10 +46,10 @@ public class Question extends BaseEntity {
     private LocalDateTime publishAt;
     private Integer owner;
 
-    public Question(String questionId, QuestionDto questionDto, int categoryId) {
+    public Question(String questionId, ArticleDto articleDto, int categoryId) {
         this.questionId = questionId;
-        this.title = questionDto.getTitle();
-        this.content = questionDto.getContent();
+        this.title = articleDto.getTitle();
+        this.content = articleDto.getContent();
         this.categoryId = categoryId;
         this.weight = 0;
         this.publishAt = LocalDateTime.now();

+ 4 - 2
web/src/main/java/cn/reghao/bnt/web/blog/model/vo/EditArticle.java

@@ -18,7 +18,8 @@ public class EditArticle {
     private String content;
     private String editor;
     private Integer categoryId;
-    private String tags;
+    private List<String> tags;
+    private String published;
 
     public EditArticle(Article article, List<String> tags) {
         this.articleId = article.getArticleId();
@@ -26,6 +27,7 @@ public class EditArticle {
         this.content = article.getContent();
         this.editor = article.getEditor();
         this.categoryId = article.getCategoryId();
-        this.tags = tags.toString().replace("[", "").replace("]", "");
+        this.tags = tags;
+        this.published = article.getPublished() ? "1" : "0";
     }
 }

+ 4 - 3
web/src/main/java/cn/reghao/bnt/web/blog/service/ArticleQuery.java

@@ -12,6 +12,7 @@ import cn.reghao.bnt.web.blog.model.vo.*;
 import cn.reghao.bnt.web.util.MarkdownUtil;
 import cn.reghao.jutil.jdk.converter.DateTimeConverter;
 import cn.reghao.bnt.web.blog.db.repository.CategoryRepository;
+import org.springframework.cache.annotation.Cacheable;
 import org.springframework.data.domain.*;
 import org.springframework.data.jpa.domain.Specification;
 import org.springframework.stereotype.Service;
@@ -58,7 +59,7 @@ public class ArticleQuery {
         return new EditArticle(article, tags);
     }
 
-    //@Cacheable(cacheNames = "ArticleVOs", key = "#articleId", unless = "#result == null")
+    @Cacheable(cacheNames = "ArticleVOs", key = "#articleId", unless = "#result == null")
     public ArticleVO getArticle(String articleId) {
         Article article = articleRepository.findByArticleId(articleId);
         if (article == null || !article.getPublished()) {
@@ -85,10 +86,10 @@ public class ArticleQuery {
         ArticleLink next = getNextLink(id);
         articleVO.setPrev(prev);
         articleVO.setNext(next);
-        if ("markdown".equals(articleVO.getEditor())) {
+        /*if ("markdown".equals(articleVO.getEditor())) {
             String html = MarkdownUtil.getHtml(articleVO.getContent());
             articleVO.setContent(html);
-        }
+        }*/
 
         String content = articleVO.getContent();
         content += "<br/><p class=\"copyright\">声明:本文归作者所有,未经作者允许,不得转载</p>";

+ 8 - 1
web/src/main/java/cn/reghao/bnt/web/blog/service/ArticleService.java

@@ -31,16 +31,23 @@ public class ArticleService {
     private final ArticleTagRepository articleTagRepository;
     private final CategoryRepository categoryRepository;
     private final IdGenerator idGenerator;
+    private final QuestionService questionService;
 
     public ArticleService(ArticleRepository articleRepository, ArticleTagRepository articleTagRepository,
-                          CategoryRepository categoryRepository) {
+                          CategoryRepository categoryRepository, QuestionService questionService) {
         this.articleRepository = articleRepository;
         this.articleTagRepository = articleTagRepository;
         this.categoryRepository = categoryRepository;
         this.idGenerator = new IdGenerator("article-id");
+        this.questionService = questionService;
     }
 
     public Result addArticle(ArticleDto articleDto) {
+        int type = articleDto.getType();
+        if (type == 2) {
+            return questionService.addQuestion(articleDto);
+        }
+
         int categoryId = articleDto.getCategoryId();
         Category category = categoryRepository.findById(categoryId).orElse(null);
         if (category == null) {

+ 5 - 8
web/src/main/java/cn/reghao/bnt/web/blog/service/QuestionService.java

@@ -1,5 +1,6 @@
 package cn.reghao.bnt.web.blog.service;
 
+import cn.reghao.bnt.web.blog.model.dto.ArticleDto;
 import cn.reghao.bnt.web.blog.model.dto.QuestionUpdateDto;
 import cn.reghao.bnt.web.blog.model.po.Category;
 import cn.reghao.bnt.web.blog.model.po.Question;
@@ -8,7 +9,6 @@ import cn.reghao.jutil.jdk.web.result.Result;
 import cn.reghao.jutil.jdk.string.IdGenerator;
 import cn.reghao.bnt.web.blog.db.repository.CategoryRepository;
 import cn.reghao.bnt.web.blog.db.repository.QuestionRepository;
-import cn.reghao.bnt.web.blog.model.dto.QuestionDto;
 import cn.reghao.bnt.web.blog.model.vo.QuestionCount;
 import cn.reghao.bnt.web.blog.model.vo.QuestionView;
 import org.springframework.data.domain.*;
@@ -36,18 +36,15 @@ public class QuestionService {
         this.idGenerator = new IdGenerator("question-id");
     }
 
-    public Result addQuestion(QuestionDto questionDto) {
-        String categoryName = questionDto.getCategory();
-        Category category = categoryRepository.findByTypeAndName(1, categoryName);
+    public Result addQuestion(ArticleDto articleDto) {
+        int categoryId = articleDto.getCategoryId();
+        Category category = categoryRepository.findById(categoryId).orElse(null);
         if (category == null) {
             category = new Category();
             categoryRepository.save(category);
         }
-
-        int categoryId = category.getId();
         String questionId = idGenerator.stringId();
-        Question question = new Question(questionId, questionDto, categoryId);
-
+        Question question = new Question(questionId, articleDto, categoryId);
         questionRepository.save(question);
         return Result.success();
     }