2
0

ProjectController.php 5.9 KB

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