ProjectController.php 6.4 KB

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