Browse Source

library linkage

moell 10 months ago
parent
commit
82b1cf0622

+ 27 - 3
app/Http/Controllers/API/LibraryController.php

@@ -5,6 +5,7 @@ namespace App\Http\Controllers\API;
 use App\Http\Controllers\Controller;
 use App\Http\Requests\API\Library\CreateOrUpdateRequest;
 use App\Http\Resources\API\LibraryResource;
+use App\Models\Enums\LibraryType;
 use App\Models\Library;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Auth;
@@ -28,7 +29,7 @@ class LibraryController extends Controller
             ...$request->all(),
             'created_by' => Auth::id(),
             'company_id' => Auth::user()->company_id,
-            'whitelist' => $request->whitelist ? sprintf(",%s", implode(',', $request->whitelist)) : null,
+            'whitelist' => $request->whitelist ? sprintf(",%s,", implode(',', $request->whitelist)) : null,
         ]);
 
         return $this->created();
@@ -39,7 +40,7 @@ class LibraryController extends Controller
      */
     public function show(string $id)
     {
-        $library = Library::query()->findOrFail($id);
+        $library = Library::allowed()->findOrFail($id);
 
         return new LibraryResource($library);
     }
@@ -49,7 +50,7 @@ class LibraryController extends Controller
      */
     public function update(Request $request, string $id)
     {
-        $library = Library::query()->findOrFail($id);
+        $library = Library::query()->allowed()->findOrFail($id);
 
         $library->fill($request->only([
             'name', 'acl', 'asset_id', 'project_id',
@@ -68,4 +69,27 @@ class LibraryController extends Controller
     {
         //
     }
+
+    public function linkage(Request $request, string $type)
+    {
+        $libraryType = LibraryType::from($type);
+        if (in_array($libraryType->value, [LibraryType::PROJECT->value, LibraryType::ASSET->value]) && !$request->get("id")) {
+            return $this->badRequest("Parameter ID cannot be empty");
+        }
+
+        $where = match ($libraryType) {
+            LibraryType::ASSET => ['asset_id' => $request->get("id")],
+            LibraryType::PROJECT => ['project_id' => $request->get("id")],
+            LibraryType::CUSTOM => []
+        };
+
+        $libraries = Library::query()
+            ->allowed()
+            ->where("type", $type)->when($where, fn($query) => $query->where($where))
+            ->get(['id', 'name']);
+
+        return $this->success([
+            'data' => $libraries
+        ]);
+    }
 }

+ 16 - 0
app/ModelFilters/LibraryFilter.php

@@ -0,0 +1,16 @@
+<?php 
+
+namespace App\ModelFilters;
+
+use EloquentFilter\ModelFilter;
+
+class LibraryFilter 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 = [];
+}

+ 11 - 1
app/Models/Library.php

@@ -3,12 +3,17 @@
 namespace App\Models;
 
 use App\Models\Scopes\CompanyScope;
+use EloquentFilter\Filterable;
+use Illuminate\Database\Eloquent\Builder;
 use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Model;
 
+/**
+ * @method static \Illuminate\Database\Eloquent\Builder allowed()
+ */
 class Library extends Model
 {
-    use HasFactory;
+    use HasFactory, Filterable;
 
     protected $guarded = [
         'id'
@@ -18,4 +23,9 @@ class Library extends Model
     {
         static::addGlobalScope(new CompanyScope);
     }
+
+    public function scopeAllowed(Builder $query): void
+    {
+
+    }
 }

+ 2 - 0
routes/api.php

@@ -64,6 +64,8 @@ Route::middleware(['auth:sanctum'])->group(function () {
             'index', 'show'
         ]);
 
+        Route::get("library-linkage/{type}", [API\LibraryController::class, "linkage"])->name("library.linkage");
+
         Route::get("asset/{asset_id}/report",[API\AssetController::class, 'report'])->name('asset.report');
         Route::get("asset-tree",[API\AssetController::class, 'tree'])->name('asset.tree');