123movies-seo/app/Http/Controllers/NewsController.php

120 lines
3.3 KiB
PHP
Raw Permalink Normal View History

2024-08-24 23:08:42 +03:00
<?php
namespace App\Http\Controllers;
use App\Http\Requests\StoreNewsRequest;
use App\Http\Requests\UpdateNewsRequest;
use App\Models\News;
use App\Services\News\ImportNewsFromRemoteProvider;
use App\Supports\Traits\CleanItems;
use Inertia\Inertia;
use Illuminate\Support\Str;
use App\Supports\Traits\Helpers;
class NewsController extends Controller
{
use CleanItems;
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$news = News::orderByDesc('created_at')->limit(10)->get();
return $news;
}
public function list( ?int $page = 1)
{
$paginatedData = News::orderByDesc('created_at')->paginate(10, ['*'], '', $page);
$results = $paginatedData->map(fn($item) => (Object)$item->toArray())->map(function($article){
$article->title = Str::limit($article->title, 60);
$article->body = Str::limit($article->body, 150);
$article->meta['image'] = Helpers::getImageFullUrl( $article->meta['image'], 320, 180);
return $article;
});
// dd($results);
$meta['title'] = config('site.news.list.title', 'Popular Movies');
$meta['description'] = config('site.news.list.description', 'Popular Movies');
$meta['image'] = asset('assets/images/fb-capture.png');
$meta['route'] = 'news';
$pagination = ['total' => $paginatedData->lastPage(), 'page' => $page];
$data = $results;
if (defined('SHOULD_PRERENDER')) {
return view('prerender.list', compact('data', 'pagination', 'meta'));
}
$pagination = self::encodeForInertia($pagination);
$data = self::encodeForInertia($data ?? []);
$meta = self::encodeForInertia($meta ?? []) ;
return Inertia::render('News', compact('data', 'pagination', 'meta'));
}
/**
* Display the specified resource.
*
* @param \App\Models\News $news
* @return \Illuminate\Http\Response
*/
public function show(News $news, $slug, $id)
{
$article = (Object)$news->find($id)->toArray();
dd($article);
if (defined('SHOULD_PRERENDER')) {
return view('prerender.list', compact('data', 'pagination', 'meta'));
}
return Inertia::render('News', compact('data', 'pagination', 'meta'));
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\News $news
* @return \Illuminate\Http\Response
*/
public function edit(News $news)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \App\Http\Requests\UpdateNewsRequest $request
* @param \App\Models\News $news
* @return \Illuminate\Http\Response
*/
public function update(UpdateNewsRequest $request, News $news)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\News $news
* @return \Illuminate\Http\Response
*/
public function destroy(News $news)
{
//
}
/**
* @return JsonResponse
*/
public function importFromRemoteProvider()
{
app(ImportNewsFromRemoteProvider::class)->execute();
return $this->success();
}
}