Oss上传文件(有bug)
This commit is contained in:
@@ -7,6 +7,7 @@ import com.bao.dating.pojo.entity.Post;
|
|||||||
import com.bao.dating.service.PostService;
|
import com.bao.dating.service.PostService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/posts")
|
@RequestMapping("/posts")
|
||||||
@@ -16,9 +17,9 @@ public class PostController {
|
|||||||
private PostService postService;
|
private PostService postService;
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public Result<Post> createPost(Post post) {
|
public Result<Post> createPost(@RequestBody Post post, MultipartFile file) {
|
||||||
// 调用 Service 层处理发布动态业务逻辑
|
// 调用 Service 层处理发布动态业务逻辑
|
||||||
Post result = postService.createPost(post);
|
Post result = postService.createPost(post);
|
||||||
return Result.success(ResultCode.SUCCESS_REVIEW,"动态发布成功,等待审核。",result);
|
return Result.success(ResultCode.SUCCESS_REVIEW,"动态发布成功,等待审核。",result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -12,9 +12,9 @@ import java.util.List;
|
|||||||
@Data
|
@Data
|
||||||
public class Post {
|
public class Post {
|
||||||
|
|
||||||
private Long postId;
|
private Integer postId;
|
||||||
|
|
||||||
private Long userId;
|
private Integer userId;
|
||||||
|
|
||||||
private String content;
|
private String content;
|
||||||
|
|
||||||
|
|||||||
@@ -11,11 +11,11 @@ import java.time.LocalDateTime;
|
|||||||
@Data
|
@Data
|
||||||
public class PostFavorite {
|
public class PostFavorite {
|
||||||
|
|
||||||
private Long favoriteId;
|
private Integer favoriteId;
|
||||||
|
|
||||||
private Long userId;
|
private Integer userId;
|
||||||
|
|
||||||
private Long postId;
|
private Integer postId;
|
||||||
|
|
||||||
private LocalDateTime createdAt;
|
private LocalDateTime createdAt;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,11 +10,11 @@ import java.time.LocalDateTime;
|
|||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
public class PostLike {
|
public class PostLike {
|
||||||
private Long likeId;
|
private Integer likeId;
|
||||||
|
|
||||||
private Long userId;
|
private Integer userId;
|
||||||
|
|
||||||
private Long postId;
|
private Integer postId;
|
||||||
|
|
||||||
private LocalDateTime createdAt;
|
private LocalDateTime createdAt;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import java.time.LocalDateTime;
|
|||||||
@Data
|
@Data
|
||||||
public class User {
|
public class User {
|
||||||
|
|
||||||
private Long userId;
|
private Integer userId;
|
||||||
|
|
||||||
private String userName;
|
private String userName;
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,11 @@ public class PostServiceImpl implements PostService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Post createPost(Post post) {
|
public Post createPost(Post post) {
|
||||||
|
// 检查并确保userId不为空,如果为空则设置默认值
|
||||||
|
if (post.getUserId() == null) {
|
||||||
|
post.setUserId(1); // 设置默认用户ID为1
|
||||||
|
}
|
||||||
|
|
||||||
// 处理图片和视频上传到 OSS
|
// 处理图片和视频上传到 OSS
|
||||||
List<String> mediaOssKeys = new ArrayList<>();
|
List<String> mediaOssKeys = new ArrayList<>();
|
||||||
|
|
||||||
@@ -62,4 +67,4 @@ public class PostServiceImpl implements PostService {
|
|||||||
postMapper.insert(post);
|
postMapper.insert(post);
|
||||||
return post;
|
return post;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
<mapper namespace="com.bao.dating.mapper.PostMapper">
|
<mapper namespace="com.bao.dating.mapper.PostMapper">
|
||||||
<!-- 动态添加 -->
|
<!-- 动态添加 -->
|
||||||
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
|
<insert id="insert" useGeneratedKeys="true" keyProperty="postId">
|
||||||
INSERT INTO post
|
INSERT INTO post
|
||||||
(user_id, content,
|
(user_id, content,
|
||||||
<if test="mediaOssKeys != null and mediaOssKeys != ''">
|
<if test="mediaOssKeys != null and mediaOssKeys != ''">
|
||||||
@@ -17,9 +17,14 @@
|
|||||||
is_public, like_count, favorite_count, created_at, updated_at)
|
is_public, like_count, favorite_count, created_at, updated_at)
|
||||||
VALUES
|
VALUES
|
||||||
(#{userId}, #{content},
|
(#{userId}, #{content},
|
||||||
|
<if test="mediaOssKeys != null and mediaOssKeys != ''">
|
||||||
#{mediaOssKeys, typeHandler=com.bao.dating.handler.ListToVarcharTypeHandler},
|
#{mediaOssKeys, typeHandler=com.bao.dating.handler.ListToVarcharTypeHandler},
|
||||||
|
</if>
|
||||||
#{tags, typeHandler=com.bao.dating.handler.ListToVarcharTypeHandler},
|
#{tags, typeHandler=com.bao.dating.handler.ListToVarcharTypeHandler},
|
||||||
#{location}, #{isPublic}, 0, 0, #{createdAt}, #{updatedAt})
|
<if test="location != null and location != ''">
|
||||||
|
#{location},
|
||||||
|
</if>
|
||||||
|
#{isPublic}, 0, 0, #{createdAt}, #{updatedAt})
|
||||||
</insert>
|
</insert>
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
Reference in New Issue
Block a user