<?php
namespace App\Http\Controllers\V2;
use App\Http\Controllers\BaseController;
use App\Repositories\PlatformContentRepository;
use App\Transformers\PlatformContentTransformer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use League\Fractal\Resource\Collection;
use League\Fractal\Manager;
/**
 * Created by PhpStorm.
 * User: durong
 * Date: 2019/6/17
 * Time: 下午2:04
 */

class PlatformContentController extends BaseController
{
    public function __construct(PlatformContentRepository $platformContentRepository)
    {
        $this->platformContentRepository = $platformContentRepository;
    }

    //平台内容列表
    public function index(Request $request)
    {
        $platformContent = $this->platformContentRepository->index($request->all());

        $fractal = new Manager();
        $resource = new Collection($platformContent, new PlatformContentTransformer());
        $resource->setPaginator(new IlluminatePaginatorAdapter($platformContent));
        $data = $fractal->createData($resource)->toArray();
        $data['extra'] = [
            'filters' => [
                'id',
            ],
            'columns' => [
                'id',
                'title',
                'content',
                'updated_at'
            ]
        ];
        return $data;
    }

    //新增平台内容
    public function create(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'title' => 'required|string',
            'content' => 'required|string',
        ]);
        if ($validator->fails()) {
            return $this->response->error($validator->errors()->first(), 500);
        }

        return  $this->platformContentRepository->create($request->all());

    }

    //修改平台内容
    public function edit(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'required|exists:platform_content',
            'content' => 'required',
            'title' => 'string',
        ]);

        if ($validator->fails()) {
            return $this->response->error($validator->errors()->first(), 500);
        }
        return  $this->platformContentRepository->edit($request->all());
    }
}