Browse Source

容器编辑支持版本控制

moell 10 tháng trước cách đây
mục cha
commit
b3080f5719

+ 31 - 2
app/Http/Controllers/API/ContainerController.php

@@ -67,7 +67,8 @@ class ContainerController extends Controller
         $files = File::query()->where('object_id', $container->id)
             ->where('object_type', ActionObjectType::CONTAINER)
             ->where('source', 1)
-            ->pluck("id");
+            ->pluck("id")
+            ->sort();
 
         $contentFormData = [
             'description' => $imageUrlService->interceptImageUrl($request->description),
@@ -100,7 +101,6 @@ class ContainerController extends Controller
     public function update(
         CreateOrUpdateRequest $request,
         ImageUrlService $imageUrlService,
-        FileAssociationService $service,
         CustomFieldRepository $customFieldRepo,
         string $id
     )
@@ -120,6 +120,35 @@ class ContainerController extends Controller
 
         $container->fill($formData);
         $changes = ModelChangeDetector::detector(ActionObjectType::CONTAINER, $container);
+
+        $files = File::query()->where('object_id', $container->id)
+            ->where('object_type', ActionObjectType::CONTAINER)
+            ->where('source', 1)
+            ->pluck("id")
+            ->sort();
+
+        $contentFormData = [
+            'description' => $imageUrlService->interceptImageUrl($request->description),
+            'name' => $request->name,
+            'files' => $files->implode(",") ?: null
+        ];
+
+        $containerContent = $container->content;
+        $containerContent->fill($contentFormData);
+        $contentChange = ModelChangeDetector::detector(ActionObjectType::CONTAINER_CONTENT, $containerContent);
+
+        if ($contentChange) {
+            $container->version++;
+            $changes = array_merge($changes, $contentChange);
+
+            ContainerContent::query()->create([
+                ...$contentFormData,
+                'container_id' => $container->id,
+                'created_by' => Auth::id(),
+                'version' => $container->version,
+            ]);
+        }
+
         $container->save();
 
         ActionRepository::createByContainer($container, ObjectAction::EDITED, objectChanges: $changes);

+ 2 - 1
app/Models/Container.php

@@ -6,10 +6,11 @@ use App\Models\Scopes\CompanyScope;
 use Illuminate\Database\Eloquent\Builder;
 use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\SoftDeletes;
 
 class Container extends Model
 {
-    use HasFactory;
+    use HasFactory, SoftDeletes;
 
     protected $fillable = [
         'name', 'library_id', 'naming_rule_id', 'naming_rules', 'mailto', 'email_subject', 'acl', 'whitelist',

+ 4 - 0
app/Models/Enums/ActionObjectType.php

@@ -8,6 +8,7 @@ use App\Models\Plan;
 use App\Models\Project;
 use App\Models\Requirement;
 use App\Models\Task;
+use App\Services\History\Detector\ContainerContentDetector;
 use App\Services\History\Detector\ContainerDetector;
 use App\Services\History\Detector\ProjectDetector;
 use App\Services\History\Detector\RequirementDetector;
@@ -27,6 +28,8 @@ enum ActionObjectType: string
 
     case CONTAINER = "container";
 
+    case CONTAINER_CONTENT = "container_content";
+
     public function modelBuilder(): \Illuminate\Database\Eloquent\Builder
     {
         return match ($this) {
@@ -67,6 +70,7 @@ enum ActionObjectType: string
             ActionObjectType::REQUIREMENT => RequirementDetector::class,
             ActionObjectType::TASK => TaskDetector::class,
             ActionObjectType::CONTAINER => ContainerDetector::class,
+            ActionObjectType::CONTAINER_CONTENT => ContainerContentDetector::class,
             default => null
         };
     }

+ 30 - 0
app/Services/History/Detector/ContainerContentDetector.php

@@ -0,0 +1,30 @@
+<?php
+
+namespace App\Services\History\Detector;
+
+
+class ContainerContentDetector extends DetectorAbstract
+{
+    public static function fields(): array
+    {
+        return [
+            'name',
+            'description',
+            'files',
+        ];
+    }
+
+    public static function diffFields(): array
+    {
+        return [
+            'description'
+        ];
+    }
+
+    public static function converters(): array
+    {
+        return [
+
+        ];
+    }
+}

+ 0 - 1
app/Services/History/Detector/ContainerDetector.php

@@ -15,7 +15,6 @@ class ContainerDetector extends DetectorAbstract
     public static function fields(): array
     {
         return [
-            'name',
             'library_id',
             'naming_rule_id',
             'task_type',