package top.lejings.demo.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.lejings.demo.pojo.Comments;
import top.lejings.demo.pojo.Result;
import top.lejings.demo.service.CommentsService;

import java.util.List;

/*
* 对评论进行操作
* 查询评论
* 增加评论
* */
@Slf4j
@RestController
@RequestMapping("/comment")
@CrossOrigin(origins = "http://localhost:82")
public class CommentsController {

    @Autowired
    CommentsService commentsService;

    @GetMapping
    @CrossOrigin(origins = "http://localhost:82")
    public Result getComment(Integer post_id){
        log.info("获取文章id为{}的评论区",post_id);
        List<Comments> CommentsList = commentsService.getComment(post_id);
        log.info("列表为{}",CommentsList);
        return Result.success(CommentsList);
    }

    @PostMapping
    @CrossOrigin(origins = "http://localhost:82")
    public Result addComment(@RequestBody Comments comments){
        log.info("增加新的评论{}",comments);
        commentsService.addComment(comments);
        return Result.success();
    }

    @GetMapping("/search")
    public Result search(String content,String username){
        log.info("特定查询评论");
        List<Comments> searchComments = commentsService.search(content,username);
        return Result.success(searchComments);
    }

    @DeleteMapping("/delete")
    public Result deleteComment(@RequestBody Comments comments){
        log.info("删除评论{}",comments.getComment_id());
        commentsService.delete(comments);
        return Result.success();
    }

}
