Browse Source

task detail & edit rome_avatar_add_gender_address_to_users

moell 1 year ago
parent
commit
185edcea83

+ 4 - 1
app/Http/Controllers/API/TaskController.php

@@ -4,6 +4,7 @@ namespace App\Http\Controllers\API;
 
 use App\Http\Controllers\Controller;
 use App\Http\Requests\API\Task\CreateOrUpdateRequest;
+use App\Http\Resources\API\TaskDetailResource;
 use App\Models\Task;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Auth;
@@ -38,7 +39,9 @@ class TaskController extends Controller
      */
     public function show(string $id)
     {
-        //
+        $task = Task::query()->findOrFail($id);
+
+        return new TaskDetailResource($task);
     }
 
     /**

+ 22 - 0
app/Http/Resources/API/NamingRuleSimpleResource.php

@@ -0,0 +1,22 @@
+<?php
+
+namespace App\Http\Resources\API;
+
+use Illuminate\Http\Request;
+use Illuminate\Http\Resources\Json\JsonResource;
+
+class NamingRuleSimpleResource extends JsonResource
+{
+    /**
+     * Transform the resource into an array.
+     *
+     * @return array<string, mixed>
+     */
+    public function toArray(Request $request): array
+    {
+        return [
+            'id' => $this->id,
+            'name' => $this->name,
+        ];
+    }
+}

+ 22 - 0
app/Http/Resources/API/ProjectSimpleResource.php

@@ -0,0 +1,22 @@
+<?php
+
+namespace App\Http\Resources\API;
+
+use Illuminate\Http\Request;
+use Illuminate\Http\Resources\Json\JsonResource;
+
+class ProjectSimpleResource extends JsonResource
+{
+    /**
+     * Transform the resource into an array.
+     *
+     * @return array<string, mixed>
+     */
+    public function toArray(Request $request): array
+    {
+        return [
+            'id' => $this->id,
+            'name' => $this->name,
+        ];
+    }
+}

+ 22 - 0
app/Http/Resources/API/RequirementSimpleResource.php

@@ -0,0 +1,22 @@
+<?php
+
+namespace App\Http\Resources\API;
+
+use Illuminate\Http\Request;
+use Illuminate\Http\Resources\Json\JsonResource;
+
+class RequirementSimpleResource extends JsonResource
+{
+    /**
+     * Transform the resource into an array.
+     *
+     * @return array<string, mixed>
+     */
+    public function toArray(Request $request): array
+    {
+        return [
+            'id' => $this->id,
+            'title' => $this->title,
+        ];
+    }
+}

+ 37 - 1
app/Http/Resources/API/TaskDetailResource.php

@@ -14,6 +14,42 @@ class TaskDetailResource extends JsonResource
      */
     public function toArray(Request $request): array
     {
-        return parent::toArray($request);
+        return [
+            "id" => $this->id,
+            "name" => $this->name,
+            "project_id" => $this->project_id,
+            "project" => new ProjectSimpleResource($this->project),
+            "requirement_id" => $this->requirement_id,
+            "requirement" => new RequirementSimpleResource($this->requirement),
+            "naming_rule_id" => $this->naming_rule_id,
+            "naming_rule" => new NamingRuleSimpleResource($this->namingRule),
+            "parent_id" => $this->parent_id,
+            "task_type" => $this->task_type,
+            "doc_stage" => $this->doc_stage,
+            "doc_type" => $this->doc_type,
+            "status" => $this->status,
+            "assign" => $this->assign,
+            "description" => $this->description,
+            "begin" => $this->begin,
+            "end" => $this->end,
+            "mailto"  => $this->mailto,
+            "email_subject"  => $this->email_subject,
+            "acl"  => $this->acl,
+            "whitelist"  => $this->whitelist,
+            "closed_by" => new UserProfileResource($this->closedBy),
+            "closed_at" => $this->closed_at,
+            "canceled_by" => new UserProfileResource($this->canceledBy),
+            "canceled_at" => $this->canceled_at,
+            "approve_by" => new UserProfileResource($this->approveBy),
+            "approve_at" => $this->approve_at,
+            "finished_by" =>  new UserProfileResource($this->finishedBy),
+            "finished_at" => $this->finished_at,
+            "review_by" => new UserProfileResource($this->reviewBy),
+            "review_at" => $this->review_at,
+            "created_by" => new UserProfileResource($this->createdBy),
+            "custom_fields" => $this->custom_fields,
+            "created_at" => (string)$this->created_at,
+            "updated_at" => (string)$this->updated_at
+        ];
     }
 }

+ 52 - 0
app/Models/Task.php

@@ -2,6 +2,7 @@
 
 namespace App\Models;
 
+use App\Models\Scopes\CompanyScope;
 use EloquentFilter\Filterable;
 use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Model;
@@ -18,4 +19,55 @@ class Task extends Model
         'mailto' => 'array',
         'custom_fields' => 'array',
     ];
