feat(消息): 添加消息点赞功能

- 在MessageController中添加点赞接口
- 在MessageRepository中添加点赞数更新方法
- 在IMessageService和MessageService中实现点赞逻辑
- 初始化测试数据时设置点赞数默认值为0
- 完善相关文档注释
This commit is contained in:
qingfeng1121
2025-10-23 18:18:27 +08:00
parent f53e251d46
commit 46be613f28
11 changed files with 5740 additions and 129 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -90,7 +90,12 @@ public class MessageController {
logger.info("接收创建消息的请求: {}", message != null ? message.getNickname() : "null");
return messageService.saveMessage(message);
}
// 点赞数增加
@PostMapping("/{id}/like")
public ResponseMessage<Message> likeMessage(@PathVariable Integer id) {
logger.info("接收点赞消息的请求: {}", id);
return messageService.likeMessage(id);
}
/**
* 根据ID删除消息
*/

View File

@@ -12,7 +12,7 @@ import java.util.Optional;
@Repository // 表明这是一个数据访问层组件,用于持久层操作
public interface ArticleRepository extends JpaRepository<Article, Integer> {
/**
/**
* 根据文章ID查询文章信息的方法
* 使用JPQLJava Persistence Query Language进行查询
*
@@ -21,14 +21,17 @@ public interface ArticleRepository extends JpaRepository<Article, Integer> {
*/
@Query("SELECT a FROM Article a WHERE a.articleid = :id")
Optional<Article> findById(@Param("id") Integer id);
/**
* 根据标题查询文章列表
*
* @param title 文章标题的一部分,用于模糊查询
* @return 返回符合查询条件的文章列表
*/
@Query("SELECT a FROM Article a WHERE a.title LIKE %:title%")
List<Article> findByTitle(@Param("title") String title);
/**
/**
* 根据文章ID查询已发布的文章
* 使用JPQL查询语句只查询状态为1已发布且指定ID的文章
*
@@ -37,7 +40,7 @@ public interface ArticleRepository extends JpaRepository<Article, Integer> {
@Query("SELECT a FROM Article a WHERE a.status = 1")
List<Article> findPublishedByAuthor();
/**
/**
* 根据分类ID查询已发布的文章列表
* 使用JPQL查询语句筛选状态为已发布(status=1)且指定分类(typeid)的文章
*
@@ -46,7 +49,8 @@ public interface ArticleRepository extends JpaRepository<Article, Integer> {
*/
@Query("SELECT a FROM Article a WHERE a.status = 1 AND a.attributeid = :attributeid")
List<Article> findPublishedByAttribute(@Param("attributeid") Integer attributeid);
/**
/**
* 根据属性ID查询最新的文章列表
* 使用JPQL查询语句筛选状态为已发布(status=1)且指定属性(attributeid)的文章,按创建时间降序排序
*
@@ -56,7 +60,7 @@ public interface ArticleRepository extends JpaRepository<Article, Integer> {
@Query("SELECT a FROM Article a WHERE a.status = 1 AND a.attributeid = :attributeid ORDER BY a.createdAt DESC")
List<Article> findLatestByAttribute(@Param("attributeid") Integer attributeid);
/**
/**
* 使用@Modifying注解标记这是一个修改操作通常用于UPDATE或DELETE语句
* 使用@Query注解定义自定义的JPQL查询语句
* 该查询用于将指定文章的浏览量(viewCount)增加1
@@ -67,8 +71,7 @@ public interface ArticleRepository extends JpaRepository<Article, Integer> {
@Query("UPDATE Article a SET a.viewCount = a.viewCount + 1 WHERE a.articleid = :articleid")
void incrementViewCount(@Param("articleid") Integer articleid);
/**
/**
* 根据浏览量降序查询状态为1的所有文章
* 该查询使用JPQL语句从Article实体中选取数据
*

View File

@@ -3,7 +3,9 @@ package com.qf.myafterprojecy.repository;
import com.qf.myafterprojecy.pojo.Message;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.data.jpa.repository.Modifying;
import java.util.List;
@@ -11,22 +13,57 @@ import java.util.List;
public interface MessageRepository extends JpaRepository<Message, Integer> {
// 根据文章ID查询消息
/**
* 根据文章ID查询消息
* @param articleid 文章ID
* @return 文章下的消息列表
*/
List<Message> findByArticleid(Integer articleid);
// 查询所有父消息(回复的根消息)
/**
* 查询所有父消息(回复的根消息)
* @return 根回复消息列表
*/
List<Message> findByParentidIsNull();
// 根据父消息ID查询回复
/**
* 根据父消息ID查询回复
* @param parentid 父消息ID
* @return 回复消息列表
*/
List<Message> findByParentid(Integer parentid);
// 根据昵称模糊查询消息
/**
* 根据昵称模糊查询消息
* @param nickname 昵称关键词
* @return 包含关键词的消息列表
*/
List<Message> findByNicknameContaining(String nickname);
// 查询指定文章下的所有父消息(根回复)
@Query("SELECT m FROM Message m WHERE m.articleid = ?1 AND m.parentid IS NULL ORDER BY m.createdAt DESC")
List<Message> findRootMessagesByArticleId(Integer articleId);
/**
* 查询指定文章下的所有父消息(根回复)
* @param articleId 文章ID
* @return 根回复消息列表
*/
@Query("SELECT m FROM Message m WHERE m.articleid = :articleId AND m.parentid IS NULL ORDER BY m.createdAt DESC")
List<Message> findRootMessagesByArticleId(@Param("articleId") Integer articleId);
/**
* 点赞数增加
* @param messageId 消息ID
*/
@Modifying
@Query("UPDATE Message m SET m.likes = COALESCE(m.likes, 0) + 1 WHERE m.messageid = :messageId")
void incrementLikes(@Param("messageId") Integer messageId);
// 统计指定文章的评论数量
@Query("SELECT COUNT(m) FROM Message m WHERE m.articleid = ?1")
Long countByArticleId(Integer articleId);
/**
* 统计指定文章的评论数量
* @param articleId 文章ID
* @return 评论数量
*/
@Query("SELECT COUNT(m) FROM Message m WHERE m.articleid = :articleId")
Long countByArticleId(@Param("articleId") Integer articleId);
}

