53 lines
1.5 KiB
PHP
53 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Blog;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Resources\CategoryResource;
|
|
use App\Http\Resources\PostResource;
|
|
use App\Models\Blog\Category;
|
|
use App\Models\Blog\Post;
|
|
|
|
/**
|
|
* @OA\Tag(name="Blog Home", description="Blog homepage aggregated data")
|
|
*/
|
|
class HomeController extends Controller
|
|
{
|
|
/**
|
|
* @OA\Get(
|
|
* path="/blog/homepage",
|
|
* tags={"Blog Home"},
|
|
* summary="Get homepage data: latest posts, categories, most viewed, random",
|
|
* @OA\Response(response=200, description="OK")
|
|
* )
|
|
*/
|
|
public function index()
|
|
{
|
|
$latestPosts = Post::with(['category', 'tags', 'author'])
|
|
->orderBy('published_at', 'desc')
|
|
->take(5)
|
|
->get();
|
|
|
|
$latestCategories = Category::orderBy('created_at', 'desc')
|
|
->take(5)
|
|
->get();
|
|
|
|
$mostViewedPosts = Post::with(['category', 'tags', 'author'])
|
|
->orderBy('views', 'desc')
|
|
->take(5)
|
|
->get();
|
|
|
|
$randomPosts = Post::with(['category', 'tags', 'author'])
|
|
->inRandomOrder()
|
|
->take(4)
|
|
->get();
|
|
|
|
return response()->json([
|
|
'latest_posts' => PostResource::collection($latestPosts),
|
|
'latest_categories' => CategoryResource::collection($latestCategories),
|
|
'most_viewed_posts' => PostResource::collection($mostViewedPosts),
|
|
'random_posts' => PostResource::collection($randomPosts),
|
|
], 200);
|
|
}
|
|
}
|