<?php

namespace App\Http\Controllers\Manage\Book;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Modules\Book\Services\BookDomainService;
use App\Http\Controllers\Manage\Book\Transformers\BookDomainTransformer;
class BookDomainController extends Controller
{

    /**
     * @apiDefine Book 图书模块
     */


    /**
     * @apiVersion 1.0.0
     * @apiDescription 获取图书域名
     * @api {get} books/domain 获取图书域名
     * @apiGroup Book
     * @apiName getBookDomain
     * @apiSuccess {int}         code          态码
     * @apiSuccess {String}      msg           信息
     * @apiSuccess {object}      data          结果集
     * @apiSuccess {Int}         data.id       id
     * @apiSuccess {String}      data.domain   域名
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *       {
     *           code: 0,
     *           msg: "",
     *           data: [
     *               {
     *                   id: 1,
     *                   domain: "aizhuishu.com"
     *               },
     *               {
     *                   id: 2,
     *                   domain: "leyue"
     *               }
     *           ]
     *       }
     */
    public function getBookDomain(){
        $res = BookDomainService::getEnableDomain();
        return response()->collection(new BookDomainTransformer(),$res);
    }


    /**
     * @apiVersion 1.0.0
     * @apiDescription 图书域名添加
     * @api {post} books/domain 图书域名添加
     * @apiParam {String} domain 图书域名添加
     * @apiGroup Book
     * @apiName addBookDomain
     * @apiSuccess {int}         code 状态码
     * @apiSuccess {String}      msg  信息
     * @apiSuccess {object}      data 结果集
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *       code: 0,
     *       msg: "",
     *       data: {}
     *     }
     */
    public function addBookDomain(Request $request){
        $domain = $request->input('domain');
        if(empty($domain)){
            return response()->error('PARAM_EMPTY');
        }

        if(BookDomainService::isExists($domain)){
            return response()->error('PARAM_ERROR');
        }
        BookDomainService::addDomain(['domain'=>$domain,'is_enabled'=>1]);
        return response()->success();
    }
}
