<?php

namespace App\Http\Controllers\Api\V1;

use App\Models\WxArticleInfoModel;
use App\Services\Base\ErrorCode;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class FindController extends Controller
{/**
 * @api {get} /api/find/index 发现
 * @apiDescription 发现
 * @apiGroup Find
 * @apiParam {string}   appid          appid
 * @apiParam {int}   [page]          分页参数
 * @apiPermission none
 * @apiVersion 0.1.0
 * @apiSuccessExample {json} Success-Response:
 *  HTTP/1.1 200 OK
{
    "status": true,
    "status_code": 0,
    "message": "",
    "data": {
        "current_page": 1,
        "data": [
            {
                "id": 1,
                "title": "test",
                "pic": "/upload/article/20171013/91a15c6f30ad510db5165dad5ac1fff9.jpg",
                "info": "驱蚊器翁",
                "viewed": 0,
                "sort": 1,
                "created_at": "2017-10-13 03:34:02",
                "updated_at": "2017-10-13 03:34:02"
            },
        ],
        "first_page_url": "http://www.s1.com/api/find/index?page=1",
        "from": 1,
        "last_page": 1,
        "last_page_url": "http://www.s1.com/api/find/index?page=1",
        "next_page_url": null,
        "path": "http://www.s1.com/api/find/index",
        "per_page": 10,
        "prev_page_url": null,
        "to": 3,
        "total": 3
    }
}
 * @apiErrorExample {json} Error-Response:
 * HTTP/1.1 400 Bad Request
 */
    public function index(Request $request)
    {
        $validator = Validator::make($request->all(),
            [
                'appid'         => 'required',
            ],
            [
                'appid.required'        => 'appid不存在',
            ]
        );

        if ($validator->fails()) {
            return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
        }
        $appid = $request->input('appid');
        $articles = WxArticleInfoModel::where('appid',$appid)->orderBy('sort')->paginate();
        return $this->api($articles);
    }
}
