package jnpf.cultivate.controller.app.casebase;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.cultivate.mapper.FtbCultivateCaseBaseLikeMapper;
import jnpf.model.cultivate.po.casebase.FtbCultivateCaseBaseLike;
import jnpf.util.UserProvider;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Title: web案例库点赞
 * @Author: peng.hao
 * @create: 2024/7/22:11:30
 */
@RestController
@RequestMapping("/app/case-base/like")
public class FtbCultivateCaseBaseLikeAppController {

    @Resource
    FtbCultivateCaseBaseLikeMapper caseBaseLikeMapper;

    /**
     * 点赞案例
     * @param id 主键
     * @return
     */
    @GetMapping("/likeTheCase")
    public ActionResult<Object> likeTheCase(@RequestParam String id){
        UserInfo user = UserProvider.getUser();
        FtbCultivateCaseBaseLike like = new FtbCultivateCaseBaseLike();
        like.setLikeUserId(user.getUserId());
        like.setGainedId(id);
        caseBaseLikeMapper.insert(like);
        return ActionResult.success();
    }

    /**
     * 点赞取消
     * @param id
     * @return
     */
    @DeleteMapping("/likeCancel")
    public ActionResult<Object> likeCancel(@RequestParam String id){
        UserInfo user = UserProvider.getUser();
        LambdaUpdateWrapper<FtbCultivateCaseBaseLike> lambdaed = Wrappers.lambdaUpdate();
        lambdaed.eq(FtbCultivateCaseBaseLike::getLikeUserId,user.getUserId());
        lambdaed.eq(FtbCultivateCaseBaseLike::getGainedId,id);
        caseBaseLikeMapper.delete(lambdaed);
        return ActionResult.success();
    }
}
