Browse Source

library delete

moell 10 months ago
parent
commit
5c827eb01a
2 changed files with 14 additions and 1 deletions
  1. 9 1
      app/Http/Controllers/API/LibraryController.php
  2. 5 0
      app/Models/Library.php

+ 9 - 1
app/Http/Controllers/API/LibraryController.php

@@ -67,7 +67,15 @@ class LibraryController extends Controller
      */
     public function destroy(string $id)
     {
-        //
+        $library = Library::query()->allowed()->findOrFail($id);
+
+        if ($library->container()->count() > 0) {
+            return $this->badRequest("The container has been associated and is not allowed to be deleted");
+        }
+
+        $library->delete();
+
+        return $this->noContent();
     }
 
     public function linkage(Request $request, string $type)

+ 5 - 0
app/Models/Library.php

@@ -28,4 +28,9 @@ class Library extends Model
     {
 
     }
+
+    public function container(): \Illuminate\Database\Eloquent\Relations\HasMany
+    {
+        return $this->hasMany(Container::class, "library_id");
+    }
 }