Browse Source

$project->id 改为 $project->uid

visuddhinanda 1 year ago
parent
commit
5356aa23a0

+ 22 - 18
api-v8/app/Http/Api/ProjectApi.php

@@ -1,42 +1,46 @@
 <?php
+
 namespace App\Http\Api;
 
 use App\Models\Project;
 use Illuminate\Support\Facades\Log;
 use Illuminate\Support\Facades\App;
 
-class ProjectApi{
-    public static function getById($id){
-        if(!$id){
+class ProjectApi
+{
+    public static function getById($id)
+    {
+        if (!$id) {
             return null;
         };
-        $project = Project::where('id',$id)->first();
-        if($project){
+        $project = Project::find($id);
+        if ($project) {
             return [
-                'id'=>$id,
-                'title'=>$project->title,
-                'type'=>$project->type,
-                'description'=>$project->description,
+                'id' => $id,
+                'title' => $project->title,
+                'type' => $project->type,
+                'description' => $project->description,
             ];
-        }else{
+        } else {
             return null;
         }
     }
 
-    public static function getListByIds($ids){
-        if(!$ids){
+    public static function getListByIds($ids)
+    {
+        if (!$ids) {
             return null;
         };
-        $projects = Project::whereIn('id',$ids)->get();
+        $projects = Project::whereIn('uid', $ids)->get();
         $output = array();
         foreach ($ids as $key => $id) {
             foreach ($projects as $project) {
-                if($project->id === $id){
+                if ($project->uid === $id) {
                     $output[] = [
-                        'id'=>$id,
-                        'title'=>$project->title,
-                        'type'=>$project->type,
-                        'description'=>$project->description,
+                        'id' => $id,
+                        'title' => $project->title,
+                        'type' => $project->type,
+                        'description' => $project->description,
                     ];
                     continue;
                 };

+ 6 - 6
api-v8/app/Http/Controllers/ProjectController.php

@@ -31,7 +31,7 @@ class ProjectController extends Controller
                     ->where('type', $request->get('type', 'instance'));
                 break;
             case 'project-tree':
-                $table = Project::where('id', $request->get('project_id'))
+                $table = Project::where('uid', $request->get('project_id'))
                     ->orWhereJsonContains('path', $request->get('project_id'));
                 break;
             default:
@@ -50,7 +50,7 @@ class ProjectController extends Controller
         $sql = $table->toSql();
         Log::debug('sql', ['sql' => $sql]);
 
-        $table = $table->orderBy($request->get('order', 'created_at'), $request->get('dir', 'desc'));
+        $table = $table->orderBy($request->get('order', 'id'), $request->get('dir', 'asc'));
 
         $table = $table->skip($request->get("offset", 0))
             ->take($request->get('limit', 10000));
@@ -87,11 +87,11 @@ class ProjectController extends Controller
         if (!self::canEdit($user['user_uid'], $studioId)) {
             return $this->error(__('auth.failed'), 403, 403);
         }
-        $new = Project::firstOrNew(['id' => $request->get('id')]);
+        $new = Project::firstOrNew(['uid' => $request->get('id')]);
         if (Str::isUuid($request->get('id'))) {
-            $new->id = $request->get('id');
+            $new->uid = $request->get('id');
         } else {
-            $new->id =  Str::uuid();
+            $new->uid =  Str::uuid();
         }
         $new->title = $request->get('title');
         $new->description = $request->get('description');
@@ -101,7 +101,7 @@ class ProjectController extends Controller
         $new->type = $request->get('type', 'instance');
 
         if (Str::isUuid($request->get('parent_id'))) {
-            $parentPath = Project::where('id', $request->get('parent_id'))->value('path');
+            $parentPath = Project::where('uid', $request->get('parent_id'))->value('path');
             $parentPath = json_decode($parentPath);
             if (!is_array($parentPath)) {
                 $parentPath = array();

+ 4 - 4
api-v8/app/Http/Controllers/ProjectTreeController.php

@@ -42,7 +42,7 @@ class ProjectTreeController extends Controller
         $newData = [];
         foreach ($request->get('data') as $key => $value) {
             $newData[] = [
-                'id' => Str::uuid(),
+                'uid' => Str::uuid(),
                 'old_id' => $value['id'],
                 'title' => $value['title'],
                 'type' => $value['type'],
@@ -68,7 +68,7 @@ class ProjectTreeController extends Controller
                     $newData[$key]['parent_id'] = null;
                 }
             } else if (!empty($request->get('parent_id'))) {
-                $pPath = Project::where('id', $request->get('parent_id'))->value('path');
+                $pPath = Project::where('uid', $request->get('parent_id'))->value('path');
                 $parentPath = json_decode($pPath);
                 if (!is_array($parentPath)) {
                     $parentPath = [];
@@ -80,10 +80,10 @@ class ProjectTreeController extends Controller
         $output = [];
         foreach ($newData as $key => $value) {
             $children = array_filter($newData, function ($element) use ($value) {
-                return $element['parent_id'] === $value['id'];
+                return $element['parent_id'] === $value['uid'];
             });
             $output[] = [
-                'id' => $value['id'],
+                'id' => $value['uid'],
                 'resId' => $value['res_id'],
                 'isLeaf' => count($children) === 0,
             ];

+ 3 - 3
api-v8/app/Http/Resources/ProjectResource.php

@@ -18,7 +18,7 @@ class ProjectResource extends JsonResource
     public function toArray($request)
     {
         $data = [
-            'id' => $this->id,
+            'id' => $this->uid,
             'title' => $this->title,
             'type' => $this->type,
             'description' => $this->description,
@@ -27,8 +27,8 @@ class ProjectResource extends JsonResource
             'parent' => ProjectApi::getById($this->parent_id),
             'path' => ProjectApi::getListByIds(json_decode($this->path)),
             'description' => $this->description,
-            "owner"=> StudioApi::getById($this->owner_id),
-            "editor"=> UserApi::getIdByUuid($this->editor_id),
+            "owner" => StudioApi::getById($this->owner_id),
+            "editor" => UserApi::getIdByUuid($this->editor_id),
             'created_at' => $this->created_at,
             'updated_at' => $this->updated_at,
         ];

+ 3 - 2
api-v8/app/Models/Project.php

@@ -8,9 +8,10 @@ use Illuminate\Database\Eloquent\Model;
 class Project extends Model
 {
     use HasFactory;
-    protected $primaryKey = 'id';
+    protected $primaryKey = 'uid';
     protected $casts = [
-        'id' => 'string'
+        'uid' => 'string'
     ];
+
     protected $fillable = ['id'];
 }