package com.izouma.nineth.web;

import com.izouma.nineth.domain.NewsLike;
import com.izouma.nineth.service.NewsLikeService;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.repo.NewsLikeRepo;
import com.izouma.nineth.utils.ObjUtils;
import com.izouma.nineth.utils.SecurityUtils;
import com.izouma.nineth.utils.excel.ExcelUtils;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/newsLike")
@AllArgsConstructor
public class NewsLikeController extends BaseController {
    private NewsLikeService newsLikeService;
    private NewsLikeRepo    newsLikeRepo;

    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    public NewsLike save(@RequestBody NewsLike record) {
        if (record.getId() != null) {
            NewsLike orig = newsLikeRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
            ObjUtils.merge(orig, record);
            return newsLikeRepo.save(orig);
        }
        return newsLikeRepo.save(record);
    }


    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/all")
    public Page<NewsLike> all(@RequestBody PageQuery pageQuery) {
        return newsLikeService.all(pageQuery);
    }

    @GetMapping("/get/{id}")
    public NewsLike get(@PathVariable Long id) {
        return newsLikeRepo.findById(id).orElseThrow(new BusinessException("无记录"));
    }

    @PostMapping("/del/{id}")
    public void del(@PathVariable Long id) {
        newsLikeRepo.softDelete(id);
    }

    @GetMapping("/excel")
    @ResponseBody
    public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
        List<NewsLike> data = all(pageQuery).getContent();
        ExcelUtils.export(response, data);
    }

    @GetMapping("/{id}/like")
    @ApiOperation("点赞")
    @CacheEvict("news")
    public void like(@PathVariable Long id) {
        newsLikeService.like(SecurityUtils.getAuthenticatedUser().getId(), id);
    }

    @GetMapping("/{id}/unlike")
    @ApiOperation("取消点赞")
    @CacheEvict("news")
    public void unlike(@PathVariable Long id) {
        newsLikeService.unlike(SecurityUtils.getAuthenticatedUser().getId(), id);
    }

    @GetMapping("/{id}/likeRoom")
    @ApiOperation("点赞")
    public void likeRoom(@PathVariable Long id) {
        newsLikeService.likeRoom(SecurityUtils.getAuthenticatedUser().getId(), id);
    }

    @GetMapping("/{id}/unlikeRoom")
    @ApiOperation("取消点赞")
    public void unlikeRoom(@PathVariable Long id) {
        newsLikeService.unlikeRoom(SecurityUtils.getAuthenticatedUser().getId(), id);
    }

    @GetMapping("/{id}/likeAuction")
    @ApiOperation("点赞拍卖")
    public void likeAuction(@PathVariable Long id) {
        newsLikeService.likeAuction(SecurityUtils.getAuthenticatedUser().getId(), id);
    }

    @GetMapping("/{id}/unlikeAuction")
    @ApiOperation("取消点赞拍卖")
    public void unlikeAuction(@PathVariable Long id) {
        newsLikeService.unlikeAuction(SecurityUtils.getAuthenticatedUser().getId(), id);
    }
}

