<?php

namespace Modules\Mall\Http\Controllers\Api;

use App\Api\Controllers\Controller;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Modules\Mall\Http\Resources\Api\Goods\TagResource;
use Modules\Mall\Models\Tag;

class TagController extends Controller
{

    /**
     * Notes   : 标签列表
     * @Date   : 2021/6/3 2:20 下午
     * @Author : <Jason.C>
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request): JsonResponse
    {
        $shopId = $request->shop_id;
        $limit  = $request->limit;

        $list = Tag::when($shopId, function (Builder $query) use ($shopId) {
            $query->where('shop_id', $shopId);
        })->when($limit, function (Builder $query) use ($limit) {
            $query->take($limit);
        })->get();

        return $this->success(TagResource::collection($list));
    }

}