package com.ruoyi.web.controller.front;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mingxue.domain.QuestionCollection;
import com.ruoyi.mingxue.service.IQuestionCollectionService;
import io.lettuce.core.dynamic.annotation.Param;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

@Api(tags = "前端V2 题库")
@RestController
@RequestMapping("front/questions")
public class FrontQuestionsController extends BaseController {
    @Autowired
    private IQuestionCollectionService questionCollectionService;

    @ApiOperation("收藏试题")
    @PostMapping("collect")
    public AjaxResult collect(Long questionId) {
        QuestionCollection questionCollection = new QuestionCollection();
        questionCollection.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        questionCollection.setQuestionId(questionId);
        if(CollectionUtils.isEmpty(questionCollectionService.selectQuestionCollectionList(questionCollection))) {
            return AjaxResult.success(questionCollectionService.insertQuestionCollection(questionCollection));
        }
        return AjaxResult.success(1);
    }

    @ApiOperation("取消收藏试题")
    @PostMapping("cancelCollect")
    public AjaxResult cancelQuestionCollection(Long questionId) {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        return AjaxResult.success(questionCollectionService.deleteQuestionCollectionById(questionId, userId));
    }
}
