ActivityManagerController.php 2.65 KB
<?php

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

use App\Helpers\OperationLog;
use App\Http\Container\AdminSection\Requests\Audition\ActivityManagerCreateRequest;
use App\Http\Container\AdminSection\Requests\Audition\ActivityManagerUpdateRequest;
use App\Models\Activity;
use App\Models\Pivots\ActivityLinkPivot;
use App\Support\Controller;
use Hikoon\LaravelApi\Facades\Response;
use Hikoon\LaravelApi\Support\ApiCode;

class ActivityManagerController extends Controller
{
    /**
     * @param \App\Http\Container\AdminSection\Requests\Audition\ActivityManagerCreateRequest $request
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function store(ActivityManagerCreateRequest $request): Response
    {
        $data     = $request->safe()->toArray();
        $activity = Activity::query()->find($data['activity_id']);
        $activity->links()->wherePivot('type', 'manager')->syncWithPivotValues($data['user_ids'], ['type' => 'manager', 'expand->permission' => $data['expand->permission']], false);
        OperationLog::admin()->createAction()->subject($activity)->content('《%s》新增外部管理员', $activity->getAttribute('song_name'));
        return $this->success(ApiCode::CREATE_SUCCESS);
    }

    /**
     * @param \App\Http\Container\AdminSection\Requests\Audition\ActivityManagerUpdateRequest $request
     * @param \App\Models\Pivots\ActivityLinkPivot                                            $activityManager
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function update(ActivityManagerUpdateRequest $request, ActivityLinkPivot $activityManager): Response
    {
        $activityManager->update($request->safe()->toArray());
        $nickName     = $activityManager->link()->value('nick_name');
        $activityName = $activityManager->activity()->value('song_name');
        OperationLog::admin()->subject($activityManager)->updateAction()->content('《%s》调整外部管理员:%s 的权限', $activityName, $nickName);
        return $this->successWithData($activityManager, ApiCode::UPDATE_SUCCESS);
    }

    /**
     * @param \App\Models\Pivots\ActivityLinkPivot $activityManager
     * @return \Hikoon\LaravelApi\Facades\Response
     */
    public function destroy(ActivityLinkPivot $activityManager): Response
    {
        $nickName     = $activityManager->link()->value('nick_name');
        $activityName = $activityManager->activity()->value('song_name');
        $activityManager->delete();
        OperationLog::admin()->subject($activityManager)->deleteAction()->content('《%s》移除外部管理员:%s', $activityName, $nickName);
        return $this->success(ApiCode::DELETE_SUCCESS);
    }
}