Przeglądaj źródła

apply namingRule

peterguo 1 miesiąc temu
rodzic
commit
ea5808dfc8

+ 3 - 2
app/Http/Controllers/API/FolderController.php

@@ -360,6 +360,7 @@ class FolderController extends Controller
                 'total' => $folders_total + $files_total,
                 'files' => FileByObjectResource::collection($files),
                 'folder_parent_id'=>$folderId>0?$folder->parent_id:$folderId,
+                'folder_parent_name'=>$folderId>0?$folder->parent?->name:''
             ]
         ]);
     }
@@ -438,9 +439,9 @@ class FolderController extends Controller
 
     }
 
-    public function applyNaming(string $folderId, string $namingId, FoldersService $foldersService): void
+    public function applyNaming(string $folderId, string $namingRuleId, FoldersService $foldersService): void
     {
-        $foldersService->update($folderId, ['naming_rule_id' => $namingId]);
+        $foldersService->applyNaming($folderId, $namingRuleId);
         $this->noContent();
     }
 }

+ 12 - 0
app/Models/Folder.php

@@ -6,6 +6,7 @@ use App\Models\Scopes\CompanyScope;
 use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Database\Eloquent\Relations\BelongsTo;
+use Illuminate\Database\Eloquent\Relations\HasMany;
 use Illuminate\Database\Eloquent\SoftDeletes;
 
 class Folder extends Model
@@ -13,6 +14,7 @@ class Folder extends Model
     use HasFactory, SoftDeletes;
 
     protected $guarded = ['id'];
+    private mixed $children;
 
     protected static function booted(): void
     {
@@ -28,4 +30,14 @@ class Folder extends Model
     {
         return $this->belongsTo(NamingRule::class);
     }
+
+    public function parent(): BelongsTo
+    {
+        return $this->belongsTo(Folder::class, 'parent_id');
+    }
+
+    public function children(): HasMany
+    {
+        return $this->hasMany(Folder::class, 'parent_id');
+    }
 }

+ 7 - 3
app/Services/Folder/FoldersService.php

@@ -3,13 +3,17 @@
 namespace App\Services\Folder;
 
 use App\Models\Folder;
+use App\Models\NamingRule;
 
 class FoldersService
 {
-    public function update(string $id, array $updatedData): void
+    public function applyNaming(string $id, string $namingRuleId): void
     {
         $folder = Folder::query()->findOrFail($id);
-        $folder->fill($updatedData);
-        $folder->save();
+        $namingRule = NamingRule::query()->findOrFail($namingRuleId);
+        // 子孙文件夹均需更新
+        Folder::query()
+            ->where('path', 'like', $folder->path . '%')
+            ->update(['naming_rule_id' => $namingRule->id]);
     }
 }