MaterialController.php 2.33 KB
<?php

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

use App\Helpers\OperationLog;
use App\Http\Container\AdminSection\Requests\System\MaterialRequest;
use App\Models\SystemMaterial;
use App\Support\Controller;
use Hikoon\LaravelApi\Facades\Response;
use Hikoon\LaravelApi\Support\ApiCode;
use Illuminate\Http\Request;

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

        return $this->successWithData($data);
    }

    /**
     * @param \App\Http\Container\AdminSection\Requests\System\MaterialRequest $request
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function store(MaterialRequest $request): Response
    {
        $attributes = $request->safe()->offsetGet('data');
        $ids        = [];
        foreach ($attributes as $attribute) {
            $ids[] = SystemMaterial::query()->create($attribute)?->getKey();
        }
        OperationLog::admin()->createAction()->subject(new SystemMaterial(['id' => 0]))->content('素材:%s', implode(',', $ids));
        return $this->success(ApiCode::CREATE_SUCCESS);
    }

    /**
     * @param \App\Http\Container\AdminSection\Requests\System\MaterialRequest $request
     * @param \App\Models\SystemMaterial                                       $material
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function update(MaterialRequest $request, SystemMaterial $material): Response
    {
        $material->update($request->validated());
        OperationLog::admin()->updateAction()->subject($material)->content('素材:%s', $material->getKey());
        return $this->success(ApiCode::UPDATE_SUCCESS);
    }

    /**
     * @param \App\Models\SystemMaterial $material
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function destroy(SystemMaterial $material): Response
    {
        $material->delete();
        OperationLog::admin()->deleteAction()->subject($material)->content('素材:%s', $material->getKey());
        return $this->success(ApiCode::DELETE_SUCCESS);
    }
}