ProjectController.php 6.3 KB

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