feat(消息): 添加消息点赞功能
- 在MessageController中添加点赞接口 - 在MessageRepository中添加点赞数更新方法 - 在IMessageService和MessageService中实现点赞逻辑 - 初始化测试数据时设置点赞数默认值为0 - 完善相关文档注释
This commit is contained in:
5478
logs/web_project.log
5478
logs/web_project.log
File diff suppressed because it is too large
Load Diff
@@ -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删除消息
|
||||
*/
|
||||
|
||||
@@ -10,70 +10,73 @@ import org.springframework.stereotype.Repository;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository // 表明这是一个数据访问层组件,用于持久层操作
|
||||
@Repository // 表明这是一个数据访问层组件,用于持久层操作
|
||||
public interface ArticleRepository extends JpaRepository<Article, Integer> {
|
||||
/**
|
||||
* 根据文章ID查询文章信息的方法
|
||||
* 使用JPQL(Java Persistence Query Language)进行查询
|
||||
*
|
||||
* @param id 文章的唯一标识符,作为查询条件
|
||||
* @return 返回一个Optional<Article>对象,可能包含文章信息,也可能为空(如果未找到对应ID的文章)
|
||||
*/
|
||||
/**
|
||||
* 根据文章ID查询文章信息的方法
|
||||
* 使用JPQL(Java Persistence Query Language)进行查询
|
||||
*
|
||||
* @param id 文章的唯一标识符,作为查询条件
|
||||
* @return 返回一个Optional<Article>对象,可能包含文章信息,也可能为空(如果未找到对应ID的文章)
|
||||
*/
|
||||
@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的文章
|
||||
*
|
||||
* @return 返回符合查询条件的文章列表
|
||||
*/
|
||||
|
||||
/**
|
||||
* 根据文章ID查询已发布的文章
|
||||
* 使用JPQL查询语句,只查询状态为1(已发布)且指定ID的文章
|
||||
*
|
||||
* @return 返回符合查询条件的文章列表
|
||||
*/
|
||||
@Query("SELECT a FROM Article a WHERE a.status = 1")
|
||||
List<Article> findPublishedByAuthor();
|
||||
|
||||
/**
|
||||
* 根据分类ID查询已发布的文章列表
|
||||
* 使用JPQL查询语句,筛选状态为已发布(status=1)且指定分类(typeid)的文章
|
||||
*
|
||||
* @param attributeid 分类ID,通过@Param注解映射到查询语句中的:attributeid参数
|
||||
* @return 返回符合条件Article对象的列表
|
||||
*/
|
||||
/**
|
||||
* 根据分类ID查询已发布的文章列表
|
||||
* 使用JPQL查询语句,筛选状态为已发布(status=1)且指定分类(typeid)的文章
|
||||
*
|
||||
* @param attributeid 分类ID,通过@Param注解映射到查询语句中的:attributeid参数
|
||||
* @return 返回符合条件Article对象的列表
|
||||
*/
|
||||
@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)的文章,按创建时间降序排序
|
||||
*
|
||||
* @param attributeid 属性ID,通过@Param注解映射到查询语句中的:attributeid参数
|
||||
* @return 返回符合条件Article对象的列表,按创建时间降序排列
|
||||
*/
|
||||
|
||||
/**
|
||||
* 根据属性ID查询最新的文章列表
|
||||
* 使用JPQL查询语句,筛选状态为已发布(status=1)且指定属性(attributeid)的文章,按创建时间降序排序
|
||||
*
|
||||
* @param attributeid 属性ID,通过@Param注解映射到查询语句中的:attributeid参数
|
||||
* @return 返回符合条件Article对象的列表,按创建时间降序排列
|
||||
*/
|
||||
@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
|
||||
*
|
||||
* @param articleid 文章的唯一标识符,通过@Param注解将方法参数与查询参数绑定
|
||||
*/
|
||||
/**
|
||||
* 使用@Modifying注解标记这是一个修改操作,通常用于UPDATE或DELETE语句
|
||||
* 使用@Query注解定义自定义的JPQL查询语句
|
||||
* 该查询用于将指定文章的浏览量(viewCount)增加1
|
||||
*
|
||||
* @param articleid 文章的唯一标识符,通过@Param注解将方法参数与查询参数绑定
|
||||
*/
|
||||
@Modifying
|
||||
@Query("UPDATE Article a SET a.viewCount = a.viewCount + 1 WHERE a.articleid = :articleid")
|
||||
void incrementViewCount(@Param("articleid") Integer articleid);
|
||||
|
||||
|
||||
/**
|
||||
* 根据浏览量降序查询状态为1的所有文章
|
||||
* 该查询使用JPQL语句,从Article实体中选取数据
|
||||
*
|
||||
* @return 返回一个Article对象的列表,按浏览量(viewCount)降序排列
|
||||
*/
|
||||
/**
|
||||
* 根据浏览量降序查询状态为1的所有文章
|
||||
* 该查询使用JPQL语句,从Article实体中选取数据
|
||||
*
|
||||
* @return 返回一个Article对象的列表,按浏览量(viewCount)降序排列
|
||||
*/
|
||||
@Query("SELECT a FROM Article a WHERE a.status = 1 ORDER BY a.viewCount DESC")
|
||||
List<Article> findMostViewed();
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -33,7 +33,7 @@ public class ArticleService implements IArticleService {
|
||||
try {
|
||||
if (id == null || id.isEmpty()) {
|
||||
return ResponseMessage.failure("文章ID不能为空");
|
||||
}
|
||||
}
|
||||
Article article = articleRepository.findById(Integer.parseInt(id))
|
||||
.orElseThrow(() -> new RuntimeException("文章不存在"));
|
||||
|
||||
@@ -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对象
|
||||
*/
|
||||
|
||||
@@ -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对象
|
||||
*/
|
||||
|
||||
@@ -7,34 +7,50 @@ import com.qf.myafterprojecy.pojo.dto.MessageDto;
|
||||
import java.util.List;
|
||||
|
||||
public interface IMessageService {
|
||||
/**
|
||||
* 获取所有消息的方法
|
||||
* @return 返回一个ResponseMessage对象,其中包含一个可迭代的Message集合
|
||||
* ResponseMessage是响应消息的包装类,Iterable<Message>表示可迭代的消息集合
|
||||
*/
|
||||
/**
|
||||
* 获取所有消息的方法
|
||||
*
|
||||
* @return 返回一个ResponseMessage对象,其中包含一个可迭代的Message集合
|
||||
* ResponseMessage是响应消息的包装类,Iterable<Message>表示可迭代的消息集合
|
||||
*/
|
||||
ResponseMessage<Iterable<Message>> getAllMessages();
|
||||
/**
|
||||
* 根据消息ID获取消息的方法
|
||||
* @param id 消息的唯一标识符
|
||||
* @return ResponseMessage<Message> 包含消息的响应对象,其中Message为消息的具体内容
|
||||
*/
|
||||
|
||||
/**
|
||||
* 根据消息ID获取消息的方法
|
||||
*
|
||||
* @param id 消息的唯一标识符
|
||||
* @return ResponseMessage<Message> 包含消息的响应对象,其中Message为消息的具体内容
|
||||
*/
|
||||
ResponseMessage<Message> getMessageById(Integer id);
|
||||
/**
|
||||
* 保存消息的方法
|
||||
* @param message 消息数据传输对象,包含需要保存的消息信息
|
||||
* @return ResponseMessage<Message> 返回一个响应消息对象,包含操作结果和保存后的消息信息
|
||||
*/
|
||||
|
||||
/**
|
||||
* 保存消息的方法
|
||||
*
|
||||
* @param message 消息数据传输对象,包含需要保存的消息信息
|
||||
* @return ResponseMessage<Message> 返回一个响应消息对象,包含操作结果和保存后的消息信息
|
||||
*/
|
||||
ResponseMessage<Message> saveMessage(MessageDto message);
|
||||
/**
|
||||
* 根据消息ID删除消息的方法
|
||||
* @param id 要删除的消息ID
|
||||
* @return ResponseMessage<Message> 包含操作结果的响应消息,其中泛型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 评论数量
|
||||
*/
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user