forked from iVMiku/guidance-backend
minio
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
package com.ivmiku.tutorial.service;
|
||||
|
||||
import com.ivmiku.tutorial.entity.Comment;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 评论服务接口,定义评论相关的业务操作方法。
|
||||
*/
|
||||
public interface CommentService extends IService<Comment> {
|
||||
/**
|
||||
* 保存评论。
|
||||
* 将评论数据保存到数据库。
|
||||
*
|
||||
* @param comment 要保存的评论数据
|
||||
*/
|
||||
|
||||
void createComment(Comment comment);
|
||||
|
||||
/**
|
||||
* 根据ID获取评论详情。
|
||||
* 从数据库中根据评论ID获取评论的详细信息。
|
||||
*
|
||||
* @param commentId 评论的唯一标识ID
|
||||
* @return Comment 返回查询到的评论对象
|
||||
*/
|
||||
|
||||
Comment getCommentById(Long commentId);
|
||||
|
||||
/**
|
||||
* 更新评论。
|
||||
* 根据评论ID和用户ID更新评论内容。
|
||||
*
|
||||
* @param commentId 要更新的评论的唯一标识ID
|
||||
* @param userId 更新评论的用户ID
|
||||
* @param comment 更新后的评论数据
|
||||
*/
|
||||
|
||||
void updateComment(Long commentId, String userId, Comment comment);
|
||||
/**
|
||||
* 删除评论。
|
||||
* 根据评论ID删除指定的评论。
|
||||
*
|
||||
* @param commentId 要删除的评论的唯一标识ID
|
||||
* @param userId 删除评论的用户ID
|
||||
*/
|
||||
|
||||
void deleteComment(Long commentId, String userId);
|
||||
|
||||
List<Comment> getPostComments(Long postId);
|
||||
|
||||
List<Comment> getCommentReplies(Long commentId);
|
||||
|
||||
|
||||
/**
|
||||
* 回复评论。
|
||||
* 用户通过POST请求提交回复数据,服务端接收并保存回复。
|
||||
*
|
||||
* @param parentCommentId 父评论的ID
|
||||
* @param postId 帖子的ID
|
||||
* @param content 回复内容
|
||||
* @param mentionedUserId (可选)@的用户ID
|
||||
* @return Result 返回操作结果
|
||||
*/
|
||||
void createReply(Long parentCommentId, Long postId, String content, String mentionedUserId);
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.ivmiku.tutorial.service;
|
||||
|
||||
import com.ivmiku.tutorial.entity.Community;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface CommunityService extends IService<Community> {
|
||||
|
||||
/**
|
||||
* 创建社区。
|
||||
* 根据社区信息创建一个新的社区记录。
|
||||
*
|
||||
* @param community 社区信息
|
||||
*/
|
||||
void createCommunity(Community community);
|
||||
|
||||
/**
|
||||
* 更新社区信息。
|
||||
* 根据社区ID更新社区的相关信息。
|
||||
*
|
||||
* @param id 社区的唯一标识ID
|
||||
* @param community 更新后的社区信息
|
||||
*/
|
||||
void updateCommunity(Long id, Community community);
|
||||
|
||||
/**
|
||||
* 删除社区。
|
||||
* 根据社区ID删除指定的社区记录。
|
||||
*
|
||||
* @param id 社区的唯一标识ID
|
||||
*/
|
||||
void deleteCommunity(Long id);
|
||||
|
||||
/**
|
||||
* 根据社区名称获取社区信息。
|
||||
*
|
||||
* @param name 社区名称
|
||||
* @return 社区信息
|
||||
*/
|
||||
List<Community> getByName(String name);
|
||||
|
||||
/**
|
||||
* 获取社区列表。
|
||||
*
|
||||
* @return 社区列表
|
||||
*/
|
||||
List<Community> getCommunityList();
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.ivmiku.tutorial.service;
|
||||
|
||||
import com.ivmiku.tutorial.response.Result;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.UUID;
|
||||
|
||||
public interface FileService {
|
||||
|
||||
String uploadMinio(MultipartFile file) throws Exception;
|
||||
|
||||
Result downLoadMinio(String url, HttpServletResponse response) throws Exception;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.ivmiku.tutorial.service;
|
||||
|
||||
/**
|
||||
* InteractionService接口定义用户对帖子和评论的收藏与点赞操作。
|
||||
*/
|
||||
public interface InteractionService {
|
||||
void favoritePost(String userOpenid, Long postId);
|
||||
void unfavoritePost(String userOpenid, Long postId);
|
||||
void likePost(String userOpenid, Long postId);
|
||||
void unlikePost(String userOpenid, Long postId);
|
||||
void likeComment(String userOpenid, Long commentId);
|
||||
void unlikeComment(String userOpenid, Long commentId);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.ivmiku.tutorial.service;
|
||||
|
||||
import com.ivmiku.tutorial.entity.Post;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface PostService extends IService<Post> {
|
||||
void createPost(Post post);
|
||||
Post getPostById(Long postId);
|
||||
void updatePost(Long postId, String userId, Post post);
|
||||
void deletePost(Long postId, String userId);
|
||||
|
||||
List<Post> getPostList(String userId);
|
||||
|
||||
List<Post> getCommunityPostList(Long communityId);
|
||||
|
||||
List<Post> getOfficialPosts(); // 新增获取官方帖子的方法
|
||||
|
||||
List<Post> getNonOfficialPosts(); // 新增获取非官方帖子的方法
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.ivmiku.tutorial.service;
|
||||
|
||||
import com.ivmiku.tutorial.entity.Post;
|
||||
import com.ivmiku.tutorial.entity.PostTag;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.ivmiku.tutorial.entity.Tag;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface PostTagService extends IService<PostTag> {
|
||||
/**
|
||||
* 创建帖子标签。
|
||||
* 将帖子标签数据保存到数据库。
|
||||
*
|
||||
* @param postTag 要保存的帖子标签数据
|
||||
*/
|
||||
|
||||
void createPostTag(PostTag postTag);
|
||||
|
||||
/**
|
||||
* 根据ID获取帖子标签详情。
|
||||
* 从数据库中根据帖子标签ID获取帖子标签的详细信息。
|
||||
*
|
||||
* @param postTagId 帖子标签的唯一标识ID
|
||||
* @return PostTag 返回查询到的帖子标签对象
|
||||
*/
|
||||
|
||||
PostTag getPostTagById(Long postTagId);
|
||||
|
||||
/**
|
||||
* 更新帖子标签。
|
||||
* 根据帖子标签ID更新帖子标签的内容。
|
||||
*
|
||||
* @param postTagId 要更新的帖子标签的唯一标识ID
|
||||
* @param postTag 更新后的帖子标签数据
|
||||
*/
|
||||
|
||||
void updatePostTag(Long postTagId, PostTag postTag);
|
||||
|
||||
/**
|
||||
* 删除帖子标签。
|
||||
* 根据帖子标签ID删除指定的帖子标签。
|
||||
*
|
||||
* @param postTagId 要删除的帖子标签的唯一标识ID
|
||||
*/
|
||||
|
||||
void deletePostTag(Long postTagId);
|
||||
|
||||
List<Tag> getPostTagList(Long postId);
|
||||
|
||||
List<Post> getTagPostList(Long tagId);
|
||||
|
||||
List<Tag> getSortedTagList();
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.ivmiku.tutorial.service;
|
||||
|
||||
import com.ivmiku.tutorial.entity.Tag;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface TagService extends IService<Tag> {
|
||||
/**
|
||||
* 创建标签
|
||||
* @param tag 标签实体对象
|
||||
*/
|
||||
void createTag(Tag tag);
|
||||
|
||||
/**
|
||||
* 根据ID获取标签
|
||||
* @param tagId 标签ID
|
||||
* @return 标签实体对象
|
||||
*/
|
||||
Tag getTagById(Long tagId);
|
||||
|
||||
/**
|
||||
* 更新标签信息
|
||||
* @param tagId 标签ID
|
||||
* @param tag 更新后的标签实体对象
|
||||
*/
|
||||
void updateTag(Long tagId, Tag tag);
|
||||
|
||||
/**
|
||||
* 删除标签
|
||||
* @param tagId 标签ID
|
||||
*/
|
||||
|
||||
void deleteTag(Long tagId);
|
||||
|
||||
List<Tag> getTagList();
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
package com.ivmiku.tutorial.service.impl;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.ivmiku.tutorial.entity.Comment;
|
||||
import com.ivmiku.tutorial.mapper.CommentMapper;
|
||||
import com.ivmiku.tutorial.service.CommentService;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 评论服务实现类。
|
||||
*/
|
||||
@Service
|
||||
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
|
||||
|
||||
// 日志记录器,用于记录服务层的日志信息
|
||||
private static final Logger logger = LoggerFactory.getLogger(CommentServiceImpl.class);
|
||||
|
||||
@Autowired
|
||||
protected CommentMapper commentMapper;
|
||||
|
||||
/**
|
||||
* 保存评论。
|
||||
* 将评论数据保存到数据库。
|
||||
*
|
||||
* @param comment 要保存的评论数据
|
||||
*/
|
||||
@Override
|
||||
public void createComment(Comment comment) {
|
||||
logger.info("开始保存评论数据");
|
||||
comment.setCreatedAt(new Timestamp(System.currentTimeMillis()));
|
||||
comment.setUpdatedAt(new Timestamp(System.currentTimeMillis()));
|
||||
comment.setIsDeleted(0);
|
||||
|
||||
save(comment);
|
||||
logger.info("评论数据保存成功,评论ID:{}", comment.getCommentId());
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID获取评论详情。
|
||||
* 从数据库中根据评论ID获取评论的详细信息。
|
||||
*
|
||||
* @param commentId 评论的唯一标识ID
|
||||
* @return Comment 返回查询到的评论对象
|
||||
*/
|
||||
@Override
|
||||
public Comment getCommentById(Long commentId) {
|
||||
logger.info("开始根据ID获取评论详情,评论ID:{}", commentId);
|
||||
return getById(commentId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新评论。
|
||||
* 根据评论ID和用户ID更新评论内容。
|
||||
*
|
||||
* @param commentId 要更新的评论的唯一标识ID
|
||||
* @param userId 更新评论的用户ID
|
||||
* @param comment 更新后的评论数据
|
||||
*/
|
||||
@Override
|
||||
public void updateComment(Long commentId, String userId, Comment comment) {
|
||||
logger.info("用户ID:{}开始更新评论,评论ID:{}", userId, commentId);
|
||||
comment.setCommentId(commentId);
|
||||
comment.setUpdatedAt(new Timestamp(System.currentTimeMillis()));
|
||||
updateById(comment);
|
||||
logger.info("用户ID:{}的评论ID:{}更新成功", userId, commentId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除评论。
|
||||
* 根据评论ID删除指定的评论。
|
||||
*
|
||||
* @param commentId 要删除的评论的唯一标识ID
|
||||
* @param userId 删除评论的用户ID
|
||||
*/
|
||||
@Override
|
||||
public void deleteComment(Long commentId, String userId) {
|
||||
logger.info("用户ID:{}开始删除评论,评论ID:{}", userId, commentId);
|
||||
Comment comment = getById(commentId);
|
||||
if (comment != null) {
|
||||
comment.setIsDeleted(1); // 标记为已删除
|
||||
comment.setUpdatedAt(new Timestamp(System.currentTimeMillis()));
|
||||
updateById(comment);
|
||||
logger.info("用户ID:{}的评论ID:{}删除成功", userId, commentId);
|
||||
} else {
|
||||
logger.warn("尝试删除的评论ID:{}不存在", commentId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取帖子下的所有评论。
|
||||
*
|
||||
* @param postId 帖子的唯一标识ID
|
||||
* @return List<Comment> 返回评论列表
|
||||
*/
|
||||
@Override
|
||||
public List<Comment> getPostComments(Long postId) {
|
||||
logger.info("开始获取帖子ID:{}的评论列表", postId);
|
||||
LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(Comment::getPostId, postId).eq(Comment::getIsDeleted, 0);
|
||||
List<Comment> comments = commentMapper.selectList(wrapper);
|
||||
if (comments != null) {
|
||||
logger.info("获取帖子ID:{}的评论列表成功", postId);
|
||||
} else {
|
||||
logger.warn("帖子ID:{}的评论列表为空", postId);
|
||||
}
|
||||
return comments;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取评论下的所有回复。
|
||||
*
|
||||
* @param commentId 评论的唯一标识ID
|
||||
* @return List<Comment> 返回回复列表
|
||||
*/
|
||||
@Override
|
||||
public List<Comment> getCommentReplies(Long commentId) {
|
||||
logger.info("开始获取评论ID:{}的回复列表", commentId);
|
||||
LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(Comment::getParentCommentId, commentId).eq(Comment::getIsDeleted, 0);
|
||||
List<Comment> replies = commentMapper.selectList(wrapper);
|
||||
if (replies != null) {
|
||||
logger.info("获取评论ID:{}的回复列表成功", commentId);
|
||||
} else {
|
||||
logger.warn("评论ID:{}的回复列表为空", commentId);
|
||||
}
|
||||
return replies;
|
||||
}
|
||||
/**
|
||||
* 回复评论。
|
||||
* 用户通过POST请求提交回复数据,服务端接收并保存回复。
|
||||
*
|
||||
* @param parentCommentId 父评论的ID
|
||||
* @param postId 帖子的ID
|
||||
* @param content 回复内容
|
||||
* @param mentionedUserId (可选)@的用户ID
|
||||
* @return Result 返回操作结果
|
||||
*/
|
||||
@Override
|
||||
public void createReply(Long parentCommentId, Long postId, String content, String mentionedUserId) {
|
||||
logger.info("回复评论请求开始,父评论ID:{},帖子ID:{}", parentCommentId, postId);
|
||||
|
||||
// 检查父评论是否存在
|
||||
Comment parentComment = getCommentById(parentCommentId);
|
||||
if (parentComment == null) {
|
||||
logger.warn("父评论ID:{}不存在", parentCommentId);
|
||||
throw new RuntimeException("父评论不存在");
|
||||
}
|
||||
|
||||
// 创建回复评论对象
|
||||
Comment reply = new Comment();
|
||||
reply.setUserOpenid(StpUtil.getLoginIdAsString());
|
||||
reply.setParentCommentId(parentCommentId);
|
||||
reply.setPostId(postId);
|
||||
reply.setContent(content);
|
||||
if (mentionedUserId != null) {
|
||||
reply.setMentionedUserId(mentionedUserId);
|
||||
}
|
||||
createComment(reply);
|
||||
|
||||
logger.info("评论回复成功,评论ID:{}", reply.getCommentId());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package com.ivmiku.tutorial.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.ivmiku.tutorial.entity.Community;
|
||||
import com.ivmiku.tutorial.mapper.CommunityMapper;
|
||||
import com.ivmiku.tutorial.response.Result;
|
||||
import com.ivmiku.tutorial.service.CommunityService;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 社区服务实现类。
|
||||
*/
|
||||
@Service
|
||||
public class CommunityServiceImpl extends ServiceImpl<CommunityMapper, Community> implements CommunityService {
|
||||
|
||||
// 日志记录器,用于记录服务层的日志信息
|
||||
private static final Logger logger = LoggerFactory.getLogger(CommunityServiceImpl.class);
|
||||
|
||||
@Autowired
|
||||
private CommunityMapper communityMapper;
|
||||
|
||||
/**
|
||||
* 创建社区。
|
||||
* 根据社区信息创建一个新的社区记录。
|
||||
*
|
||||
* @param community 社区信息
|
||||
*/
|
||||
@Override
|
||||
public void createCommunity(Community community) {
|
||||
logger.info("开始创建社区");
|
||||
save(community);
|
||||
logger.info("社区创建成功,社区ID:{}", community.getCommunityId());
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新社区信息。
|
||||
* 根据社区ID更新社区的相关信息。
|
||||
*
|
||||
* @param id 社区的唯一标识ID
|
||||
* @param community 更新后的社区信息
|
||||
*/
|
||||
@Override
|
||||
public void updateCommunity(Long id, Community community) {
|
||||
logger.info("开始更新社区信息,社区ID:{}", id);
|
||||
community.setCommunityId(id);
|
||||
updateById(community);
|
||||
logger.info("社区信息更新成功,社区ID:{}", id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除社区。
|
||||
* 根据社区ID删除指定的社区记录。
|
||||
*
|
||||
* @param id 社区的唯一标识ID
|
||||
*/
|
||||
@Override
|
||||
public void deleteCommunity(Long id) {
|
||||
logger.info("开始删除社区,社区ID:{}", id);
|
||||
removeById(id);
|
||||
logger.info("社区删除成功,社区ID:{}", id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据社区名称获取社区信息。
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public List<Community> getByName(String name) {
|
||||
LambdaQueryWrapper<Community> wrapper = new LambdaQueryWrapper<>();
|
||||
//根据社区名称模糊查询
|
||||
wrapper.like(Community::getName, name);
|
||||
List<Community> communitys = communityMapper.selectList(wrapper);
|
||||
return communitys;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取社区列表
|
||||
* @return 社区列表
|
||||
*/
|
||||
@Override
|
||||
public List<Community> getCommunityList() {
|
||||
return communityMapper.selectList(null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.ivmiku.tutorial.service.impl;
|
||||
|
||||
|
||||
import com.ivmiku.tutorial.response.Result;
|
||||
import com.ivmiku.tutorial.service.FileService;
|
||||
import com.ivmiku.tutorial.util.MinioClientUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
public class FileServiceImpl implements FileService {
|
||||
@Autowired
|
||||
private MinioClientUtil minioClientUtil;
|
||||
|
||||
@Value("${minio.endpoint}")
|
||||
private String endpoint;
|
||||
|
||||
@Value("${minio.port}")
|
||||
private String port;
|
||||
|
||||
@Value("${minio.bucketName}")
|
||||
private String bucketName;
|
||||
|
||||
@Override
|
||||
public String uploadMinio(MultipartFile file) throws Exception {
|
||||
String filename = file.getOriginalFilename();
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
String imgType = filename.substring(filename.lastIndexOf("."));
|
||||
String fileName = uuid + imgType;
|
||||
|
||||
boolean flag = minioClientUtil.putObject(fileName, file.getInputStream());
|
||||
String path = "http://" + endpoint + ":" + port + "/" + bucketName + "/" + fileName;
|
||||
return flag ? path : "上传失败";
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Result downLoadMinio(String url, HttpServletResponse response) throws Exception {
|
||||
String trim = url.trim();
|
||||
String path = trim.substring(trim.indexOf("/", 1), trim.length());
|
||||
minioClientUtil.getObject(path, response);
|
||||
return Result.ok();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package com.ivmiku.tutorial.service.impl;
|
||||
|
||||
import com.ivmiku.tutorial.entity.Favorite;
|
||||
import com.ivmiku.tutorial.entity.Likee;
|
||||
import com.ivmiku.tutorial.mapper.FavoriteMapper;
|
||||
import com.ivmiku.tutorial.mapper.LikeMapper;
|
||||
import com.ivmiku.tutorial.service.InteractionService;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* InteractionServiceImpl 实现了 InteractionService 接口,处理用户对帖子和评论的收藏与点赞操作。
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class InteractionServiceImpl implements InteractionService {
|
||||
|
||||
@Resource
|
||||
private FavoriteMapper favoriteMapper;
|
||||
|
||||
@Resource
|
||||
private LikeMapper likeMapper;
|
||||
|
||||
@Override
|
||||
public void favoritePost(String userOpenid, Long postId) {
|
||||
log.info("User {} is favoriting post {}", userOpenid, postId);
|
||||
Favorite favorite = new Favorite();
|
||||
favorite.setUserOpenid(userOpenid);
|
||||
favorite.setPostId(postId);
|
||||
favorite.setIsDeleted(0);
|
||||
favoriteMapper.insert(favorite);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unfavoritePost(String userOpenid, Long postId) {
|
||||
log.info("User {} is unfavoriting post {}", userOpenid, postId);
|
||||
QueryWrapper<Favorite> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("user_openid", userOpenid)
|
||||
.eq("post_id", postId)
|
||||
.eq("is_deleted", 0);
|
||||
Favorite favorite = favoriteMapper.selectOne(queryWrapper);
|
||||
if (favorite != null) {
|
||||
favorite.setIsDeleted(1);
|
||||
favoriteMapper.updateById(favorite);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void likePost(String userOpenid, Long postId) {
|
||||
log.info("User {} is liking post {}", userOpenid, postId);
|
||||
Likee likee = new Likee();
|
||||
likee.setUserOpenid(userOpenid);
|
||||
likee.setPostId(postId);
|
||||
likee.setIsDeleted(0);
|
||||
likeMapper.insert(likee);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unlikePost(String userOpenid, Long postId) {
|
||||
log.info("User {} is unliking post {}", userOpenid, postId);
|
||||
QueryWrapper<Likee> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("user_openid", userOpenid)
|
||||
.eq("post_id", postId)
|
||||
.eq("is_deleted", 0);
|
||||
Likee likee = likeMapper.selectOne(queryWrapper);
|
||||
if (likee != null) {
|
||||
likee.setIsDeleted(1);
|
||||
likeMapper.updateById(likee);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void likeComment(String userOpenid, Long commentId) {
|
||||
log.info("User {} is liking comment {}", userOpenid, commentId);
|
||||
Likee likee = new Likee();
|
||||
likee.setUserOpenid(userOpenid);
|
||||
likee.setCommentId(commentId);
|
||||
likee.setIsDeleted(0);
|
||||
likeMapper.insert(likee);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unlikeComment(String userOpenid, Long commentId) {
|
||||
log.info("User {} is unliking comment {}", userOpenid, commentId);
|
||||
QueryWrapper<Likee> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("user_openid", userOpenid)
|
||||
.eq("comment_id", commentId)
|
||||
.eq("is_deleted", 0);
|
||||
Likee likee = likeMapper.selectOne(queryWrapper);
|
||||
if (likee != null) {
|
||||
likee.setIsDeleted(1);
|
||||
likeMapper.updateById(likee);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package com.ivmiku.tutorial.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.ivmiku.tutorial.entity.Post;
|
||||
import com.ivmiku.tutorial.mapper.PostMapper;
|
||||
import com.ivmiku.tutorial.service.PostService;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements PostService {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(PostServiceImpl.class);
|
||||
|
||||
@Autowired
|
||||
private PostMapper postMapper;
|
||||
|
||||
@Override
|
||||
public void createPost(Post post) {
|
||||
logger.info("开始创建帖子");
|
||||
post.setCreatedAt(new Timestamp(System.currentTimeMillis())); // 设置创建时间
|
||||
post.setUpdatedAt(new Timestamp(System.currentTimeMillis())); // 设置更新时间
|
||||
post.setIsDeleted(0); // 初始化为未删除
|
||||
save(post);
|
||||
logger.info("帖子创建成功,帖子ID:{}", post.getPostId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Post getPostById(Long postId) {
|
||||
logger.info("开始根据ID获取帖子详情,帖子ID:{}", postId);
|
||||
return getById(postId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updatePost(Long postId, String userId, Post post) {
|
||||
logger.info("用户ID:{}开始更新帖子,帖子ID:{}", userId, postId);
|
||||
post.setPostId(postId);
|
||||
post.setUpdatedAt(new Timestamp(System.currentTimeMillis())); // 更新修改时间
|
||||
updateById(post);
|
||||
logger.info("用户ID:{}的帖子ID:{}更新成功", userId, postId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deletePost(Long postId, String userId) {
|
||||
logger.info("用户ID:{}开始删除帖子,帖子ID:{}", userId, postId);
|
||||
Post post = getById(postId);
|
||||
if (post != null) {
|
||||
post.setIsDeleted(1); // 设置为已删除
|
||||
post.setUpdatedAt(new Timestamp(System.currentTimeMillis())); // 更新修改时间
|
||||
updateById(post);
|
||||
logger.info("用户ID:{}的帖子ID:{}删除成功", userId, postId);
|
||||
} else {
|
||||
logger.warn("尝试删除的帖子ID:{}不存在", postId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Post> getPostList(String userId) {
|
||||
logger.info("用户ID:{}开始获取帖子列表", userId);
|
||||
LambdaQueryWrapper<Post> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(Post::getUserOpenid, userId);
|
||||
List<Post> posts = postMapper.selectList(wrapper);
|
||||
return posts;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Post> getCommunityPostList(Long communityId) {
|
||||
logger.info("开始获取社区ID:{}的帖子列表", communityId);
|
||||
LambdaQueryWrapper<Post> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(Post::getCommunityId, communityId);
|
||||
List<Post> posts = postMapper.selectList(wrapper);
|
||||
return posts;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Post> getOfficialPosts() {
|
||||
logger.info("开始获取所有官方创建的帖子");
|
||||
LambdaQueryWrapper<Post> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(Post::getIsOfficial, 1);
|
||||
List<Post> posts = postMapper.selectList(wrapper);
|
||||
return posts;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Post> getNonOfficialPosts() {
|
||||
logger.info("开始获取所有非官方创建的帖子");
|
||||
LambdaQueryWrapper<Post> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(Post::getIsOfficial, 0);
|
||||
List<Post> posts = postMapper.selectList(wrapper);
|
||||
return posts;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package com.ivmiku.tutorial.service.impl;
|
||||
|
||||
import com.ivmiku.tutorial.entity.Post;
|
||||
import com.ivmiku.tutorial.entity.PostTag;
|
||||
import com.ivmiku.tutorial.entity.Tag;
|
||||
import com.ivmiku.tutorial.mapper.PostMapper;
|
||||
import com.ivmiku.tutorial.mapper.PostTagMapper;
|
||||
import com.ivmiku.tutorial.mapper.TagMapper;
|
||||
import com.ivmiku.tutorial.service.PostTagService;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 帖子标签服务实现类。
|
||||
*/
|
||||
@Service
|
||||
public class PostTagServiceImpl extends ServiceImpl<PostTagMapper, PostTag> implements PostTagService {
|
||||
|
||||
// 日志记录器,用于记录服务层的日志信息
|
||||
private static final Logger logger = LoggerFactory.getLogger(PostTagServiceImpl.class);
|
||||
|
||||
@Autowired
|
||||
private TagMapper tagMapper;
|
||||
|
||||
@Autowired
|
||||
private PostMapper postMapper;
|
||||
|
||||
@Autowired
|
||||
private PostTagMapper postTagMapper;
|
||||
|
||||
/**
|
||||
* 创建帖子标签。
|
||||
* 将帖子标签数据保存到数据库。
|
||||
*
|
||||
* @param postTag 要保存的帖子标签数据
|
||||
*/
|
||||
@Override
|
||||
public void createPostTag(PostTag postTag) {
|
||||
logger.info("开始创建帖子标签");
|
||||
save(postTag);
|
||||
logger.info("帖子标签创建成功,标签ID:{}", postTag.getPostTagId());
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID获取帖子标签详情。
|
||||
* 从数据库中根据帖子标签ID获取帖子标签的详细信息。
|
||||
*
|
||||
* @param postTagId 帖子标签的唯一标识ID
|
||||
* @return PostTag 返回查询到的帖子标签对象
|
||||
*/
|
||||
@Override
|
||||
public PostTag getPostTagById(Long postTagId) {
|
||||
logger.info("开始根据ID获取帖子标签详情,标签ID:{}", postTagId);
|
||||
return getById(postTagId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新帖子标签。
|
||||
* 根据帖子标签ID更新帖子标签的内容。
|
||||
*
|
||||
* @param postTagId 要更新的帖子标签的唯一标识ID
|
||||
* @param postTag 更新后的帖子标签数据
|
||||
*/
|
||||
@Override
|
||||
public void updatePostTag(Long postTagId, PostTag postTag) {
|
||||
logger.info("开始更新帖子标签,标签ID:{}", postTagId);
|
||||
postTag.setPostTagId(postTagId);
|
||||
updateById(postTag);
|
||||
logger.info("帖子标签更新成功,标签ID:{}", postTagId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除帖子标签。
|
||||
* 根据帖子标签ID删除指定的帖子标签。
|
||||
*
|
||||
* @param postTagId 要删除的帖子标签的唯一标识ID
|
||||
*/
|
||||
@Override
|
||||
public void deletePostTag(Long postTagId) {
|
||||
logger.info("开始删除帖子标签,标签ID:{}", postTagId);
|
||||
removeById(postTagId);
|
||||
logger.info("帖子标签删除成功,标签ID:{}", postTagId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Tag> getPostTagList(Long postId) {
|
||||
logger.info("开始获取帖子标签列表,帖子ID:{}", postId);
|
||||
return tagMapper.getPostTagList(postId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Post> getTagPostList(Long tagId) {
|
||||
logger.info("开始获取标签帖子列表,标签ID:{}", tagId);
|
||||
return postMapper.getTagPostList(tagId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Tag> getSortedTagList() {
|
||||
logger.info("开始获取排序后的标签列表");
|
||||
return tagMapper.getSortedTagList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package com.ivmiku.tutorial.service.impl;
|
||||
|
||||
import com.ivmiku.tutorial.entity.Tag;
|
||||
import com.ivmiku.tutorial.mapper.TagMapper;
|
||||
import com.ivmiku.tutorial.service.TagService;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 标签服务实现类
|
||||
*/
|
||||
@Service
|
||||
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {
|
||||
|
||||
// 使用SLF4J的LoggerFactory创建一个Logger实例
|
||||
private static final Logger logger = LoggerFactory.getLogger(TagServiceImpl.class);
|
||||
|
||||
@Autowired
|
||||
private TagMapper tagMapper;
|
||||
|
||||
/**
|
||||
* 创建标签
|
||||
* @param tag 标签实体对象
|
||||
*/
|
||||
@Override
|
||||
public void createTag(Tag tag) {
|
||||
try {
|
||||
// 保存标签到数据库
|
||||
save(tag);
|
||||
logger.info("Tag created successfully: {}", tag);
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to create tag: {}", tag, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID获取标签
|
||||
* @param tagId 标签ID
|
||||
* @return 标签实体对象
|
||||
*/
|
||||
@Override
|
||||
public Tag getTagById(Long tagId) {
|
||||
try {
|
||||
// 根据ID查询标签
|
||||
return getById(tagId);
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to get tag by id: {}", tagId, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新标签信息
|
||||
* @param tagId 标签ID
|
||||
* @param tag 更新后的标签实体对象
|
||||
*/
|
||||
@Override
|
||||
public void updateTag(Long tagId, Tag tag) {
|
||||
try {
|
||||
// 设置标签ID并更新标签信息
|
||||
tag.setTagId(tagId);
|
||||
updateById(tag);
|
||||
logger.info("Tag updated successfully: {}", tag);
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to update tag with id {}: {}", tagId, tag, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除标签
|
||||
* @param tagId 标签ID
|
||||
*/
|
||||
@Override
|
||||
public void deleteTag(Long tagId) {
|
||||
try {
|
||||
// 根据ID删除标签
|
||||
removeById(tagId);
|
||||
logger.info("Tag deleted successfully with id: {}", tagId);
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to delete tag with id: {}", tagId, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Tag> getTagList() {
|
||||
List<Tag> tags = tagMapper.selectList(null);
|
||||
if (tags != null) {
|
||||
return tags;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user