<?php

namespace Modules\Cms\Http\Controllers\Api;

use App\Api\Controllers\Controller;
use Illuminate\Http\Request;
use Modules\Cms\Http\Resources\CategoryCollection;
use Modules\Cms\Http\Resources\CategoryResource;
use Modules\Cms\Models\Category;

class CategoryController extends Controller
{

    /**
     * Notes   : 分类首页
     * @Date   : 2021/4/19 9:29 上午
     * @Author : < Jason.C >
     * @param  \Illuminate\Http\Request  $request
     * @return mixed
     */
    public function index(Request $request)
    {
        $parentId = $request->get('parent_id', 0);

        if (!is_integer($parentId)) {
            return $this->failed('The param must be integer');
        }

        $result = Category::where('parent_id', $parentId)
                          ->where('status', 1)
                          ->orderBy('order', 'asc')
                          ->get();

        return $this->success(new CategoryCollection($result));
    }

    /**
     * Notes   : 分类的详情
     * @Date   : 2021/4/19 9:29 上午
     * @Author : < Jason.C >
     * @param  \Modules\Cms\Models\Category  $category
     * @return mixed
     */
    public function show(Category $category)
    {

        return $this->success(new CategoryResource($category));
    }

}