<?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\Category\CategoryResource;
use Modules\Mall\Models\Category;

class CategoryController extends Controller
{

    /**
     * Notes   : 商品分类列表
     *
     * @Date   : 2021/6/3 12:08 下午
     * @Author : <Jason.C>
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request): JsonResponse
    {
        $shopId = $request->shop_id;

        $list = Category::query()
            ->where('id', '<>', 2)
            ->when($shopId, function (Builder $query) use ($shopId) {
                $query->where('shop_id', $shopId);
            })->where('parent_id', 0)->get();

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

    }

}