<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\QuestionModel;
use Illuminate\Http\Request;

class QuestionController extends Controller
{
    /**
     * 创建问题
     * @param  Request  $request
     * @return array
     */
    public function create(Request $request)
    {
        $this->validate($request, [
            'title' => 'required',
            'content' => 'required',
            'type' => 'required'
        ]);
        $question = new QuestionModel();
        $question->fill($request->all());
        $question->save();
        return response([
            'code' => 200,
            'message' => 'success'
        ]);
    }

    /**
     * 更新问题
     * @param  Request  $request
     * @param  int  $question_id
     * @return array
     */
    public function update(Request $request, int $question_id)
    {
        $question = QuestionModel::findOrFail($question_id);
        $question->fill($request->all());
        $question->save();
        return response([
            'code' => 200,
            'message' => 'success'
        ]);
    }

    /**
     * 删除问题
     * @param  int  $question_id
     * @return array
     * @throws \Exception
     */
    public function delete(int $question_id)
    {
        $question = QuestionModel::findOrFail($question_id);
        $question->delete();
        return response([
            'code' => 200,
            'message' => 'success'
        ]);
    }

    /**
     * 获取问题
     * @param  int  $question_id
     * @return array
     * @throws \Exception
     */
    public function show(int $question_id)
    {
        $question = QuestionModel::find($question_id);
        return response([
            'code' => 200,
            'message' => 'success',
            'data' => $question
        ]);
    }

    /**
     * 问题列表
     * @return array
     * @throws \Exception
     */
    public function index(Request $request)
    {
        $questions = QuestionModel::get();
        return response([
            'code' => 200,
            'message' => 'success',
            'data' => $questions
        ]);
    }
}
