Browse Source

Merge branch 'project-list' of kyle/autocde2.0 into dev

Mo 1 year ago
parent
commit
fd9f830cb0

+ 5 - 2
app/Http/Controllers/API/ProjectController.php

@@ -4,6 +4,7 @@ namespace App\Http\Controllers\API;
 
 use App\Http\Controllers\Controller;
 use App\Http\Requests\API\Project\CreateOrUpdateRequest;
+use App\Http\Resources\API\ProjectResource;
 use App\Models\Project;
 use App\Models\ProjectAsset;
 use App\Models\ProjectPlan;
@@ -16,9 +17,11 @@ class ProjectController extends Controller
     /**
      * Display a listing of the resource.
      */
-    public function index()
+    public function index(Request $request)
     {
-        //
+        $projects = Project::filter($request->all())->get();
+
+        return ProjectResource::collection($projects);
     }
 
     /**

+ 28 - 0
app/Http/Resources/API/ProjectResource.php

@@ -0,0 +1,28 @@
+<?php
+
+namespace App\Http\Resources\API;
+
+use Illuminate\Http\Request;
+use Illuminate\Http\Resources\Json\JsonResource;
+
+class ProjectResource 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,
+            'code' => $this->code,
+            'status' => $this->status,
+            'const' => $this->const,
+            'begin' => $this->begin,
+            'end' => $this->end,
+            'available_days' => $this->available_days,
+        ];
+    }
+}

+ 16 - 0
app/ModelFilters/ProjectFilter.php

@@ -0,0 +1,16 @@
+<?php 
+
+namespace App\ModelFilters;
+
+use EloquentFilter\ModelFilter;
+
+class ProjectFilter extends ModelFilter
+{
+    /**
+    * Related Models that have ModelFilters as well as the method on the ModelFilter
+    * As [relationMethod => [input_key1, input_key2]].
+    *
+    * @var array
+    */
+    public $relations = [];
+}

+ 8 - 1
app/Models/Project.php

@@ -2,14 +2,21 @@
 
 namespace App\Models;
 
+use App\Models\Scopes\CompanyScope;
+use EloquentFilter\Filterable;
 use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Model;
 
 class Project extends Model
 {
-    use HasFactory;
+    use HasFactory, Filterable;
 
     protected $fillable = [
         "name","code","const","available_days","status","begin","end","latitude","longitude","type","acl","whitelist","description"
     ];
+
+    protected static function booted(): void
+    {
+        static::addGlobalScope(new CompanyScope);
+    }
 }

+ 3 - 0
database/factories/ProjectFactory.php

@@ -4,6 +4,7 @@ namespace Database\Factories;
 
 use Carbon\Carbon;
 use Illuminate\Database\Eloquent\Factories\Factory;
+use Illuminate\Support\Facades\Auth;
 
 /**
  * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Project>
@@ -31,6 +32,8 @@ class ProjectFactory extends Factory
             'acl' => 'private',
             'whitelist' => ',1,',
             'description' => fake()->text(),
+            'company_id' => Auth::user()->company_id,
+            'created_by' => Auth::id(),
         ];
     }
 }

+ 2 - 2
database/migrations/2024_01_21_031515_create_projects_table.php

@@ -19,8 +19,8 @@ return new class extends Migration
             $table->decimal('const', 10, 2)->nullable();
             $table->integer('available_days')->default(0);
             $table->string("status", 20)->default("undone")->comment("undone, pending_review, suspended, closed");
-            $table->timestamp("begin")->nullable();
-            $table->timestamp("end")->nullable();
+            $table->date("begin")->nullable();
+            $table->date("end")->nullable();
             $table->decimal('latitude', 10, 6)->nullable();
             $table->decimal('longitude', 10, 6)->nullable();
             $table->string("type", 20)->nullable();

+ 24 - 0
tests/Feature/API/ProjectTest.php

@@ -11,6 +11,30 @@ use Tests\Feature\TestCase;
 
 class ProjectTest extends TestCase
 {
+    public function test_project_list()
+    {
+        Project::factory(30)->create();
+
+        $response = $this->get(route('project.index'));
+
+        $response->assertStatus(200)
+            ->assertJsonStructure([
+                'data' => [
+                    '*' => [
+                        'id',
+                        'name',
+                        'code',
+                        'status',
+                        'const',
+                        'begin',
+                        'end',
+                        'available_days',
+                    ]
+                ]
+            ]);
+    }
+
+
     public function test_project_create(): void
     {
         $form = Project::factory()->make();