ProjectController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Project;
  4. use Illuminate\Http\Request;
  5. use App\Http\Api\AuthApi;
  6. use App\Http\Api\StudioApi;
  7. use App\Http\Resources\ProjectResource;
  8. use Illuminate\Support\Str;
  9. use Illuminate\Support\Facades\Log;
  10. class ProjectController extends Controller
  11. {
  12. /**
  13. * Display a listing of the resource.
  14. *
  15. * @return \Illuminate\Http\Response
  16. */
  17. public function index(Request $request)
  18. {
  19. $user = AuthApi::current($request);
  20. if (!$user) {
  21. Log::error('notification auth failed {request}', ['request' => $request]);
  22. return $this->error(__('auth.failed'), 401, 401);
  23. }
  24. switch ($request->get('view')) {
  25. case 'studio':
  26. $table = Project::where('owner_id', $user['user_uid'])
  27. ->whereNull('parent_id');
  28. if ($request->get('type', 'normal') !== 'all') {
  29. $table = $table->where('type', $request->get('type', 'normal'));
  30. }
  31. break;
  32. case 'project-tree':
  33. $table = Project::where('id', $request->get('project_id'))
  34. ->orWhereJsonContains('path', $request->get('project_id'));
  35. break;
  36. default:
  37. # code...
  38. break;
  39. }
  40. if ($request->has('keyword')) {
  41. $table = $table->where('title', 'like', '%' . $request->get('keyword') . '%');
  42. }
  43. if ($request->has('status')) {
  44. $table = $table->whereIn('status', explode(',', $request->get('status')));
  45. }
  46. $count = $table->count();
  47. $sql = $table->toSql();
  48. Log::debug('sql', ['sql' => $sql]);
  49. $table = $table->orderBy($request->get('order', 'created_at'), $request->get('dir', 'desc'));
  50. $table = $table->skip($request->get("offset", 0))
  51. ->take($request->get('limit', 10000));
  52. $result = $table->get();
  53. return $this->ok(
  54. [
  55. "rows" => ProjectResource::collection($result),
  56. "count" => $count,
  57. ]
  58. );
  59. }
  60. public static function canEdit($user_uid, $studio_uid)
  61. {
  62. return $user_uid == $studio_uid;
  63. }
  64. /**
  65. * Store a newly created resource in storage.
  66. *
  67. * @param \Illuminate\Http\Request $request
  68. * @return \Illuminate\Http\Response
  69. */
  70. public function store(Request $request)
  71. {
  72. //
  73. $user = AuthApi::current($request);
  74. if (!$user) {
  75. return $this->error(__('auth.failed'), 401, 401);
  76. }
  77. $studioId = StudioApi::getIdByName($request->get('studio_name'));
  78. if (!self::canEdit($user['user_uid'], $studioId)) {
  79. return $this->error(__('auth.failed'), 403, 403);
  80. }
  81. $new = Project::firstOrNew(['id' => $request->get('id')]);
  82. if (Str::isUuid($request->get('id'))) {
  83. $new->id = $request->get('id');
  84. } else {
  85. $new->id = Str::uuid();
  86. }
  87. $new->title = $request->get('title');
  88. $new->description = $request->get('description');
  89. $new->parent_id = $request->get('parent_id');
  90. $new->editor_id = $user['user_uid'];
  91. $new->owner_id = $studioId;
  92. $new->type = $request->get('type', 'normal');
  93. if (Str::isUuid($request->get('parent_id'))) {
  94. $parentPath = Project::where('id', $request->get('parent_id'))->value('path');
  95. $parentPath = json_decode($parentPath);
  96. if (!is_array($parentPath)) {
  97. $parentPath = array();
  98. }
  99. array_push($parentPath, $new->parent_id);
  100. $new->path = json_encode($parentPath, JSON_UNESCAPED_UNICODE);
  101. }
  102. $new->save();
  103. return $this->ok(new ProjectResource($new));
  104. }
  105. /**
  106. * Display the specified resource.
  107. *
  108. * @param \App\Models\Project $project
  109. * @return \Illuminate\Http\Response
  110. */
  111. public function show(Project $project)
  112. {
  113. //
  114. return $this->ok(new ProjectResource($project));
  115. }
  116. /**
  117. * Show the form for editing the specified resource.
  118. *
  119. * @param \App\Models\Project $project
  120. * @return \Illuminate\Http\Response
  121. */
  122. public function edit(Project $project)
  123. {
  124. //
  125. }
  126. /**
  127. * Update the specified resource in storage.
  128. *
  129. * @param \Illuminate\Http\Request $request
  130. * @param \App\Models\Project $project
  131. * @return \Illuminate\Http\Response
  132. */
  133. public function update(Request $request, Project $project)
  134. {
  135. //
  136. }
  137. /**
  138. * Remove the specified resource from storage.
  139. *
  140. * @param \App\Models\Project $project
  141. * @return \Illuminate\Http\Response
  142. */
  143. public function destroy(Project $project)
  144. {
  145. //
  146. }
  147. }