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/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..7b92907a 100644 --- a/src/main/java/com/greate/community/controller/DiscussPostController.java +++ b/src/main/java/com/greate/community/controller/DiscussPostController.java @@ -12,13 +12,19 @@ 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; 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 +120,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); @@ -296,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 @@