BannerController.php 3.38 KB
<?php

namespace App\Http\Container\AdminSection\Controllers\System;

use App\Helpers\OperationLog;
use App\Http\Request\BooleanStatusRequest;
use App\Http\Container\AdminSection\Requests\System\BannerRequest;
use App\Models\Banner;
use App\Support\Controller;
use Auth;
use Hikoon\LaravelApi\Facades\Response;
use Hikoon\LaravelApi\Support\ApiCode;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\URL;

class BannerController extends Controller
{
    /**
     * @param \Illuminate\Http\Request $request
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function index(Request $request): Response
    {
        $pageSize = $request->get('pageSize', 20);
        $filter   = $request->except('page', 'pageSize');
        $data     = Banner::filter($filter)->paginate($pageSize);
        return $this->successWithData($data);
    }

    /**
     * @param \App\Http\Container\AdminSection\Requests\System\BannerRequest $request
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function store(BannerRequest $request): Response
    {
        $attribute = $request->safe()->merge(['user_id' => Auth::id()])->toArray();
        Banner::created(static fn(Banner $banner) => $banner->getAttribute('type') === 4 && $banner->update(['content_picture' => URL::asset('/app/banners/' . $banner->getKey())]));
        $banner = Banner::query()->create($attribute);
        OperationLog::admin()->createAction()->subject($banner)->content('轮播图《%s》', $banner->getAttribute('name'));
        return $this->successWithData($banner, ApiCode::CREATE_SUCCESS);
    }

    /**
     * @param \App\Http\Container\AdminSection\Requests\System\BannerRequest $request
     * @param \App\Models\Banner                                             $banner
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function update(BannerRequest $request, Banner $banner): Response
    {
        $attribute = $request->safe()->toArray();
        if ($banner->getAttribute('type') === 4) {
            $attribute['content_picture'] = URL::asset('/app/banners/' . $banner->getKey());
        }
        $banner->update($attribute);
        OperationLog::admin()->updateAction()->subject($banner)->content('轮播图《%s》', $banner->getAttribute('name'));
        return $this->successWithData($banner, ApiCode::UPDATE_SUCCESS);
    }

    /**
     * @param \App\Http\Request\BooleanStatusRequest $request
     * @param \App\Models\Banner                                             $banner
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function changeStatus(BooleanStatusRequest $request, Banner $banner): Response
    {
        $status = $request->safe()->offsetGet('status');
        $banner->update(['status' => $status]);
        OperationLog::admin()->statusAction()->subject($banner)->content('轮播图《%s》%s', $banner->getAttribute('name'), $banner->getAttribute('status')?->label());
        return $this->success(ApiCode::UPDATE_SUCCESS);
    }

    /**
     * @param \App\Models\Banner $banner
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function destroy(Banner $banner): Response
    {
        Banner::query()->whereKey($banner->getKey())->limit(1)->delete();
        OperationLog::admin()->deleteAction()->subject($banner)->content('轮播图《%s》', $banner->getAttribute('name'));
        return $this->success(ApiCode::DELETE_SUCCESS);
    }
}