Kaynağa Gözat

created_by,updated_by

peterguo 6 gün önce
ebeveyn
işleme
8404dfc261

+ 4 - 21
app/Http/Controllers/API/FolderController.php

@@ -8,6 +8,7 @@ use App\Http\Requests\API\Folder\UpdateRequest;
 use App\Http\Resources\API\FileByObjectResource;
 use App\Http\Resources\API\FileVersioTreeByObjectResource;
 use App\Http\Resources\API\FolderDetailResource;
+use App\Http\Resources\API\UserProfileResource;
 use App\Models\Enums\FolderObjectType;
 use App\Models\Enums\ObjectApprovalStatus;
 use App\Models\File;
@@ -21,6 +22,7 @@ use Illuminate\Database\Eloquent\Collection;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\DB;
+use function Symfony\Component\String\s;
 
 class FolderController extends Controller
 {
@@ -276,7 +278,6 @@ class FolderController extends Controller
         $folders = Folder::with(["namingRule" => function ($query) {
             $query->select(['id', 'name', 'combination_rules']);
         }])
-            ->select(['id', 'name', 'naming_rule_id', 'created_by', 'updated_by', 'created_at', 'updated_at'])
             ->where($objectWhere)
             ->when($name, fn($query) => $query->where("name", "like", "%$name%"))
             ->when($folderId, fn($query) => $query->where("parent_id", $folderId))
@@ -285,27 +286,9 @@ class FolderController extends Controller
             ->orderBy(in_array($orderByFiled, Folder::getColumns()) ? $orderByFiled : 'updated_at', $orderByType)
             ->paginate($pageSize);
         $folders_total = $folders->total();
-        $sonFolderCount=Folder::query()
-            ->where($objectWhere)
-            ->whereIn('parent_id',$folders->pluck('id'))
-            ->selectRaw("count(*) as cut, parent_id")
-            ->groupBy("parent_id")
-            ->pluck("cut", "parent_id");
-
-        $sonFileCount=File::query()
-            ->where($objectWhere)
-            ->whereIn('folder_id',$folders->pluck('id'))
-            ->where("is_latest_version", 1)
-            ->selectRaw("count(*) as cut, folder_id")
-            ->groupBy("folder_id")
-            ->pluck("cut", "folder_id");
-
 
         $index=1;
-        $folders = $folders->map(function (Folder $folder) use ($sonFolderCount,$sonFileCount,&$index) {
-            $folder->itemCount =$sonFolderCount->get($folder->id, 0)+$sonFileCount->get($folder->id, 0);
-            $folder->type = 'folder';
-            $folder->uniId = $folder->type . '_' . $folder->id;
+        $folders = $folders->map(function (Folder $folder) use (&$index) {
             $folder->display_id=(string)$index++;
             return $folder;
         });
@@ -366,7 +349,7 @@ class FolderController extends Controller
         return $this->success([
             'object'=>$container,
             'data' => [
-                'folders' => $folders,
+                'folders' => FolderDetailResource::collection($folders),
                 'total' => $folders_total + $files_total,
                 'files' => FileByObjectResource::collection($files),
                 'folder_parent_id'=>$folderId>0?$folder->parent_id:$folderId,

+ 2 - 0
app/Http/Resources/API/FileByObjectResource.php

@@ -25,7 +25,9 @@ class FileByObjectResource extends JsonResource
             'download_url' => Storage::url($this->pathname),
             'size' => $this->size,
             'created_by' => $this->createdBy ? new UserProfileResource($this->createdBy) : null,
+            'updated_by' => $this->updatedBy ? new UserProfileResource($this->updatedBy) : null,
             'created_at' => (string) $this->created_at,
+            'updated_at' => (string) $this->updated_at,
             'version' => $this->version,
             'display_id'=>(string)$this->display_id,
             'object_type'=>$this->object_type,

+ 9 - 0
app/Http/Resources/API/FolderDetailResource.php

@@ -20,6 +20,15 @@ class FolderDetailResource extends JsonResource
             'name' => $this->name,
             'parent_id' => $this->parent_id,
             'sequence' => $this->sequence,
+            'created_at' => (string)$this->created_at,
+            'updated_at' => (string)$this->updated_at,
+            'created_by' => $this->createdBy ? new UserProfileResource($this->createdBy) : null,
+            'updated_by' => $this->updatedBy ? new UserProfileResource($this->updatedBy) : null,
+            'type' => 'folder',
+            'itemCount' => $this->children()->count() + $this->files()->where('is_latest_version', 1)->count(),
+            'uniId' => 'folder_' . $this->id,
+            'display_id'=>(string)$this->display_id,
+            'naming_rule' => new NamingRuleSimpleResource($this->namingRule),
         ];
     }
 }

+ 5 - 0
app/Models/File.php

@@ -64,6 +64,11 @@ class File extends Model
         return $this->belongsTo(User::class, "created_by");
     }
 
+    public function updatedBy(): BelongsTo
+    {
+        return $this->belongsTo(User::class, "updated_by");
+    }
+
     public function folder(): BelongsTo
     {
         return $this->belongsTo(Folder::class);

+ 10 - 0
app/Models/Folder.php

@@ -57,4 +57,14 @@ class Folder extends Model
     {
         return $this->hasOneThrough(Library::class, Container::class, 'id', 'id', 'object_id', 'library_id');
     }
+
+    public function createdBy(): BelongsTo
+    {
+        return $this->belongsTo(User::class, "created_by");
+    }
+
+    public function updatedBy(): BelongsTo
+    {
+        return $this->belongsTo(User::class, "updated_by");
+    }
 }