package com.luozhihui.project.controller;

import com.luozhihui.project.common.ApiResponse;
import com.luozhihui.project.entity.Comment;
import com.luozhihui.project.entity.User;
import com.luozhihui.project.mapper.CommentMapper;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api")
@CrossOrigin
public class CommentController {
    //发布评论
    @Resource
    CommentMapper commentMapper;
    @CrossOrigin
    @PostMapping("/comment")
    public ApiResponse postComment(Comment comment) {
        commentMapper.save(comment); // 保存评论
        return new ApiResponse(null, 200, "发布成功");
    }
    //查看所有评论
    @CrossOrigin
    @PostMapping("/AllComment")
    public ApiResponse AllComment() {
        try {
            // 查询所有订单
            List<Comment> comment = commentMapper.FindAllComment();
            if (comment != null) {
                return new ApiResponse(comment, 200, "查询成功");
            } else {
                return new ApiResponse(null, 404, "未找到"); // 如果未找到，返回适当的状态码和消息
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResponse(null, 500, "服务器内部错误"); // 捕获任何异常并返回服务器内部错误
        }
    }
}
