ProjectDynamicController.php
2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?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);
}
}