ProjectController.php 6.0 KB

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