<?php

namespace App\Http\Controllers\Channel\Book;

use App\Modules\Book\Services\BookChannelScoreService;
use Illuminate\Http\Request;
use App\Http\Controllers\Channel\BaseController;
use Hashids;

class BookChannelScoreController extends BaseController
{
    /**
     * @apiDefine Book 图书模块
     */


    /**
     * @apiVersion 1.0.0
     * @apiDescription 自主评分
     * @api {post} book/score 自主评分
     * @apiGroup Book
     * @apiParam {String} score 评分(A,B,C,D...)
     * @apiParam {Number} bid 图书id
     * @apiName score
     * @apiSuccess  {int}  code 状态码
     * @apiSuccess  {String}  msg 信息
     * @apiSuccess  {object}  data 结果集
     * @apiSuccessExample {json} Success-Response:
     *     {
     *       "code": 0,
     *       "msg": "",
     *       "data": {
     *       }
     */
    public function score(Request $request)
    {
        $channel_id = $this->getChannelId();
        $score = $request->post('score');
        $bid = $request->post('bid');
        if (empty($score) || empty($bid)) {
            return response()->error("PARAM_EMPTY");
        }

        if (!is_numeric($bid)) {
            $bid = Hashids::decode($bid)[0];
        }

        if (strlen($score) != 1 || is_numeric($score) || !in_array(strtoupper($score), range('A', 'Z'))) {
            //return response()->error("PARAM_EMPTY");
        }

        BookChannelScoreService::score($channel_id, strtoupper($score), $bid);
        return response()->success();
    }
}
