PushConfigController.php 2.67 KB
<?php

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

use App\Helpers\OperationLog;
use App\Http\Container\AdminSection\Requests\System\BrokerPushConfigRequest;
use App\Models\BrokerPushConfig;
use Auth;
use Hikoon\LaravelApi\Facades\Response;
use Hikoon\LaravelApi\Support\ApiCode;
use Hikoon\LaravelApi\Support\ApiController;
use Illuminate\Http\Request;
use Str;

class PushConfigController extends ApiController
{
    /**
     * @param \Illuminate\Http\Request $request
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function index(Request $request): Response
    {
        $pageSize  = $request->get('pageSize', 20);
        $fetchType = $request->get('fetchType', 'page');
        $filter    = $request->except(['page', 'pageSize', 'fetchType']);
        $build     = BrokerPushConfig::query()->filter($filter);

        if ($fetchType === 'all') {
            return $this->successWithData($build->get());
        }

        return $this->successWithData($build->paginate($pageSize));
    }

    /**
     * @param \App\Http\Container\AdminSection\Requests\System\BrokerPushConfigRequest $request
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function store(BrokerPushConfigRequest $request): Response
    {
        $config = BrokerPushConfig::query()->create($request->safe()->merge(['user_id' => Auth::id()])->toArray());
        OperationLog::admin()->subject($config)->createAction()->content('推送配置《%s》', Str::limit($config->getAttribute('identifier')));
        return $this->successWithData($config, ApiCode::CREATE_SUCCESS);
    }

    /**
     * @param \App\Http\Container\AdminSection\Requests\System\BrokerPushConfigRequest $request
     * @param \App\Models\BrokerPushConfig                                             $pushConfig
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function update(BrokerPushConfigRequest $request, BrokerPushConfig $pushConfig): Response
    {
        $pushConfig->update($request->safe()->toArray());
        OperationLog::admin()->subject($pushConfig)->updateAction()->content('推送配置《%s》', Str::limit($pushConfig->getAttribute('identifier')));
        return $this->successWithData($pushConfig, ApiCode::UPDATE_SUCCESS);
    }

    /**
     * @param \App\Models\BrokerPushConfig $pushConfig
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function destroy(BrokerPushConfig $pushConfig): Response
    {
        $pushConfig->delete();
        OperationLog::admin()->subject($pushConfig)->deleteAction()->content('推送配置《%s》', Str::limit($pushConfig->getAttribute('identifier')));
        return $this->success(ApiCode::DELETE_SUCCESS);
    }
}