package com.mcwl.web.controller.communityCenter;


import com.mcwl.common.core.domain.AjaxResult;
import com.mcwl.common.core.domain.R;
import com.mcwl.common.core.page.TableDataInfo;
import com.mcwl.communityCenter.domain.dto.PublishDetailRes;
import com.mcwl.communityCenter.domain.dto.PublishPageRes;
import com.mcwl.communityCenter.domain.dto.PublishRes;
import com.mcwl.communityCenter.domain.vo.PublishVo;
import com.mcwl.communityCenter.service.PublishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Objects;

/**
 * 发布
 */
@Api(tags = "社区帖子发布")
@RestController
@RequestMapping("publish")
@RequiredArgsConstructor
public class PublishController {

    private final PublishService publishService;


    /**
     * 获取发布列表
     */
    @ApiOperation(value = "获取发布列表")
    @PostMapping("list")
    public TableDataInfo list(@RequestBody @Valid PublishPageRes publishPageRes) {
        return publishService.getPublishList(publishPageRes);
    }

    /**
     * 获取发布详情
     */
    @ApiOperation(value = "获取发布详情")
    @PostMapping("detail")
    public R<PublishVo> getPublishDetail(@RequestBody @Valid PublishDetailRes publishDetailRes) {

        PublishVo publishVo = publishService.getDetail(publishDetailRes);
        if (Objects.isNull(publishVo)) {
            return R.fail("获取详情失败");
        }
        return R.ok(publishVo);
    }

    /**
     * 添加
     */
    @ApiOperation(value = "发布")
    @PostMapping("publish")
    public R<Object> publish(@RequestBody @Valid PublishRes publishRes) {
        return publishService.publish(publishRes);
    }

    /**
     * 获取发布图片列表
     */
    @ApiOperation(value = "获取发布图片列表")
    @PostMapping("publishImage")
    public TableDataInfo publishImage(@RequestBody @Valid PublishPageRes publishPageRes) {
        return publishService.publishImage(publishPageRes);
    }

    /**
     * 获取发布文件列表
     */
    @ApiOperation(value = "获取发布文件列表")
    @PostMapping("publishFile")
    public TableDataInfo publishFile(@RequestBody @Valid PublishPageRes publishPageRes) {
        return publishService.publishFile(publishPageRes);
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除")
    @GetMapping("remove")
    public R<Object> deletePublish(@NotNull(message = "id不能为空") @ApiParam(value = "id") @Valid Long id) {
        publishService.removeById(id);
        return R.ok();
    }

}
