新增获赞收藏发布

This commit is contained in:
苏元皓
2024-08-23 15:31:12 +08:00
parent 19f3325b50
commit d6492a9657
23 changed files with 521 additions and 24 deletions

View File

@@ -210,7 +210,6 @@ public class CommentController {
public Result getPostComments(@PathVariable("postId") Long postId, @RequestBody Pages pages) {
logger.info("获取帖子ID{}的评论列表", postId);
logger.info("获取pageSize{}", pages.getPageSize());
System.out.println("aaaaaa" + pages.getPageSize());
IPage<Comment> comments = commentService.getPostComments(postId, pages.getPageNum(), pages.getPageSize());
return Result.ok(comments);
}

View File

@@ -4,6 +4,7 @@ import cn.dev33.satoken.annotation.SaCheckLogin;
import com.ivmiku.tutorial.entity.Community;
import com.ivmiku.tutorial.response.Result;
import com.ivmiku.tutorial.service.CommunityService;
import com.ivmiku.tutorial.service.CommunitytagService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.models.annotations.OpenAPI30;
import org.apiguardian.api.API;

View File

@@ -0,0 +1,47 @@
package com.ivmiku.tutorial.controller;
import com.ivmiku.tutorial.entity.Communitytag;
import com.ivmiku.tutorial.response.Result;
import com.ivmiku.tutorial.service.CommunitytagService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/communityTags")
public class CommunitytagController {
@Resource
private CommunitytagService communitytagService;
@PostMapping("/create")
public Result createCommunityTag(@RequestBody Communitytag communityTag) {
communitytagService.createCommunityTag(communityTag);
return Result.ok();
}
@PutMapping("/update/{id}")
public Result updateCommunityTag(@PathVariable Long id, @RequestBody Communitytag communityTag) {
communitytagService.updateCommunityTag(id, communityTag);
return Result.ok();
}
@DeleteMapping("/delete/{id}")
public Result deleteCommunityTag(@PathVariable Long id) {
communitytagService.deleteCommunityTag(id);
return Result.ok();
}
@GetMapping("/get/{id}")
public Result getCommunityTag(@PathVariable Long id) {
Communitytag communityTag = communitytagService.getCommunityTagById(id);
return Result.ok(communityTag);
}
@GetMapping("/list/{communityId}")
public Result getCommunityTagListByCommunityId(@PathVariable Long communityId) {
List<Communitytag> tags = communitytagService.getCommunityTagListByCommunityId(communityId);
return Result.ok(tags);
}
}

View File

@@ -2,12 +2,17 @@ package com.ivmiku.tutorial.controller;
import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.hash.Hash;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ivmiku.tutorial.entity.Post;
import com.ivmiku.tutorial.response.Result;
import com.ivmiku.tutorial.service.InteractionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
/**
* InteractionController 是处理用户与帖子的收藏、点赞以及对评论的点赞功能的控制器类。
@@ -104,4 +109,35 @@ public class InteractionController {
interactionService.unlikeComment(userOpenid, commentId);
return Result.ok();
}
/**
* 我的收藏接口
* @return
*/
@GetMapping("/getFavoritePosts")
public Result getFavoritePosts() {
String userOpenid = StpUtil.getLoginIdAsString();
log.info("用户 {} 正在获取收藏的帖子", userOpenid);
IPage<Post> favoritePost = interactionService.getFavoritePosts(userOpenid);
HashMap<String, Object> data = new HashMap<>();
if (favoritePost != null) {
log.info("用户 {} 收藏的帖子获取成功", userOpenid);
data.put("total", favoritePost.getTotal());
data.put("list", favoritePost.getRecords());
return Result.ok(data);
}
return Result.ok("用户未发表帖子");
}
/**
* 统计我的获赞数量 笔记数量 收藏数量
*/
@GetMapping("/getLikeCount")
public Result getLikeCount() {
String userOpenid = StpUtil.getLoginIdAsString();
log.info("用户 {} 正在获取用户获赞的数量", userOpenid);
HashMap<String, Object> res = interactionService.getLikeCount(userOpenid);
return Result.ok();
}
}

