<?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\Banner\BannerResource;
use Modules\Mall\Models\Banner;

class BannerController extends Controller
{

    /**
     * Notes   : 获取banner的接口
     * @Date   : 2021/6/3 11:28 上午
     * @Author : <Jason.C>
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request): JsonResponse
    {
        $position = $request->position;
        $shopId   = $request->shop_id;

        // 店铺ID为null的是平台轮播
        $banners = Banner::when($shopId, function (Builder $query) use ($shopId) {
            $query->where('shop_id', $shopId);
        })->when(is_numeric($position), function (Builder $query) use ($position) {
            $query->ofPosition($position);
        })->get();

        return $this->success(BannerResource::collection($banners));
    }

}