发表于: 2025-06-17 21:00:50

0 29


今天完成的事情:(一定要写非常细致的内容,比如说学会了盒子模型,了解了Margin)

CommentServiceImpl
package org.example.service.Impl;

import org.example.mapper.CommentMapper;
import org.example.model.Comment;
import org.example.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

@Autowired
   private CommentMapper commentMapper;

@Override
   public Long createComment(Comment comment) {
long currentTime = System.currentTimeMillis();
comment.setCreateAt(currentTime);
comment.setUpdateAt(currentTime);
commentMapper.insert(comment);
return comment.getId();
}

@Override
   public boolean updateComment(Comment comment) {
comment.setUpdateAt(System.currentTimeMillis());
return commentMapper.update(comment) > 0;
}

@Override
   public boolean deleteComment(Long id) {
return commentMapper.delete(id) > 0;
}

@Override
   public Comment getCommentByName(String name) {
return commentMapper.selectByName(name);
}

@Override
   public List<Comment> getAllComments() {
return commentMapper.selectAll();
}

@Override
   public List<Comment> getCommentsByPid(Long pid) {
return commentMapper.selectByPid(pid);
}
}

CommentController
package org.example.controller;

import org.example.mapper.CommentMapper;
import org.example.model.ApiResponse;
import org.example.model.Comment;
import org.example.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/comment")
public class CommentController {

@Autowired
   private CommentService commentService;

@Autowired
   private CommentMapper commentMapper;

// 获取单个评论
   @GetMapping("/{name}")
public ApiResponse<Comment> getComment(@PathVariable String name) {
Comment comment = commentService.getCommentByName(name);
if (comment != null) {
return ApiResponse.success(comment);
} else {
return ApiResponse.error(404, "评论不存在");
}
}

// 创建评论
   @PostMapping("/")
public ApiResponse<Map<String, Object>> createComment(@RequestBody Comment comment) {
try {
Long id = commentService.createComment(comment);
if (id != null) {
Map<String, Object> response = new HashMap<>();
response.put("id", id);
return ApiResponse.success(response);
} else {
return ApiResponse.error(500, "创建失败");
}
} catch (Exception e) {
return ApiResponse.error(400, "参数错误: " + e.getMessage());
}
}

// 更新评论
   @PutMapping("/{id}")
public ApiResponse<Map<String, Object>> updateComment(@PathVariable Long id, @RequestBody Comment comment) {
try {
comment.setId(id);
boolean success = commentService.updateComment(comment);
if (success) {
Map<String, Object> response = new HashMap<>();
response.put("id", id);
return ApiResponse.success(response);
} else {
return ApiResponse.error(500, "更新失败,请检查ID是否存在或字段是否有改动");
}
} catch (Exception e) {
e.printStackTrace();
return ApiResponse.error(400, "更新失败: " + e.getMessage());
}
}

// 删除评论
   @DeleteMapping("/{id}")
public ApiResponse<String> deleteComment(@PathVariable Long id) {
boolean success = commentService.deleteComment(id);
if (success) {
return ApiResponse.success("删除成功");
} else {
return ApiResponse.error(500, "删除失败");
}
}

// 根据作品ID获取评论
   @GetMapping("/by-pid/{pid}")
public ApiResponse<List<Comment>> getCommentsByPid(@PathVariable Long pid) {
List<Comment> comments = commentService.getCommentsByPid(pid);
return ApiResponse.success(comments);
}
}


明天计划的事情:(一定要写非常细致的内容)
遇到的问题:(遇到什么困难,怎么解决的)
收获:(通过今天的学习,学到了什么知识)


返回列表 返回列表
评论

    分享到