From 2a29da6e82ae66aa7afd9606d21c152dc55cb33f Mon Sep 17 00:00:00 2001 From: Novocaine <38857631+Meizuamy@users.noreply.github.com> Date: Wed, 1 Sep 2021 18:38:37 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=A0=87=E9=A2=98?= =?UTF-8?q?=E4=B8=BA=E7=A9=BA=E5=AF=BC=E8=87=B4=E7=9A=84=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E7=82=B9=E5=87=BB=E6=9F=A5=E7=9C=8B=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sql/init_discuss_post.sql | 4 ++-- .../community/controller/DiscussPostController.java | 9 +++------ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/sql/init_discuss_post.sql b/sql/init_discuss_post.sql index 405f2e7e..b7a61d9d 100644 --- a/sql/init_discuss_post.sql +++ b/sql/init_discuss_post.sql @@ -2,8 +2,8 @@ DROP TABLE IF EXISTS `discuss_post`; SET character_set_client = utf8mb4 ; CREATE TABLE `discuss_post` ( `id` int(11) NOT NULL AUTO_INCREMENT, - `user_id` int(11) DEFAULT NULL, - `title` varchar(100) DEFAULT NULL, + `user_id` int(11) NOT NULL, + `title` varchar(100) NOT NULL, `content` text, `type` int(11) DEFAULT NULL COMMENT '0-普通; 1-置顶;', `status` int(11) DEFAULT NULL COMMENT '0-正常; 1-精华; 2-拉黑;', diff --git a/src/main/java/com/greate/community/controller/DiscussPostController.java b/src/main/java/com/greate/community/controller/DiscussPostController.java index 4dda60ca..e995385a 100644 --- a/src/main/java/com/greate/community/controller/DiscussPostController.java +++ b/src/main/java/com/greate/community/controller/DiscussPostController.java @@ -13,12 +13,14 @@ import com.greate.community.util.RedisKeyUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.util.HtmlUtils; +import javax.validation.constraints.NotEmpty; import java.io.File; import java.util.*; @@ -114,12 +116,7 @@ public class DiscussPostController implements CommunityConstant { */ @PostMapping("/add") @ResponseBody - public String addDiscussPost(String title, String content) { - User user = hostHolder.getUser(); - if (user == null) { - return CommunityUtil.getJSONString(403, "您还未登录"); - } - + public String addDiscussPost(@NotEmpty(message = "文章标题不能为空") String title, String content, @AuthenticationPrincipal User user) { DiscussPost discussPost = new DiscussPost(); discussPost.setUserId(user.getId()); discussPost.setTitle(title); From cbbc633697241c0e4f1349b1045926a4b1f6feb1 Mon Sep 17 00:00:00 2001 From: Novocaine <38857631+Meizuamy@users.noreply.github.com> Date: Thu, 2 Sep 2021 13:26:07 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E6=96=87=E7=AB=A0=E6=81=A2=E5=A4=8D=E5=8A=9F=E8=83=BD=EF=BC=8C?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=83=A8=E5=88=86=E5=B8=B8=E8=A7=81=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E8=A7=A3=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/Questions.md | 14 +++ .../controller/DiscussPostController.java | 30 ++++++ .../controller/advice/ExceptionAdvice.java | 9 +- .../community/dao/DiscussPostMapper.java | 10 ++ .../community/service/DiscussPostService.java | 14 +++ .../resources/mapper/discusspost-mapper.xml | 13 +++ src/main/resources/static/js/discuss.js | 18 ++++ src/main/resources/templates/index.html | 1 + .../templates/site/admin/onDelete.html | 102 ++++++++++++++++++ .../templates/site/discuss-detail.html | 4 +- 10 files changed, 210 insertions(+), 5 deletions(-) create mode 100644 docs/Questions.md create mode 100644 src/main/resources/templates/site/admin/onDelete.html diff --git a/docs/Questions.md b/docs/Questions.md new file mode 100644 index 00000000..adb9e72a --- /dev/null +++ b/docs/Questions.md @@ -0,0 +1,14 @@ +# 常见问题 + +## Elasticsearch相关 + +#### 1. none of the configured nodes are available + +通常为es的配置问题,请检查配置文件中es相关的配置设置,是否和本地搭建的环境一致。 +```yaml +# es +# 可以通过es的config目录下的配置文件 elasticsearch.yml 获取自己集群配置的cluster-name +spring.data.elasticsearch.cluster-name = docker-cluster +# 服务器的ip、端口 +spring.data.elasticsearch.cluster-nodes = 127.0.0.1:9300 +``` \ No newline at end of file diff --git a/src/main/java/com/greate/community/controller/DiscussPostController.java b/src/main/java/com/greate/community/controller/DiscussPostController.java index e995385a..7b92907a 100644 --- a/src/main/java/com/greate/community/controller/DiscussPostController.java +++ b/src/main/java/com/greate/community/controller/DiscussPostController.java @@ -12,7 +12,11 @@ import com.greate.community.util.HostHolder; import com.greate.community.util.RedisKeyUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -293,5 +297,31 @@ public class DiscussPostController implements CommunityConstant { return CommunityUtil.getJSONString(0); } + @PostMapping("/recover") + @ResponseBody + @PreAuthorize(AUTHORITY_ADMIN) + public String recover(int id){ + discussPostService.updateStatus(id,0); + Event event = new Event() + .setTopic(TOPIC_PUBLISH) + .setUserId(hostHolder.getUser().getId()) + .setEntityType(ENTITY_TYPE_POST) + .setEntityId(id); + eventProducer.fireEvent(event); + + return CommunityUtil.getJSONString(200,"恢复成功"); + } + + + @RequestMapping("/onDelete") + @PreAuthorize(AUTHORITY_ADMIN) + public String onDelete(Model model, + @RequestParam(value = "pageIndex", defaultValue = "0") int pageIndex, + @RequestParam(value = "pageSize", defaultValue = "10")int size){ + Pageable page = PageRequest.of(pageIndex,size); + PageImpl posts = discussPostService.onDelete(page); + model.addAttribute("posts",posts); + return "site/admin/onDelete"; + } } diff --git a/src/main/java/com/greate/community/controller/advice/ExceptionAdvice.java b/src/main/java/com/greate/community/controller/advice/ExceptionAdvice.java index 52ca9f3d..4a8bdd32 100644 --- a/src/main/java/com/greate/community/controller/advice/ExceptionAdvice.java +++ b/src/main/java/com/greate/community/controller/advice/ExceptionAdvice.java @@ -22,10 +22,11 @@ public class ExceptionAdvice { @ExceptionHandler({Exception.class}) public void handleException(Exception e, HttpServletRequest request, HttpServletResponse response) throws IOException { - logger.error("服务器发生异常:" + e.getMessage()); - for (StackTraceElement element : e.getStackTrace()) { - logger.error(element.toString()); - } +// logger.error("服务器发生异常:" + e.getMessage()); + logger.error(e.getMessage(), e); +// for (StackTraceElement element : e.getStackTrace()) { +// logger.error(element.toString()); +// } // 区分异步请求和普通请求 String xRequestedWith = request.getHeader("x-requested-with"); if ("XMLHttpRequest".equals(xRequestedWith)) { diff --git a/src/main/java/com/greate/community/dao/DiscussPostMapper.java b/src/main/java/com/greate/community/dao/DiscussPostMapper.java index 3f656f28..9c21b8a6 100644 --- a/src/main/java/com/greate/community/dao/DiscussPostMapper.java +++ b/src/main/java/com/greate/community/dao/DiscussPostMapper.java @@ -74,4 +74,14 @@ public interface DiscussPostMapper { * @return */ int updateScore(int id, double score); + + /** + * 查询已经删除的帖子 + * @param offset + * @param limit + * @return + */ + List selectDiscussPostByStatus(long offset, int limit); + + int countDiscussPostByStatus(); } diff --git a/src/main/java/com/greate/community/service/DiscussPostService.java b/src/main/java/com/greate/community/service/DiscussPostService.java index a3e5048c..db2e5af7 100644 --- a/src/main/java/com/greate/community/service/DiscussPostService.java +++ b/src/main/java/com/greate/community/service/DiscussPostService.java @@ -5,6 +5,7 @@ import com.github.benmanes.caffeine.cache.Caffeine; import com.github.benmanes.caffeine.cache.LoadingCache; import com.greate.community.dao.DiscussPostMapper; import com.greate.community.entity.DiscussPost; +import com.greate.community.entity.Page; import com.greate.community.util.SensitiveFilter; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; @@ -12,6 +13,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.web.util.HtmlUtils; @@ -201,4 +204,15 @@ public class DiscussPostService { return discussPostMapper.updateScore(id, score); } + /** + * 查询已经删除的帖子 + * @param page + * @return + */ + public PageImpl onDelete(Pageable page) { + List discussPosts = discussPostMapper.selectDiscussPostByStatus(page.getOffset(),page.getPageSize()); + long total = discussPostMapper.countDiscussPostByStatus(); + return new PageImpl(discussPosts, page, total); + } + } diff --git a/src/main/resources/mapper/discusspost-mapper.xml b/src/main/resources/mapper/discusspost-mapper.xml index 05e2b977..1ef1e0f6 100644 --- a/src/main/resources/mapper/discusspost-mapper.xml +++ b/src/main/resources/mapper/discusspost-mapper.xml @@ -53,6 +53,19 @@ where id = #{id} + + + + + + update discuss_post set comment_count = #{commentCount} diff --git a/src/main/resources/static/js/discuss.js b/src/main/resources/static/js/discuss.js index 547c490e..978e7edc 100644 --- a/src/main/resources/static/js/discuss.js +++ b/src/main/resources/static/js/discuss.js @@ -2,6 +2,7 @@ $(function(){ $("#topBtn").click(updateTop); $("#wonderfulBtn").click(setWonderful); $("#deleteBtn").click(setDelete); + $("#recoverBtn").click(recover); }); // 点赞 @@ -79,4 +80,21 @@ function setDelete() { } } ) +} + +function recover() { + $.post( + CONTEXT_PATH + "/discuss/recover", + {"id":$("#postId").val()}, + function (data) { + data = $.parseJSON(data); + if (data.code == 200) { + // 删除成功后,跳转到首页 + location.href = CONTEXT_PATH + "/index"; + } + else { + alert(data.msg); + } + } + ) } \ No newline at end of file diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index 94809fab..aabdb703 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -48,6 +48,7 @@