ProjectDynamicController.php 2.46 KB
<?php


namespace App\Http\Container\AppSection\Controllers;


use App\Helpers\JsonResource;
use App\Http\Container\AppSection\Requests\ProjectDynamicCreateRequest;
use App\Http\Container\AppSection\Requests\ProjectDynamicQueryRequest;
use App\Http\Container\AppSection\Requests\ProjectDynamicUpdateRequest;
use App\Models\Pivots\UserProjectPivot;
use App\Models\ProjectDynamic;
use Hikoon\LaravelApi\Support\ApiController;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;


class ProjectDynamicController extends ApiController
{
    /**
     * @param \App\Http\Container\AppSection\Requests\ProjectDynamicQueryRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(ProjectDynamicQueryRequest $request): JsonResponse
    {
        $filter = $request->safe()->except('pageSize');
        $result = ProjectDynamic::filter($filter)
            ->orderByDesc('is_top')->orderByDesc('created_at')
            ->paginate($request->get('pageSize', 20));
        foreach ($result as $item) {
            $item['is_master'] = UserProjectPivot::query()->where(['project_id' => $item['project_id'], 'user_id' => Auth::id()])->exists();
        }
        return JsonResource::success(JsonResource::SUCCESS, $result);
    }

    /**
     * @param \App\Http\Container\AppSection\Requests\ProjectDynamicCreateRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(ProjectDynamicCreateRequest $request): JsonResponse
    {
        $data   = $request->safe()->toArray();
        $result = ProjectDynamic::create($data);
        return JsonResource::success(JsonResource::SUCCESS, $result);
    }


    public function show(Request $request): JsonResponse
    {
        $id      = $request->dynamic;
        $dynamic = ProjectDynamic::query()->find($id);
        return JsonResource::success(JsonResource::SUCCESS, $dynamic);
    }


    public function update(ProjectDynamicUpdateRequest $request): JsonResponse
    {
        $id      = $request->dynamic;
        $dynamic = ProjectDynamic::query()->find($id);
        $dynamic->update($request->safe()->toArray());
        return JsonResource::success(JsonResource::SUCCESS, $dynamic);
    }

    public function destroy(Request $request): JsonResponse
    {
        $id      = $request->dynamic;
        $dynamic = ProjectDynamic::query()->find($id);
        $dynamic->delete();
        return JsonResource::success(JsonResource::DELETE_SUCCESS);
    }

}