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] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=A0=87=E9=A2=98=E4=B8=BA?= =?UTF-8?q?=E7=A9=BA=E5=AF=BC=E8=87=B4=E7=9A=84=E6=97=A0=E6=B3=95=E7=82=B9?= =?UTF-8?q?=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);