package com.ruoyi.cai.controller.app;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.cai.dto.app.query.DynamicAddReq;
import com.ruoyi.cai.dto.app.query.DynamicQuery;
import com.ruoyi.cai.dto.app.query.DynamicStarReq;
import com.ruoyi.cai.dto.app.vo.DynamicListVo;
import com.ruoyi.cai.service.DynamicService;
import com.ruoyi.cai.service.UserFollowDynamicService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.helper.LoginHelper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/dynamic")
@Tag(name = "广场动态相关接口")
public class UserDynamicAppController {

    @Autowired
    private DynamicService dynamicService;
    @Autowired
    private UserFollowDynamicService userFollowDynamicService;

    @GetMapping("/page")
    @Operation(summary = "动态查询动态")
    public TableDataInfo<DynamicListVo> page(PageQuery pageQuery, DynamicQuery query){
        Page<DynamicListVo> resp =  dynamicService.pageApp(pageQuery,query);
        return TableDataInfo.build(resp);
    }

    @PostMapping("/star")
    @Operation(summary = "关注动态")
    public R<Void> star(@RequestBody DynamicStarReq query){
        query.setUserId(LoginHelper.getUserId());
        userFollowDynamicService.star(query);
        return R.ok();
    }

    @PostMapping("/unstar")
    @Operation(summary = "取消关注动态")
    public R<Void> unStar(@RequestBody DynamicStarReq query){
        query.setUserId(LoginHelper.getUserId());
        userFollowDynamicService.unStar(query);
        return R.ok();
    }

    @PostMapping("/push")
    @Operation(summary = "发布动态")
    public R<Void> push(@RequestBody DynamicAddReq res){
        res.setUserId(LoginHelper.getUserId());
        dynamicService.saveDynamic(res);
        return R.ok();
    }

}
