<?php

namespace Modules\Cms\Http\Controllers\Api;

use App\Api\Controllers\Controller;
use Illuminate\Http\Request;
use Modules\Cms\Http\Resources\TagCollection;
use Modules\Cms\Http\Resources\TagResource;
use Modules\Cms\Models\Tag;

class TagController extends Controller
{

    /**
     * Notes   : 标签列表
     * @Date   : 2021/4/25 1:31 下午
     * @Author : < Jason.C >
     * @param  \Illuminate\Http\Request  $request
     * @return mixed
     */
    public function index(Request $request)
    {
        $name = $request->name;

        $tags = Tag::when($name, function ($query) use ($name) {
            $query->where('name', 'like', "%{$name}%");
        })->orderByDesc('id')->paginate();

        return $this->success(new TagCollection($tags));
    }

    public function show(Tag $tag)
    {
        return $this->success(new TagResource($tag));
    }

}