You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
banban/src/main/java/com/controller/CommentController.java

213 lines
8.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.controller;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONObject;
import com.entity.Comment;
import com.entity.User;
import com.github.pagehelper.PageInfo;
import com.service.ICommentService;
import com.service.IMovieService;
import com.service.IUserService;
/**
* 评论管理模块
* 该类主要用于处理与评论相关的各种操作请求,包括查询所有评论、分页查询评论、用户添加评论、修改评论、删除评论以及按用户名查询评论等功能。
* @author Wxj
*/
@Controller
@RequestMapping("/comment")
public class CommentController {
// 注入评论服务层接口,用于调用评论相关的业务逻辑方法
@Resource
private ICommentService commentService;
// 注入用户服务层接口,用于获取用户相关信息等操作
@Resource
private IUserService userService;
// 注入电影服务层接口,可能用于和评论关联的电影相关操作,比如更新电影的评论数量等
@Resource
private IMovieService movieService;
/**
* 查询所有评论的方法
* 该方法会获取所有的评论信息并为每条评论设置对应的评论用户信息然后将结果封装成JSON格式返回。
* 返回的JSON数据包含状态码、提示信息、数据总条数以及评论数据列表。
* @return JSONObject 包含评论相关信息的JSON对象
*/
@RequestMapping("findAllComments")
@ResponseBody
public JSONObject findAllComments() {
JSONObject obj = new JSONObject();
// 调用评论服务层的方法获取所有评论列表
List<Comment> list = commentService.findAllComments();
// 遍历评论列表为每条评论设置对应的评论用户信息通过用户ID从用户服务层获取用户对象
for (Comment comment : list) {
comment.setComment_user(userService.findUserById(comment.getUser_id()));
}
obj.put("code", 0);
obj.put("msg", "");
obj.put("count", list.size());
obj.put("data", list);
return obj;
}
/**
* 分页查询所有评论的方法
* 根据传入的页码、每页显示数量以及可选的关键词参数,分页查询评论信息。同样会为每条评论设置对应的评论用户信息,
* 最后将包含分页评论数据的相关信息如状态码、提示信息、总记录数、当前页评论列表等封装成JSON格式返回。
* @param page 页码默认值为1
* @param limit 每页显示的记录数量默认值为10
* @param keyword 用于筛选评论的关键词(可选参数)
* @return JSONObject 包含分页评论相关信息的JSON对象
*/
@RequestMapping("findAllCommentsPage")
@ResponseBody
public JSONObject findAllCommentsPage(@RequestParam(value = "page", defaultValue = "1") Integer page,
@RequestParam(value = "pageSize", defaultValue = "10") Integer limit,
String keyword) {
PageInfo<Comment> info = commentService.findAllCommentsBySplitPage(page, limit, keyword);
// 遍历当前页的评论列表,为每条评论设置对应的评论用户信息
for (Comment comment : info.getList()) {
comment.setComment_user(userService.findUserById(comment.getUser_id()));
}
JSONObject obj = new JSONObject();
obj.put("code", 0);
obj.put("msg", "");
obj.put("count", info.getTotal());
obj.put("data", info.getList());
return obj;
}
/**
* 用户添加评论的方法
* 根据传入的电影ID、评论内容以及从HttpServletRequest中获取当前登录用户信息创建评论对象并保存到数据库中。
* 如果保存成功还会尝试更新对应电影的评论数量最后根据操作结果封装不同的提示信息到JSON对象中返回。
* @param movie_id 被评论电影的ID
* @param comment_content 评论的具体内容
* @param request HttpServletRequest对象用于获取当前登录用户信息
* @return JSONObject 包含添加评论操作结果信息的JSON对象
*/
@RequestMapping("addCommentByUser")
@ResponseBody
public JSONObject addCommentByUser(@RequestParam("movie_id") long movie_id,
@RequestParam("comment_content") String comment_content,
HttpServletRequest request) {
User user = (User) request.getSession().getAttribute("user");
JSONObject obj = new JSONObject();
if (user == null) {
// 如果用户未登录,设置相应的状态码和提示信息
obj.put("code", 200);
obj.put("msg", "您未登录,登录之后才可评论~");
} else {
Comment comment = new Comment();
comment.setComment_content(comment_content);
comment.setMovie_id(movie_id);
comment.setUser_id(user.getUser_id());
comment.setComment_time(new Date());
// 调用评论服务层方法添加评论,返回受影响的行数
Integer rs = commentService.addComemnt(comment);
if (rs > 0) {
// 如果评论添加成功,尝试更新对应电影的评论数量
Integer rs2 = movieService.addCommentCount(comment.getMovie_id());
if (rs2 > 0) {
obj.put("code", 0);
obj.put("msg", "评论成功~");
} else {
obj.put("code", 200);
obj.put("msg", "评论失败2~");
}
} else {
obj.put("code", 200);
obj.put("msg", "评论失败~");
}
}
return obj;
}
/**
* 修改评论的方法
* 根据传入的评论ID和新的评论内容先获取对应的评论对象更新其评论内容和评论时间然后调用评论服务层方法更新评论信息
* 根据更新结果封装不同的提示信息到JSON对象中返回。
* @param comment_id 需要修改的评论的ID
* @param comment_content 新的评论内容
* @return JSONObject 包含修改评论操作结果信息的JSON对象
*/
@RequestMapping("updateComment")
@ResponseBody
public JSONObject updateComment(@RequestParam("comment_id") long comment_id,
@RequestParam("comment_content") String comment_content) {
JSONObject obj = new JSONObject();
Comment comment = this.commentService.findCommentById(comment_id);
comment.setComment_time(new Date());
comment.setComment_content(comment_content);
Integer rs = commentService.updateComment(comment);
if (rs > 0) {
obj.put("code", 0);
obj.put("msg", "修改成功~");
} else {
obj.put("code", 200);
obj.put("msg", "修改失败~");
}
return obj;
}
/**
* 删除评论的方法
* 根据传入的评论ID先尝试减少对应电影的评论数量调用电影服务层方法然后再调用评论服务层方法删除评论记录
* 根据删除操作的结果封装不同的提示信息到JSON对象中返回。
* @param comment_id 需要删除的评论的ID
* @return JSONObject 包含删除评论操作结果信息的JSON对象
*/
@RequestMapping("deleteComemnt")
@ResponseBody
public JSONObject deleteComment(@RequestParam("comment_id") long comment_id) {
JSONObject obj = new JSONObject();
// 先获取要删除评论对应的电影ID然后调用电影服务层方法减少该电影的评论数量
Integer rs2 = movieService.delCommentCount(commentService.findCommentById(comment_id).getMovie_id());
Integer rs = commentService.deleteComment(comment_id);
if (rs > 0) {
obj.put("code", 0);
obj.put("msg", "删除成功~");
} else {
obj.put("code", 200);
obj.put("msg", "删除失败~");
}
return obj;
}
/**
* 按用户名分页查询评论的方法
* 根据传入的页码、每页显示数量以及用户名参数,分页查询该用户的评论信息,为每条评论设置对应的评论用户信息后,
* 将包含分页评论数据的相关信息如状态码、提示信息、总记录数、当前页评论列表等封装成JSON格式返回。
* @param page 页码默认值为1
* @param limit 每页显示的记录数量默认值为10
* @param user_name 要查询评论的用户名
* @return JSONObject 包含按用户名分页查询评论相关信息的JSON对象
*/
@RequestMapping("findCommentsByUserName")
@ResponseBody
public JSONObject findCommentsByUserName(@RequestParam(value = "page", defaultValue = "1") Integer page,
@RequestParam(value = "limit", defaultValue = "10") Integer limit,
@RequestParam("user_name") String user_name) {
PageInfo<Comment> info = commentService.findCommentsByUserName(page, limit, user_name);
// 遍历当前页的评论列表,为每条评论设置对应的评论用户信息
for (Comment comment : info.getList()) {
comment.setComment_user(userService.findUserById(comment.getUser_id()));
}
JSONObject obj = new JSONObject();
obj.put("code", 0);
obj.put("msg", "");
obj.put("count", info.getTotal());
obj.put("data", info.getList());
return obj;
}
}