package com.luojia_channel.modules.post.controller;


import com.luojia_channel.common.domain.Result;
import com.luojia_channel.common.domain.page.PageResponse;
import com.luojia_channel.modules.post.dto.req.PostSaveDTO;
import com.luojia_channel.modules.post.dto.req.PostPageQueryDTO;
import com.luojia_channel.modules.post.dto.resp.PostBasicInfoDTO;
import com.luojia_channel.modules.post.entity.Post;
import com.luojia_channel.modules.post.service.PostService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RequiredArgsConstructor
@RestController
@RequestMapping("/post")
public class PostController {

    private final PostService postService;

    // 创建帖子
    @PostMapping
    public Result<Void> createPost(@RequestBody PostSaveDTO postSaveDTO) {
        postService.createPost(postSaveDTO);
        return Result.success();
    }

    // 设置帖子封面
    @PostMapping("/cover")
    public Result<String> setCover(@RequestParam("file") MultipartFile file){
        return Result.success(postService.setCover(file));
    }

    // 更新帖子
    @PutMapping
    public Result<Void> updatePost(@RequestBody PostSaveDTO postSaveDTO) {
        postService.updatePost(postSaveDTO);
        return Result.success();
    }

    // 删除帖子
    @DeleteMapping
    public Result<Void> deletePost(@RequestParam("id") Long id) {
        postService.deletePost(id);
        return Result.success();
    }

    // 根据ID获取帖子详情
    @GetMapping("/detail")
    public Post getPostDetail(@RequestParam("id") Long id) {
        return postService.getPostDetail(id);
    }

    // 分页查询帖子
    @GetMapping("/list")
    public Result<PageResponse<PostBasicInfoDTO>> pagePost(@RequestBody PostPageQueryDTO postPageQueryDTO) {
        return Result.success(postService.pagePost(postPageQueryDTO));
    }

    // 查看自己的帖子
    @GetMapping("/of/me")
    public Result<PageResponse<PostBasicInfoDTO>> pagePostOfMe(@RequestBody PostPageQueryDTO postPageQueryDTO) {
        return Result.success(postService.pagePostOfMe(postPageQueryDTO));
    }
}
