You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.4 KiB
51 lines
1.4 KiB
|
2 weeks ago
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Controllers\Miniapp;
|
||
|
|
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
use App\Models\News;
|
||
|
|
use App\Support\ApiResponse;
|
||
|
|
use App\Support\Miniapp\MiniappPresenter;
|
||
|
|
use Illuminate\Http\JsonResponse;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
|
||
|
|
class NewsController extends Controller
|
||
|
|
{
|
||
|
|
use ApiResponse;
|
||
|
|
|
||
|
|
public function index(Request $request): JsonResponse
|
||
|
|
{
|
||
|
|
$query = News::query()
|
||
|
|
->with(['categoryItem'])
|
||
|
|
->where('status', 1)
|
||
|
|
->whereNotNull('published_at');
|
||
|
|
|
||
|
|
if ($request->filled('category_dict_item_id')) {
|
||
|
|
$query->where('category_dict_item_id', (int) $request->query('category_dict_item_id'));
|
||
|
|
}
|
||
|
|
|
||
|
|
$paginator = $query
|
||
|
|
->orderByDesc('published_at')
|
||
|
|
->orderByDesc('id')
|
||
|
|
->paginate((int) $request->query('page_size', 20))
|
||
|
|
->withQueryString();
|
||
|
|
|
||
|
|
$paginator->getCollection()->transform(
|
||
|
|
fn (News $news) => MiniappPresenter::serializeNewsList($news)
|
||
|
|
);
|
||
|
|
|
||
|
|
return $this->paginated($paginator);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function show(int $news): JsonResponse
|
||
|
|
{
|
||
|
|
$model = News::query()
|
||
|
|
->with(['categoryItem'])
|
||
|
|
->where('status', 1)
|
||
|
|
->whereNotNull('published_at')
|
||
|
|
->findOrFail($news);
|
||
|
|
|
||
|
|
return $this->ok(MiniappPresenter::serializeNewsDetail($model));
|
||
|
|
}
|
||
|
|
}
|