View File

@@ -61,6 +61,7 @@ public class MessageDataChecker implements CommandLineRunner {
message1.setCreatedAt(new Date());
message1.setArticleid(1);
message1.setParentid(null); // 根评论
message1.setLikes(0); // 设置点赞数初始值为0
messageRepository.save(message1);
// 添加回复
@@ -71,6 +72,7 @@ public class MessageDataChecker implements CommandLineRunner {
reply1.setCreatedAt(new Date());
reply1.setArticleid(1);
reply1.setParentid(message1.getMessageid()); // 回复第一篇评论
reply1.setLikes(0); // 设置点赞数初始值为0
messageRepository.save(reply1);
// 添加第二篇文章的评论
@@ -81,6 +83,7 @@ public class MessageDataChecker implements CommandLineRunner {
message2.setCreatedAt(new Date());
message2.setArticleid(2);
message2.setParentid(null);
message2.setLikes(0); // 设置点赞数初始值为0
messageRepository.save(message2);
logger.info("成功添加了{}条测试消息数据", messageRepository.count());

View File

@@ -46,6 +46,7 @@ public class ArticleService implements IArticleService {
return ResponseMessage.failure("获取文章失败");
}
}
@Override
@Transactional(readOnly = true)
public ResponseMessage<List<Article>> getArticlesByTitle(String title) {
@@ -163,8 +164,10 @@ public class ArticleService implements IArticleService {
return ResponseMessage.failure("获取属性文章失败");
}
}
/**
* 增加文章浏览量
*
* @param id 文章ID
* @return 返回包含更新后文章信息的ResponseMessage对象
*/

View File

@@ -8,18 +8,46 @@ import java.util.List;
public interface IArticleService {
ResponseMessage<Article> getArticleById(String id);
ResponseMessage<List<Article>> getAllArticles();
/**
* 根据标题查询文章列表
*
* @param title 文章标题的一部分,用于模糊查询
* @return 返回符合查询条件的文章列表
*/
ResponseMessage<List<Article>> getArticlesByTitle(String title);
/**
* 创建新文章
* 仅限AUTHOR角色用户访问
*
* @param articleDto 包含文章数据的DTO对象
* @return 返回包含新创建文章信息的ResponseMessage对象
*/
ResponseMessage<Article> saveArticle(ArticleDto articleDto);
/**
* 更新指定ID的文章
*
* @param id 文章ID
* @param articleDto 包含更新信息的ArticleDto对象
* @return 返回包含操作结果的ResponseMessage对象
*/
ResponseMessage<Article> updateArticle(Integer id, ArticleDto articleDto);
/**
* 删除指定ID的文章
*
* @param id 文章ID
* @return 返回包含操作结果的ResponseMessage对象
*/
ResponseMessage<Article> deleteArticle(Integer id);
/**
* 根据分类ID查询文章列表兼容旧接口
*
* @param typeid 分类ID
* @return 返回符合查询条件的文章列表
*/
@@ -27,6 +55,7 @@ public interface IArticleService {
/**
* 根据属性ID查询文章列表
*
* @param attributeid 属性ID
* @return 返回符合查询条件的文章列表
*/
@@ -34,13 +63,17 @@ public interface IArticleService {
/**
* 根据属性ID查询最新文章列表
*
* @param attributeid 属性ID
* @return 返回符合查询条件的最新文章列表
*/
ResponseMessage<List<Article>> getLatestArticlesByAttribute(Integer attributeid);
ResponseMessage<List<Article>> getMostViewedArticles();
/**
* 增加文章浏览量
*
* @param id 文章ID
* @return 返回包含更新后文章信息的ResponseMessage对象
*/

View File

@@ -7,34 +7,50 @@ import com.qf.myafterprojecy.pojo.dto.MessageDto;
import java.util.List;
public interface IMessageService {
/**
/**
* 获取所有消息的方法
*
* @return 返回一个ResponseMessage对象其中包含一个可迭代的Message集合
* ResponseMessage是响应消息的包装类Iterable<Message>表示可迭代的消息集合
*/
ResponseMessage<Iterable<Message>> getAllMessages();
/**
/**
* 根据消息ID获取消息的方法
*
* @param id 消息的唯一标识符
* @return ResponseMessage<Message> 包含消息的响应对象其中Message为消息的具体内容
*/
ResponseMessage<Message> getMessageById(Integer id);
/**
/**
* 保存消息的方法
*
* @param message 消息数据传输对象,包含需要保存的消息信息
* @return ResponseMessage<Message> 返回一个响应消息对象,包含操作结果和保存后的消息信息
*/
ResponseMessage<Message> saveMessage(MessageDto message);
/**
/**
* 根据消息ID删除消息的方法
*
* @param id 要删除的消息ID
* @return ResponseMessage<Message> 包含操作结果的响应消息其中泛型Message表示被删除的消息内容
*/
ResponseMessage<Message> deleteMessage(Integer id);
/**
* 增加消息的点赞数
*
* @param id 消息ID
* @return 包含操作结果的响应消息其中泛型Message表示操作后的消息内容
*/
ResponseMessage<Message> likeMessage(Integer id);
// 新增方法
/**
* 根据文章ID查询消息
*
* @param articleId 文章ID
* @return 消息列表
*/
@@ -42,12 +58,14 @@ public interface IMessageService {
/**
* 查询所有父消息(根回复)
*
* @return 父消息列表
*/
ResponseMessage<List<Message>> getRootMessages();
/**
* 根据父消息ID查询回复
*
* @param parentId 父消息ID
* @return 回复消息列表
*/
@@ -55,14 +73,18 @@ public interface IMessageService {
/**
* 根据昵称模糊查询消息
*
* @param nickname 昵称
* @return 匹配的消息列表
*/
ResponseMessage<List<Message>> searchMessagesByNickname(String nickname);
//删除所有评论
// 删除所有评论
ResponseMessage<Void> deleteAllMessages();
/**
* 获取指定文章的评论数量
*
* @param articleId 文章ID
* @return 评论数量
*/

View File

@@ -16,6 +16,8 @@ import java.util.Date;
import java.util.List;
import java.util.Optional;
import org.springframework.transaction.annotation.Transactional;
@Service
public class MessageService implements IMessageService {
@@ -59,6 +61,7 @@ public class MessageService implements IMessageService {
}
@Override
@Transactional(rollbackFor = Exception.class)
public ResponseMessage<Message> saveMessage(MessageDto messageDto) {
// 参数校验
if (messageDto == null) {
@@ -83,6 +86,7 @@ public class MessageService implements IMessageService {
Message message = new Message();
BeanUtils.copyProperties(messageDto, message);
message.setCreatedAt(new Date()); // 设置创建时间
message.setLikes(0); // 设置点赞数初始值为0
// 如果是回复确保parentid有效
if (messageDto.getParentid() != null && messageDto.getParentid() > 0) {
@@ -102,6 +106,7 @@ public class MessageService implements IMessageService {
}
@Override
@Transactional(rollbackFor = Exception.class)
public ResponseMessage<Message> deleteMessage(Integer id) {
if (id == null || id <= 0) {
logger.warn("删除消息时ID无效: {}", id);
@@ -175,6 +180,25 @@ public class MessageService implements IMessageService {
}
}
// 点赞数增加
@Override
@Transactional(rollbackFor = Exception.class)
public ResponseMessage<Message> likeMessage(Integer id) {
if (id == null || id <= 0) {
logger.warn("点赞消息时ID无效: {}", id);
return ResponseMessage.failure("消息ID无效");
}
try {
logger.info("点赞消息: {}", id);
messageRepository.incrementLikes(id);
Message likedMessage = messageRepository.findById(id).orElse(null);
return ResponseMessage.success(likedMessage, "点赞成功", true);
} catch (DataAccessException e) {
logger.error("点赞消息失败: {}", id, e);
return ResponseMessage.failure("点赞消息失败:" + e.getMessage());
}
}
@Override
public ResponseMessage<List<Message>> searchMessagesByNickname(String nickname) {
if (StringUtils.isEmpty(nickname)) {
@@ -191,8 +215,10 @@ public class MessageService implements IMessageService {
return ResponseMessage.failure("查询消息失败:" + e.getMessage());
}
}
//删除所有评论
// 删除所有评论
@Override
@Transactional(rollbackFor = Exception.class)
public ResponseMessage<Void> deleteAllMessages() {
try {
logger.info("删除所有消息");
@@ -203,6 +229,7 @@ public class MessageService implements IMessageService {
return ResponseMessage.failure("删除消息失败:" + e.getMessage());
}
}
@Override
public ResponseMessage<Long> getMessageCountByArticleId(Integer articleId) {
if (articleId == null || articleId <= 0) {