<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

use App\Models\Blog;

class BlogValidation extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        if (!auth()->check()) {
            return false;
        }

        if ($this->route('id')) {
            $blog = Blog::findorFail($this->route('id'));
            return $this->user()->can('update', $blog);
        }
        return $this->user()->can('create', Blog::class);
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'version.name' => 'required|string',
            'version.title' => 'string|nullable',
            'version.unlisted' => 'boolean',
        ];
    }
}
