- 新增 CollectionService、CommentService 和 MessageService 接口实现类 - 实现了收藏夹、评论和消息相关的业务逻辑 - 更新了 NoteLikeService 和 NoteMapper 接口,增加了点赞和笔记相关方法 - 修改了 application.properties 文件,更新了数据库连接配置
90 lines
3.1 KiB
Java
90 lines
3.1 KiB
Java
package com.example.copykamanotes.service.impl;
|
|
|
|
import com.example.copykamanotes.mapper.NoteCommentMapper;
|
|
import com.example.copykamanotes.mapper.NoteMapper;
|
|
import com.example.copykamanotes.model.base.ApiResponse;
|
|
import com.example.copykamanotes.model.base.EmptyVO;
|
|
import com.example.copykamanotes.model.entity.Note;
|
|
import com.example.copykamanotes.model.entity.NoteComment;
|
|
import com.example.copykamanotes.scope.RequestScopeData;
|
|
import com.example.copykamanotes.service.MessageService;
|
|
import com.example.copykamanotes.service.NoteCommentService;
|
|
import com.example.copykamanotes.utils.ApiResponseUtils;
|
|
import lombok.RequiredArgsConstructor;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
|
|
@Service
|
|
@RequiredArgsConstructor
|
|
public class NoteCommentServiceImpl implements NoteCommentService {
|
|
|
|
private final NoteCommentMapper noteCommentMapper;
|
|
private final NoteMapper noteMapper;
|
|
private final RequestScopeData requestScopeData;
|
|
private final MessageService messageService;
|
|
|
|
@Override
|
|
public ApiResponse<EmptyVO> createComment(Integer noteId, String content) {
|
|
Long userId = requestScopeData.getUserId();
|
|
|
|
Note note = noteMapper.findById(noteId);
|
|
if (note == null) {
|
|
return ApiResponseUtils.error("笔记不存在");
|
|
}
|
|
|
|
try {
|
|
NoteComment noteComment = new NoteComment();
|
|
noteComment.setNoteId(noteId);
|
|
noteComment.setUserId(userId);
|
|
noteComment.setContent(content);
|
|
noteComment.setCreatedAt(new Date());
|
|
noteComment.setIsDeleted(false);
|
|
noteCommentMapper.insert(noteComment);
|
|
|
|
messageService.createMessage(
|
|
note.getAuthorId(),
|
|
userId,
|
|
"note",
|
|
noteId,
|
|
"评论了你的笔记"
|
|
);
|
|
|
|
return ApiResponseUtils.success("创建评论成功");
|
|
|
|
} catch (Exception e) {
|
|
return ApiResponseUtils.error("创建评论失败");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public ApiResponse<EmptyVO> deleteComment(Integer commentId) {
|
|
Long userId = requestScopeData.getUserId();
|
|
|
|
NoteComment noteComment = noteCommentMapper.findById(commentId);
|
|
if (noteComment == null || !noteComment.getUserId().equals(userId)) {
|
|
return ApiResponseUtils.error("没有权限");
|
|
}
|
|
|
|
try {
|
|
noteComment.setIsDeleted(true);
|
|
noteComment.setUpdatedAt(new Date());
|
|
noteCommentMapper.update(noteComment);
|
|
return ApiResponseUtils.success("删除成功");
|
|
} catch (Exception e) {
|
|
return ApiResponseUtils.error("删除失败");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public ApiResponse<List<NoteComment>> getComments(Integer noteId) {
|
|
try{
|
|
List<NoteComment> comments = noteCommentMapper.findByNoteId(noteId);
|
|
return ApiResponseUtils.success("获取评论成功", comments);
|
|
} catch (Exception e) {
|
|
return ApiResponseUtils.error("获取评论失败");
|
|
}
|
|
}
|
|
}
|