diff --git a/src/Http/Resources/LibraryResource.php b/src/Http/Resources/LibraryResource.php index 30c82935..ea2e6514 100644 --- a/src/Http/Resources/LibraryResource.php +++ b/src/Http/Resources/LibraryResource.php @@ -45,6 +45,8 @@ public function toArray($request = null): array 'hasIcon' => $this->hasIcon, 'libraryId' => $this->libraryId, 'languages' => $this->languages, + 'contentsCount' => $this->contentsCount, + 'requiredLibrariesCount' => $this->requiredLibrariesCount, ]; } } diff --git a/src/Models/H5PLibrary.php b/src/Models/H5PLibrary.php index 1ac9b904..24e3505f 100644 --- a/src/Models/H5PLibrary.php +++ b/src/Models/H5PLibrary.php @@ -172,7 +172,9 @@ class H5PLibrary extends Model 'hasIcon', 'libraryId', 'languages', - 'addTo' + 'addTo', + 'contentsCount', + 'requiredLibrariesCount', ]; protected $appends = [ @@ -187,7 +189,7 @@ class H5PLibrary extends Model 'tutorialUrl', 'hasIcon', 'libraryId', - 'addTo' + 'addTo', ]; protected $hidden = [ @@ -272,11 +274,26 @@ public function getHasIconAttribute():string return isset($this->attributes['has_icon']) ? $this->attributes['has_icon'] : ''; } + public function getContentsCountAttribute(): int + { + return $this->contents()->count(); + } + + public function getRequiredLibrariesCountAttribute(): int + { + return $this->requiredLibraries()->count(); + } + public function dependencies(): HasMany { return $this->hasMany(H5PLibraryDependency::class, 'library_id', 'id'); } + public function requiredLibraries(): HasMany + { + return $this->hasMany(H5PLibraryDependency::class, 'required_library_id', 'id'); + } + public function languages(): HasMany { return $this->hasMany(H5PLibraryLanguage::class, 'library_id'); diff --git a/src/Services/HeadlessH5PService.php b/src/Services/HeadlessH5PService.php index 6e95b8b5..52053ea7 100644 --- a/src/Services/HeadlessH5PService.php +++ b/src/Services/HeadlessH5PService.php @@ -170,15 +170,19 @@ public function getConfig(): array public function getLibraries(string $machineName = null, string $major_version = null, string $minor_version = null) { $lang = config('hh5p.language'); - $libraries_url = url(config('hh5p.h5p_library_url')); + if ($machineName) { $defaultLang = $this->getEditor()->getLibraryLanguage($machineName, $major_version, $minor_version, $lang); return $this->getEditor()->getLibraryData($machineName, $major_version, $minor_version, $lang, '', $libraries_url, $defaultLang); } - return $this->getEditor()->getLibraries(); + return collect($this->getEditor()->getLibraries()) + ->each(fn($item) => $item + ->append('contentsCount') + ->append('requiredLibrariesCount') + ); } public function getEditorSettings($content = null): array @@ -495,7 +499,7 @@ public function deleteLibrary($id): bool $library = H5pLibrary::findOrFail($id); $libraryUsage = $this->getRepository()->getLibraryUsage($library->getKey()); - if ($libraryUsage['content'] > 0 ) { + if ($libraryUsage['content'] > 0) { return false; } diff --git a/tests/Api/LibraryApiTest.php b/tests/Api/LibraryApiTest.php index bb66c83a..79da8463 100644 --- a/tests/Api/LibraryApiTest.php +++ b/tests/Api/LibraryApiTest.php @@ -57,7 +57,9 @@ public function test_library_index(): void 'id', 'machineName', 'majorVersion', - 'minorVersion' + 'minorVersion', + 'contentsCount', + 'requiredLibrariesCount' ]] ]); } @@ -215,7 +217,26 @@ public function testHubContentHubMetadataCacheUnauthorized(): void ->assertUnauthorized(); } - + public function testGetLibrariesAdmin(): void + { + $this->authenticateAsAdmin(); + + $lib1 = H5PLibrary::factory()->create(); + $lib2 = H5PLibrary::factory()->create(); + H5PLibraryDependency::factory()->count(3)->create(['required_library_id' => $lib1->getKey()]); + H5PLibraryDependency::factory()->count(7)->create(['required_library_id' => $lib2->getKey()]); + H5PContent::factory()->count(2)->create(['library_id' => $lib1->getKey()]); + H5PContent::factory()->count(5)->create(['library_id' => $lib2->getKey()]); - + $response = $this + ->actingAs($this->user, 'api') + ->getJson('api/admin/hh5p/libraries') + ->assertOk(); + + $data = $response->getData(); + $this->assertEquals(2, current(array_filter($data, fn($item) => $item->id === $lib1->getKey()))->contentsCount); + $this->assertEquals(5, current(array_filter($data, fn($item) => $item->id === $lib2->getKey()))->contentsCount); + $this->assertEquals(3, current(array_filter($data, fn($item) => $item->id === $lib1->getKey()))->requiredLibrariesCount); + $this->assertEquals(7, current(array_filter($data, fn($item) => $item->id === $lib2->getKey()))->requiredLibrariesCount); + } }