-
routes/api.php
Open in GitHubuse Illuminate\Support\Facades\Route; Route::get('recent', 'Api\RecentController')->middleware('throttle:60');
-
app/Http/Controllers/Api/RecentController.php
Open in GitHubuse App\CacheKeys; use App\Http\Controllers\Controller; use App\Http\Resources\Package as PackageResource; use App\Package; use Illuminate\Http\Request; use Illuminate\Support\Facades\Cache; class RecentController extends Controller { const CACHE_LENGTH = 10; public function __invoke(Request $request) { return PackageResource::collection($this->recent()); } private function recent() { return Cache::remember(CacheKeys::recentPackages(), self::CACHE_LENGTH, function () { return Package::latest()->with(['author', 'tags'])->take(10)->get(); }); } }
-
app/Http/Resources/PackageResource.php
Open in GitHubuse App\Favorite; use App\Package; use Illuminate\Support\Str; class PackageResource extends ModelResource { public $model = Package::class; const CACHE_RATINGS_LENGTH = 5; public function toArray($package) { return [ 'id' => $package->id, 'name' => $package->name, 'composer_name' => $package->composer_name, 'packagist_namespace' => $package->composer_vendor, 'packagist_name' => $package->composer_package, 'abstract' => $package->abstract, 'is_disabled' => $package->is_disabled, 'icon_url' => $package->picture_url ?? 'https://api.adorable.io/avatars/285/'.Str::slug($package->name).'.png', 'url' => $package->url, 'average_rating' => $this->averageRating($package), 'rating_count' => $this->ratingCount($package), 'created_at' => $package->created_at->diffForHumans(), 'author' => [ 'id' => $package->author_id, 'name' => $package->author->name, 'url' => $package->author->url, 'avatar_url' => $package->author->avatar ?: 'https://api.adorable.io/avatars/285/'.Str::slug($package->author->name).'.png', 'github_username' => $package->author->github_username, ], ]; } // }