package com.cirle.scientific.questionnaire.service;

import com.cirle.scientific.questionnaire.entity.QuestionEntity;
import com.cirle.scientific.questionnaire.exception.QuestionNotFoundException;
import com.cirle.scientific.questionnaire.repository.QuestionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class QuestionService {

    @Autowired
    private QuestionRepository questionRepository;

    /**
     * 查询问题类型
     *
     * @param id 问题ID
     * @return 可选的问题类型
     */
    public Optional<QuestionEntity.Type> queryTypeById(String id) {
        return questionRepository.findTypeById(id);
    }

    /**
     * 获取问题类型
     *
     * @param id 问题ID
     * @return 问题类型
     */
    public QuestionEntity.Type getTypeById(String id) throws QuestionNotFoundException {
        Optional<QuestionEntity.Type> maybeQuestionType = queryTypeById(id);
        if (!maybeQuestionType.isPresent()) {
            throw new QuestionNotFoundException();
        }
        return maybeQuestionType.get();
    }

}
