package com.webchat.controller.client;

import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.common.enums.ClickEvent;
import com.webchat.common.helper.SessionHelper;
import com.webchat.config.annotation.SafeClick;
import com.webchat.config.annotation.ValidateLogin;
import com.webchat.domain.vo.request.MomentSaveOrUpdateVO;
import com.webchat.domain.vo.response.MomentResponseDetailVO;
import com.webchat.service.MomentService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: 程序员王七七 https://www.coderutil.com 网站作者
 * @Date: 2021-7-11 0011 16:05
 * @Description: 无描述信息
 */
@RestController
@RequestMapping("/api/moment")
public class MomentController {

    @Autowired
    private MomentService momentService;

    /**
     * 发布
     * @param vo
     * @return
     */
    @ValidateLogin
    @SafeClick(event = ClickEvent.SUBMIT, time = 10000, message = "10s内只能提交一次")
    @PostMapping("/publish")
    public APIResponseBean<Long> publish(@RequestBody MomentSaveOrUpdateVO vo) {
        Long id = momentService.publish(SessionHelper.getCurrentUserId(), vo);
        return APIResponseBeanUtil.success(id);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public APIResponseBean<Long> delete(@PathVariable Long id) {
        momentService.delete(SessionHelper.getCurrentUserId(), id);
        return APIResponseBeanUtil.success("ok");
    }

    @GetMapping("/timeLine")
    public APIResponseBean<List<MomentResponseDetailVO>> timeLine(
            @RequestParam(value = "lastId", required = false) Long lastId,
            @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        String userId = SessionHelper.getCurrentUserId();
        List<MomentResponseDetailVO> list = momentService.getUserMomentListFromTimeLineCache(userId, size, lastId);
        return APIResponseBeanUtil.success(list);
    }
}