View File

@@ -16,7 +16,9 @@ import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 帖子控制器用于处理帖子相关的HTTP请求。
@@ -82,7 +84,8 @@ public class PostController {
@GetMapping("/get/{id}")
public Result getPost(@PathVariable Long id) {
logger.info("开始获取帖子ID{}的详细信息", id);
Post post = postService.getPostById(id); // 调用服务层根据ID查询帖子
String userId = StpUtil.getLoginIdAsString();
Post post = postService.getPostById(id, userId); // 调用服务层根据ID查询帖子
if (post != null) {
logger.info("帖子ID{}的详细信息获取成功", id);
} else {
@@ -111,21 +114,24 @@ public class PostController {
/**
* 用户获取自己所有帖子的信息
* @return 当前用户帖子列表
* @return 当前用户帖子列表与该用户的总发布数量
*/
@GetMapping("/getPostList")
public Result getPostList(@RequestBody Pages pages) {
logger.info("开始获取帖子列表");
String userId = StpUtil.getLoginIdAsString(); // 获取当前登录用户的ID
IPage<Post> posts = postService.getPostList(userId, pages.getPageNum(), pages.getPageSize());
return Result.ok(posts); // 调用服务层获取帖子列表
Map<String, Object> data = new HashMap<>();
data.put("total", posts.getTotal());
data.put("posts", posts);
return Result.ok(data); // 调用服务层获取帖子列表
}
/**
* 社区获取自己所有帖子的信息
* @return 当前用户帖子列表
*/
@GetMapping("/getCommunityPostList{communityId}")
@GetMapping("/getCommunityPostList/{communityId}")
public Result getCommunityPostList(@PathVariable("communityId") Long communityId, @RequestBody Pages pages) {
logger.info("开始获取帖子列表");
IPage<Post> posts = postService.getCommunityPostList(communityId, pages.getPageNum(), pages.getPageSize());

View File

@@ -0,0 +1,30 @@
package com.ivmiku.tutorial.entity;
import com.baomidou.mybatisplus.annotation.*;
import java.io.Serializable;
import java.util.Date;
import lombok.Data;
/**
* @TableName browing_history
*/
@TableName(value ="browing_history")
@Data
public class BrowingHistory implements Serializable {
@TableId(type = IdType.AUTO)
private Long browingId;
private String userOpenid;
private Long postId;
private Date createAt;
@TableLogic
private Integer isDelete;
private static final long serialVersionUID = 1L;
}

View File

@@ -0,0 +1,27 @@
package com.ivmiku.tutorial.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import lombok.Data;
/**
* @TableName communitytag
*/
@TableName(value ="communitytag")
@Data
public class Communitytag implements Serializable {
@TableId
private Long smallCTagId;
private String cTagName;
private Long cId;
private Integer isDelete;
private static final long serialVersionUID = 1L;
}

View File

@@ -0,0 +1,18 @@
package com.ivmiku.tutorial.mapper;
import com.ivmiku.tutorial.entity.BrowingHistory;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* @author rog
* @description 针对表【browing_history】的数据库操作Mapper
* @createDate 2024-08-23 11:29:33
* @Entity com.ivmiku.tutorial.entity.BrowingHistory
*/
public interface BrowingHistoryMapper extends BaseMapper<BrowingHistory> {
}

View File

@@ -0,0 +1,18 @@
package com.ivmiku.tutorial.mapper;
import com.ivmiku.tutorial.entity.Communitytag;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* @author rog
* @description 针对表【communitytag】的数据库操作Mapper
* @createDate 2024-08-16 09:40:06
* @Entity com.ivmiku.tutorial.entity.Communitytag
*/
public interface CommunitytagMapper extends BaseMapper<Communitytag> {
}

View File

@@ -2,6 +2,8 @@ package com.ivmiku.tutorial.mapper;
import com.ivmiku.tutorial.entity.Likee;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
/**
* @author rog
@@ -11,6 +13,14 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
*/
public interface LikeMapper extends BaseMapper<Likee> {
// 获取帖子获赞数
@Select("SELECT SUM(like_count) FROM posts WHERE user_openid = #{userOpenid}")
Long getPostLikeCount(@Param("userOpenid") String userOpenid);
// 获取评论获赞数
@Select("SELECT SUM(like_count) FROM comments WHERE user_openid = #{userOpenid}")
Long getCommentLikeCount(@Param("userOpenid") String userOpenid);
}

View File

@@ -0,0 +1,18 @@
package com.ivmiku.tutorial.service;
import com.ivmiku.tutorial.entity.BrowingHistory;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
/**
* @author rog
* @description 针对表【browing_history】的数据库操作Service
* @createDate 2024-08-23 11:29:33
*/
public interface BrowingHistoryService extends IService<BrowingHistory> {
void deleteBrowingRecordById(Long browingId);
void deleteBrowingRecordsByIds(List<Long> browingIds);
}

View File

@@ -0,0 +1,24 @@
package com.ivmiku.tutorial.service;
import com.ivmiku.tutorial.entity.Communitytag;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
/**
* @author rog
* @description 针对表【communitytag】的数据库操作Service
* @createDate 2024-08-16 09:40:06
*/
public interface CommunitytagService extends IService<Communitytag> {
void createCommunityTag(Communitytag communityTag);
void updateCommunityTag(Long id, Communitytag communityTag);
void deleteCommunityTag(Long id);
Communitytag getCommunityTagById(Long id);
List<Communitytag> getCommunityTagListByCommunityId(Long communityId);
}

View File

@@ -1,5 +1,11 @@
package com.ivmiku.tutorial.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ivmiku.tutorial.entity.Post;
import java.util.HashMap;
import java.util.List;
/**
* InteractionService接口定义用户对帖子和评论的收藏与点赞操作。
*/
@@ -10,4 +16,8 @@ public interface InteractionService {
void unlikePost(String userOpenid, Long postId);
void likeComment(String userOpenid, Long commentId);
void unlikeComment(String userOpenid, Long commentId);
IPage<Post> getFavoritePosts(String userOpenid);
HashMap<String, Object> getLikeCount(String userOpenid);
}

View File

@@ -9,7 +9,8 @@ import java.util.List;
public interface PostService extends IService<Post> {
void createPost(Post post);
Post getPostById(Long postId);
Post getPostById(Long postId, String userOpenid);
void updatePost(Long postId, String userId, Post post);
void deletePost(Long postId, String userId);

View File

@@ -0,0 +1,60 @@
package com.ivmiku.tutorial.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ivmiku.tutorial.entity.BrowingHistory;
import com.ivmiku.tutorial.service.BrowingHistoryService;
import com.ivmiku.tutorial.mapper.BrowingHistoryMapper;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author rog
* @description 针对表【browing_history】的数据库操作Service实现
* @createDate 2024-08-23 11:29:33
*/
@Service
@Slf4j
public class BrowingHistoryServiceImpl extends ServiceImpl<BrowingHistoryMapper, BrowingHistory>
implements BrowingHistoryService{
private static final Logger logger = LoggerFactory.getLogger(BrowingHistoryServiceImpl.class);
@Override
public void deleteBrowingRecordById(Long browingId) {
logger.info("开始删除浏览记录浏览记录ID{}", browingId);
BrowingHistory browingHistory = getById(browingId);
if (browingHistory != null) {
browingHistory.setIsDelete(1); // 标记为已删除
updateById(browingHistory);
logger.info("浏览记录ID{} 删除成功", browingId);
} else {
logger.warn("浏览记录ID{} 不存在", browingId);
}
}
@Override
public void deleteBrowingRecordsByIds(List<Long> browingIds) {
logger.info("开始批量删除浏览记录浏览记录ID列表{}", browingIds);
if (browingIds != null && !browingIds.isEmpty()) {
List<BrowingHistory> browingHistories = listByIds(browingIds);
if (!browingHistories.isEmpty()) {
for (BrowingHistory browingHistory : browingHistories) {
browingHistory.setIsDelete(1); // 标记为已删除
}
updateBatchById(browingHistories); // 批量更新删除状态
logger.info("批量删除浏览记录成功");
} else {
logger.warn("提供的浏览记录ID列表中没有找到匹配的记录");
}
} else {
logger.warn("浏览记录ID列表为空无法删除");
}
}
}

View File

@@ -0,0 +1,50 @@
package com.ivmiku.tutorial.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ivmiku.tutorial.entity.Communitytag;
import com.ivmiku.tutorial.service.CommunitytagService;
import com.ivmiku.tutorial.mapper.CommunitytagMapper;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author rog
* @description 针对表【communitytag】的数据库操作Service实现
* @createDate 2024-08-16 09:40:06
*/
@Service
public class CommunitytagServiceImpl extends ServiceImpl<CommunitytagMapper, Communitytag>
implements CommunitytagService{
@Override
public void createCommunityTag(Communitytag communityTag) {
this.save(communityTag);
}
@Override
public void updateCommunityTag(Long id, Communitytag communityTag) {
communityTag.setSmallCTagId(id);
this.updateById(communityTag);
}
@Override
public void deleteCommunityTag(Long id) {
this.removeById(id);
}
@Override
public Communitytag getCommunityTagById(Long id) {
return this.getById(id);
}
@Override
public List<Communitytag> getCommunityTagListByCommunityId(Long communityId) {
return this.lambdaQuery().eq(Communitytag::getCId, communityId).list();
}
}

View File

@@ -1,15 +1,22 @@
package com.ivmiku.tutorial.service.impl;
import cn.hutool.core.lang.hash.Hash;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ivmiku.tutorial.entity.Favorite;
import com.ivmiku.tutorial.entity.Likee;
import com.ivmiku.tutorial.entity.Post;
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 com.ivmiku.tutorial.service.PostService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
/**
* InteractionServiceImpl 实现了 InteractionService 接口,处理用户对帖子和评论的收藏与点赞操作。
@@ -24,6 +31,9 @@ public class InteractionServiceImpl implements InteractionService {
@Resource
private LikeMapper likeMapper;
@Resource
private PostService postService;
@Override
public void favoritePost(String userOpenid, Long postId) {
log.info("User {} is favoriting post {}", userOpenid, postId);
@@ -95,4 +105,60 @@ public class InteractionServiceImpl implements InteractionService {
likeMapper.updateById(likee);
}
}
@Override
public IPage<Post> getFavoritePosts(String userOpenid) {
LambdaQueryWrapper<Favorite> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(Favorite::getUserOpenid, userOpenid);
List<Favorite> favorites = favoriteMapper.selectList(wrapper);
if (favorites != null && !favorites.isEmpty()) {
List<Long> postIds = favorites.stream().map(Favorite::getPostId).toList();
return postService.getPostList(userOpenid, 1, 10);
}
return null;
}
@Override
public HashMap<String, Object> getLikeCount(String userOpenid) {
HashMap<String, Object> res = new HashMap<>();
// 获取帖子获赞数
Long postLikeCount = likeMapper.getPostLikeCount(userOpenid);
// 获取评论获赞数
Long commentLikeCount = likeMapper.getCommentLikeCount(userOpenid);
// 如果帖子获赞数不为空,加入结果
if (postLikeCount != null) {
res.put("postLikeCount", postLikeCount);
} else {
res.put("postLikeCount", 0L); // 如果为空设置为0
}
// 如果评论获赞数不为空,加入结果
if (commentLikeCount != null) {
res.put("commentLikeCount", commentLikeCount);
} else {
res.put("commentLikeCount", 0L); // 如果为空设置为0
}
// 计算总的获赞数
Long totalLikeCount = (postLikeCount == null ? 0 : postLikeCount) +
(commentLikeCount == null ? 0 : commentLikeCount);
res.put("totalLikeCount", totalLikeCount);
// 获取收藏数量
LambdaQueryWrapper<Favorite> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(Favorite::getUserOpenid, userOpenid);
Long favoriteCount = favoriteMapper.selectCount(wrapper);
if (favoriteCount != null) {
res.put("favoriteCount", favoriteCount);
} else {
res.put("favoriteCount", 0L); // 如果为空设置为0
}
Long myTotalPost = getFavoritePosts(userOpenid).getTotal();
res.put("myTotalPost", myTotalPost);
return res;
}
}

View File

@@ -3,20 +3,18 @@ package com.ivmiku.tutorial.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ivmiku.tutorial.entity.Comment;
import com.ivmiku.tutorial.entity.BrowingHistory;
import com.ivmiku.tutorial.entity.Post;
import com.ivmiku.tutorial.mapper.PostMapper;
import com.ivmiku.tutorial.service.BrowingHistoryService;
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 java.sql.Timestamp;
import java.util.List;
import java.util.Date;
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements PostService {
@@ -26,6 +24,9 @@ public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements Po
@Autowired
private PostMapper postMapper;
@Autowired
private BrowingHistoryService browingHistoryService;
@Override
public void createPost(Post post) {
logger.info("开始创建帖子");
@@ -37,15 +38,24 @@ public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements Po
}
@Override
public Post getPostById(Long postId) {
logger.info("开始根据ID获取帖子详情帖子ID{}", postId);
LambdaQueryWrapper<Post> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(Post::getIsPublic, 1);
Post post = postMapper.selectById(wrapper);
if (post == null) {
logger.warn("帖子ID{}不存在", postId);
public Post getPostById(Long postId, String userOpenid) {
logger.info("用户openid{} 开始根据ID获取帖子详情帖子ID{}", userOpenid, postId);
// 查找帖子信息
Post post = postMapper.selectById(postId);
if (post == null || post.getIsDeleted() == 1) {
logger.warn("帖子ID{}不存在或已删除", postId);
return null;
}
// 创建浏览记录
BrowingHistory browingHistory = new BrowingHistory();
browingHistory.setUserOpenid(userOpenid);
browingHistory.setPostId(postId);
browingHistory.setCreateAt(new Date());
browingHistory.setIsDelete(0); // 未删除标记
browingHistoryService.save(browingHistory);
logger.info("用户openid{}的浏览记录创建成功", userOpenid);
return post;
}

View File

@@ -27,3 +27,7 @@ minio.accessKey=minio_root
minio.secretKey=minio_123456
minio.bucketName=haixia
## Nacos??
#spring.cloud.nacos.discovery.server-addr=/192.168.146.1:8848
#dubbo.registry.address=nacos:///192.168.146.1:8848

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ivmiku.tutorial.mapper.BrowingHistoryMapper">
<resultMap id="BaseResultMap" type="com.ivmiku.tutorial.entity.BrowingHistory">
<id property="browingId" column="browing_id" jdbcType="BIGINT"/>
<result property="userOpenid" column="user_openid" jdbcType="VARCHAR"/>
<result property="postId" column="post_id" jdbcType="BIGINT"/>
<result property="createAt" column="create_at" jdbcType="TIMESTAMP"/>
<result property="isDelete" column="is_delete" jdbcType="TINYINT"/>
</resultMap>
<sql id="Base_Column_List">
browing_id,user_openid,post_id,
create_at,is_delete
</sql>
</mapper>

View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ivmiku.tutorial.mapper.CommunitytagMapper">
<resultMap id="BaseResultMap" type="com.ivmiku.tutorial.entity.Communitytag">
<id property="smallCTagId" column="small_c_tag_id" jdbcType="BIGINT"/>
<result property="cTagName" column="c_tag_name" jdbcType="VARCHAR"/>
<result property="cId" column="c_id" jdbcType="BIGINT"/>
<result property="isDelete" column="is_delete" jdbcType="TINYINT"/>
</resultMap>
<sql id="Base_Column_List">
small_c_tag_id,c_tag_name,c_id,
is_delete
</sql>
</mapper>