42 lines
1.0 KiB
PHP
42 lines
1.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\API;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Topic;
|
|
use App\Http\Resources\TopicResource;
|
|
use Illuminate\Http\JsonResponse;
|
|
use App\Http\Resources\ArticleResource;
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class TopicsAPIController extends Controller
|
|
{
|
|
/**
|
|
* Discover Topics
|
|
*
|
|
* Take Random 14 Topics from Database
|
|
*
|
|
*/
|
|
public function index()
|
|
{
|
|
return TopicResource::collection(Topic::inRandomOrder()->take(12)->get());
|
|
}
|
|
|
|
/**
|
|
* Load all the articles for a given topics
|
|
*
|
|
* @param mixed $topic
|
|
*/
|
|
public function show(Topic $topic)
|
|
{
|
|
$currentPage = request()->get('page',1);
|
|
return Cache::remember($topic->slug. '_'.$currentPage, 300, function () use ($topic) {
|
|
return response()->json([
|
|
'topic' => new TopicResource($topic),
|
|
'articles' => $topic->articles()->with('source')->latest('published_date')->paginate(12)
|
|
]);
|
|
});
|
|
}
|
|
}
|