+
+    protected static function booted(): void
+    {
+        static::addGlobalScope(new CompanyScope);
+    }
+
+    public function requirement(): \Illuminate\Database\Eloquent\Relations\BelongsTo
+    {
+        return $this->belongsTo(Requirement::class);
+    }
+
+    public function project(): \Illuminate\Database\Eloquent\Relations\BelongsTo
+    {
+        return $this->belongsTo(Project::class);
+    }
+
+    public function namingRule(): \Illuminate\Database\Eloquent\Relations\BelongsTo
+    {
+        return $this->belongsTo(NamingRule::class);
+    }
+
+    public function createdBy(): \Illuminate\Database\Eloquent\Relations\BelongsTo
+    {
+        return $this->belongsTo(User::class, 'created_by');
+    }
+
+    public function reviewBy(): \Illuminate\Database\Eloquent\Relations\BelongsTo
+    {
+        return $this->belongsTo(User::class, 'review_by');
+    }
+
+    public function finishedBy(): \Illuminate\Database\Eloquent\Relations\BelongsTo
+    {
+        return $this->belongsTo(User::class, 'finished_by');
+    }
+
+    public function approveBy(): \Illuminate\Database\Eloquent\Relations\BelongsTo
+    {
+        return $this->belongsTo(User::class, 'approve_by');
+    }
+
+    public function canceledBy(): \Illuminate\Database\Eloquent\Relations\BelongsTo
+    {
+        return $this->belongsTo(User::class, 'canceled_by');
+    }
+
+    public function closedBy(): \Illuminate\Database\Eloquent\Relations\BelongsTo
+    {
+        return $this->belongsTo(User::class, 'closed_by');
+    }
+
 }

+ 1 - 0
database/factories/TaskFactory.php

@@ -37,6 +37,7 @@ class TaskFactory extends Factory
             'acl' => TaskACL::PRIVATE->value,
             'whitelist' => ',1,',
             'created_by' => Auth::id(),
+            'company_id' => Auth::user()->company_id,
             'mailto' => [
                 Auth::id(),
             ],

+ 1 - 1
database/migrations/2024_02_18_061942_rome_avatar_add_gender_address_to_users.php

@@ -16,7 +16,6 @@ return new class extends Migration
             $table->integer('gender')->nullable()->comment('男0,女1');
             $table->string('address',250)->nullable();
             $table->integer('department_id')->nullable()->comment('部门id');
-
         });
     }
 
@@ -26,6 +25,7 @@ return new class extends Migration
     public function down(): void
     {
         Schema::table('users', function (Blueprint $table) {
+            $table->string('avatar', 250)->nullable();
             $table->dropColumn(['gender', 'address', 'department_id']);
         });
     }

+ 1 - 0
database/migrations/2024_02_23_122917_create_tasks_table.php

@@ -15,6 +15,7 @@ return new class extends Migration
             $table->id();
             $table->string("name", 150);
             $table->integer("project_id");
+            $table->integer("company_id");
             $table->integer("requirement_id")->nullable();
             $table->integer("naming_rule_id")->nullable();
             $table->integer("parent_id");

+ 92 - 0
tests/Feature/API/TaskTest.php

@@ -22,4 +22,96 @@ class TaskTest extends TestCase
 
         $response->assertStatus(201);
     }
+
+    protected function test_task_list()
+    {
+        Task::factory(30)->create();
+
+        $response = $this->get(route('task.index'));
+
+        $response->assertStatus(200)
+            ->assertJsonStructure([
+                'data' => [
+                    '*' => [
+                        'id',
+                        'name',
+                        'global',
+                        'status',
+                        'company'
+                    ]
+                ]
+            ]);
+    }
+
+    public function test_task_show(): void
+    {
+        $task = Task::factory()->create();
+
+        $response = $this->get(route('task.show', ['task' => $task->id]));
+
+        $response->assertStatus(200)
+            ->assertJsonStructure([
+                'data' => [
+                    "id",
+                    "name",
+                    "project_id",
+                    "project",
+                    "requirement_id",
+                    "naming_rule_id",
+                    "parent_id",
+                    "task_type",
+                    "doc_stage",
+                    "doc_type",
+                    "status",
+                    "assign",
+                    "description",
+                    "begin",
+                    "end",
+                    "mailto",
+                    "email_subject",
+                    "acl",
+                    "whitelist",
+                    "closed_by",
+                    "closed_at",
+                    "canceled_by",
+                    "canceled_at",
+                    "approve_by",
+                    "approve_at",
+                    "finished_by",
+                    "finished_at",
+                    "review_by",
+                    "review_at",
+                    "created_by",
+                    "custom_fields",
+                    "created_at",
+                    "updated_at"
+                ]
+            ]);
+    }
+
+    protected function test_task_update(): void
+    {
+        $task = Task::factory()->create();
+
+        $form = Task::factory()->make();
+
+        $response = $this->put(route('task.update', ['task' => $task->id]), $form->toArray());
+
+        $response->assertStatus(204);
+
+        $newAsset = Task::find($task->id);
+
+        $this->assertEquals($form->name, $newAsset->name);
+    }
+
+    protected function test_task_delete(): void
+    {
+        $task = Task::factory()->create();
+
+        $response = $this->delete(route('task.destroy', ['task' => $task->id]));
+
+        $response->assertStatus(204);
+
+        $this->assertNull(Task::find($task->id));
+    }
 }