package com.xindazhou.business.controller.app.community.post;

import com.xindazhou.business.controller.app.community.post.vo.PostCreateReqVO;
import com.xindazhou.business.controller.app.community.post.vo.PostPageReqVO;
import com.xindazhou.business.controller.app.community.post.vo.PostRespVO;
import com.xindazhou.business.controller.app.community.post.vo.PostUpdateReqVO;
import com.xindazhou.business.service.community.post.PostService;
import com.xindazhou.framework.common.pojo.CommonResult;
import com.xindazhou.framework.common.pojo.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.xindazhou.framework.common.pojo.CommonResult.success;

/**
 * 移动端 - 帖子 Controller
 *
 * @author xindazhou
 */
@Tag(name = "移动端 - 帖子管理")
@RestController("appCommunityPostController")
@RequestMapping("/app-api/community/post")
@Validated
@Slf4j
public class PostController {

    @Resource
    private PostService postService;

    @PostMapping("/create")
    @Operation(summary = "创建帖子")
    public CommonResult<Long> createPost(@Valid @RequestBody PostCreateReqVO createReqVO) {
        Long postId = postService.createPost(createReqVO);
        return success(postId);
    }

    @PutMapping("/update")
    @Operation(summary = "更新帖子")
    public CommonResult<Boolean> updatePost(@Valid @RequestBody PostUpdateReqVO updateReqVO) {
        postService.updatePost(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除帖子")
    @Parameter(name = "id", description = "帖子ID", required = true, example = "1")
    public CommonResult<Boolean> deletePost(@RequestParam("id") Long id) {
        postService.deletePost(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得帖子")
    @Parameter(name = "id", description = "帖子ID", required = true, example = "1")
    public CommonResult<PostRespVO> getPost(@RequestParam("id") Long id) {
        PostRespVO post = postService.getPost(id);
        return success(post);
    }

    @GetMapping("/page")
    @Operation(summary = "获得帖子分页列表")
    public CommonResult<PageResult<PostRespVO>> getPostPage(@Valid PostPageReqVO pageReqVO) {
        PageResult<PostRespVO> pageResult = postService.getPostPage(pageReqVO);
        return success(pageResult);
    }

}

