ProjectController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. $studioId = StudioApi::getIdByName($request->get('studio'));
  25. switch ($request->get('view')) {
  26. case 'studio':
  27. $table = Project::where('owner_id', $studioId)
  28. ->whereNull('parent_id')
  29. ->where('type', $request->get('type', 'instance'));
  30. break;
  31. case 'project-tree':
  32. $table = Project::where('uid', $request->get('project_id'))
  33. ->orWhereJsonContains('path', $request->get('project_id'));
  34. break;
  35. case 'community':
  36. $table = Project::where('owner_id', '<>', $studioId)
  37. ->whereNull('parent_id')
  38. ->where('type', $request->get('type', 'instance'));
  39. break;
  40. default:
  41. return $this->error('view', 200, 200);
  42. break;
  43. }
  44. if ($request->has('keyword')) {
  45. $table = $table->where('title', 'like', '%' . $request->get('keyword') . '%');
  46. }
  47. if ($request->has('status')) {
  48. $table = $table->whereIn('status', explode(',', $request->get('status')));
  49. }
  50. $count = $table->count();
  51. $sql = $table->toSql();
  52. Log::debug('sql', ['sql' => $sql]);
  53. $table = $table->orderBy($request->get('order', 'id'), $request->get('dir', 'asc'));
  54. $table = $table->skip($request->get("offset", 0))
  55. ->take($request->get('limit', 10000));
  56. $result = $table->get();
  57. return $this->ok(
  58. [
  59. "rows" => ProjectResource::collection($result),
  60. "count" => $count,
  61. ]
  62. );
  63. }
  64. public static function canEdit($user_uid, $studio_uid)
  65. {
  66. return $user_uid == $studio_uid;
  67. }
  68. /**
  69. * Store a newly created resource in storage.
  70. *
  71. * @param \Illuminate\Http\Request $request
  72. * @return \Illuminate\Http\Response
  73. */
  74. public function store(Request $request)
  75. {
  76. //
  77. $user = AuthApi::current($request);
  78. if (!$user) {
  79. return $this->error(__('auth.failed'), 401, 401);
  80. }
  81. $studioId = StudioApi::getIdByName($request->get('studio_name'));
  82. if (!self::canEdit($user['user_uid'], $studioId)) {
  83. return $this->error(__('auth.failed'), 403, 403);
  84. }
  85. $new = Project::firstOrNew(['uid' => $request->get('id')]);
  86. if (Str::isUuid($request->get('id'))) {
  87. $new->uid = $request->get('id');
  88. } else {
  89. $new->uid = Str::uuid();
  90. }
  91. $new->title = $request->get('title');
  92. $new->description = $request->get('description');
  93. $new->parent_id = $request->get('parent_id');
  94. $new->editor_id = $user['user_uid'];
  95. $new->owner_id = $studioId;
  96. $new->type = $request->get('type', 'instance');
  97. if (Str::isUuid($request->get('parent_id'))) {
  98. $parentPath = Project::where('uid', $request->get('parent_id'))->value('path');
  99. $parentPath = json_decode($parentPath);
  100. if (!is_array($parentPath)) {
  101. $parentPath = array();
  102. }
  103. array_push($parentPath, $new->parent_id);
  104. $new->path = json_encode($parentPath, JSON_UNESCAPED_UNICODE);
  105. }
  106. $new->save();
  107. return $this->ok(new ProjectResource($new));
  108. }
  109. /**
  110. * Display the specified resource.
  111. *
  112. * @param \App\Models\Project $project
  113. * @return \Illuminate\Http\Response
  114. */
  115. public function show(Project $project)
  116. {
  117. //
  118. return $this->ok(new ProjectResource($project));
  119. }
  120. /**
  121. * Update the specified resource in storage.
  122. *
  123. * @param \Illuminate\Http\Request $request
  124. * @param \App\Models\Project $project
  125. * @return \Illuminate\Http\Response
  126. */
  127. public function update(Request $request, Project $project)
  128. {
  129. //
  130. $user = AuthApi::current($request);
  131. if (!$user) {
  132. return $this->error(__('auth.failed'), 401, 401);
  133. }
  134. if (!self::canEdit($user['user_uid'], $project->owner_id)) {
  135. return $this->error(__('auth.failed'), 403, 403);
  136. }
  137. $project->title = $request->get('title');
  138. $project->description = $request->get('description');
  139. $project->parent_id = $request->get('parent_id');
  140. $project->editor_id = $user['user_uid'];
  141. if (Str::isUuid($request->get('parent_id'))) {
  142. $parentPath = Project::where('uid', $request->get('parent_id'))->value('path');
  143. $parentPath = json_decode($parentPath);
  144. if (!is_array($parentPath)) {
  145. $parentPath = array();
  146. }
  147. array_push($parentPath, $project->parent_id);
  148. $project->path = json_encode($parentPath, JSON_UNESCAPED_UNICODE);
  149. }
  150. $project->save();
  151. return $this->ok(new ProjectResource($project));
  152. }
  153. /**
  154. * Remove the specified resource from storage.
  155. *
  156. * @param \App\Models\Project $project
  157. * @return \Illuminate\Http\Response
  158. */
  159. public function destroy(Project $project)
  160. {
  161. //
  162. }
  163. }