Commit workspace changes
This commit is contained in:
@@ -3,8 +3,11 @@
|
||||
namespace App\Http\Controllers\News;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use App\Services\News\NewsService;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\View\View;
|
||||
use cPad\Plugins\News\Models\NewsArticle;
|
||||
use cPad\Plugins\News\Models\NewsCategory;
|
||||
use cPad\Plugins\News\Models\NewsTag;
|
||||
@@ -12,32 +15,44 @@ use cPad\Plugins\News\Models\NewsView;
|
||||
|
||||
class NewsController extends Controller
|
||||
{
|
||||
public function __construct(private readonly NewsService $news)
|
||||
{
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Homepage — /news
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
public function index(Request $request)
|
||||
public function index(Request $request): View
|
||||
{
|
||||
$perPage = config('news.articles_per_page', 12);
|
||||
|
||||
$featured = NewsArticle::with('author', 'category')
|
||||
->published()
|
||||
->featured()
|
||||
->orderByDesc('published_at')
|
||||
->editorialOrder()
|
||||
->first();
|
||||
|
||||
$query = NewsArticle::with('author', 'category')
|
||||
$highlightQuery = NewsArticle::with('author', 'category')
|
||||
->published()
|
||||
->orderByDesc('published_at');
|
||||
->editorialOrder();
|
||||
|
||||
if ($featured) {
|
||||
$query->where('id', '!=', $featured->id);
|
||||
$highlightQuery->where('id', '!=', $featured->id);
|
||||
}
|
||||
|
||||
$articles = $query->paginate($perPage);
|
||||
$highlights = $highlightQuery->limit(3)->get();
|
||||
$excludedIds = collect([$featured?->id])->merge($highlights->pluck('id'))->filter()->all();
|
||||
|
||||
$articles = NewsArticle::with('author', 'category')
|
||||
->published()
|
||||
->when($excludedIds !== [], fn ($query) => $query->whereNotIn('id', $excludedIds))
|
||||
->editorialOrder()
|
||||
->paginate($perPage);
|
||||
|
||||
return view('news.index', [
|
||||
'featured' => $featured,
|
||||
'articles' => $articles,
|
||||
'featured' => $featured,
|
||||
'highlights' => $highlights,
|
||||
'articles' => $articles,
|
||||
] + $this->sidebarData());
|
||||
}
|
||||
|
||||
@@ -45,7 +60,7 @@ class NewsController extends Controller
|
||||
// Category page — /news/category/{slug}
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
public function category(Request $request, string $slug)
|
||||
public function category(Request $request, string $slug): View
|
||||
{
|
||||
$category = NewsCategory::where('slug', $slug)->where('is_active', true)->firstOrFail();
|
||||
$perPage = config('news.articles_per_page', 12);
|
||||
@@ -53,12 +68,12 @@ class NewsController extends Controller
|
||||
$articles = NewsArticle::with('author', 'category')
|
||||
->published()
|
||||
->byCategory($category->id)
|
||||
->orderByDesc('published_at')
|
||||
->editorialOrder()
|
||||
->paginate($perPage);
|
||||
|
||||
return view('news.category', [
|
||||
'category' => $category,
|
||||
'articles' => $articles,
|
||||
'category' => $category,
|
||||
'articles' => $articles,
|
||||
] + $this->sidebarData());
|
||||
}
|
||||
|
||||
@@ -66,7 +81,7 @@ class NewsController extends Controller
|
||||
// Tag page — /news/tag/{slug}
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
public function tag(Request $request, string $slug)
|
||||
public function tag(Request $request, string $slug): View
|
||||
{
|
||||
$tag = NewsTag::where('slug', $slug)->firstOrFail();
|
||||
$perPage = config('news.articles_per_page', 12);
|
||||
@@ -74,12 +89,46 @@ class NewsController extends Controller
|
||||
$articles = NewsArticle::with('author', 'category')
|
||||
->published()
|
||||
->whereHas('tags', fn ($q) => $q->where('news_tags.slug', $slug))
|
||||
->orderByDesc('published_at')
|
||||
->editorialOrder()
|
||||
->paginate($perPage);
|
||||
|
||||
return view('news.tag', [
|
||||
'tag' => $tag,
|
||||
'articles' => $articles,
|
||||
'tag' => $tag,
|
||||
'articles' => $articles,
|
||||
] + $this->sidebarData());
|
||||
}
|
||||
|
||||
public function archive(Request $request, int $year, int $month): View
|
||||
{
|
||||
abort_unless($month >= 1 && $month <= 12, 404);
|
||||
|
||||
$perPage = config('news.articles_per_page', 12);
|
||||
$articles = NewsArticle::with('author', 'category')
|
||||
->published()
|
||||
->whereYear('published_at', $year)
|
||||
->whereMonth('published_at', $month)
|
||||
->editorialOrder()
|
||||
->paginate($perPage);
|
||||
|
||||
return view('news.archive', [
|
||||
'archiveDate' => now()->setDate($year, $month, 1),
|
||||
'articles' => $articles,
|
||||
] + $this->sidebarData());
|
||||
}
|
||||
|
||||
public function author(Request $request, string $username): View
|
||||
{
|
||||
$author = User::query()->with('profile')->where('username', $username)->firstOrFail();
|
||||
$perPage = config('news.articles_per_page', 12);
|
||||
$articles = NewsArticle::with('author', 'category')
|
||||
->published()
|
||||
->where('author_id', $author->id)
|
||||
->editorialOrder()
|
||||
->paginate($perPage);
|
||||
|
||||
return view('news.author', [
|
||||
'author' => $author,
|
||||
'articles' => $articles,
|
||||
] + $this->sidebarData());
|
||||
}
|
||||
|
||||
@@ -87,9 +136,9 @@ class NewsController extends Controller
|
||||
// Article page — /news/{slug}
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
public function show(Request $request, string $slug)
|
||||
public function show(Request $request, string $slug): View
|
||||
{
|
||||
$article = NewsArticle::with('author', 'category', 'tags')
|
||||
$article = NewsArticle::with('author.profile', 'category', 'tags', 'relatedEntities')
|
||||
->published()
|
||||
->where('slug', $slug)
|
||||
->firstOrFail();
|
||||
@@ -98,17 +147,18 @@ class NewsController extends Controller
|
||||
$this->trackView($request, $article);
|
||||
|
||||
// Related articles (same category, excluding current)
|
||||
$related = NewsArticle::with('author')
|
||||
$related = NewsArticle::with('author', 'category')
|
||||
->published()
|
||||
->when($article->category_id, fn ($q) => $q->where('category_id', $article->category_id))
|
||||
->where('id', '!=', $article->id)
|
||||
->orderByDesc('published_at')
|
||||
->editorialOrder()
|
||||
->limit(config('news.related_limit', 4))
|
||||
->get();
|
||||
|
||||
return view('news.show', [
|
||||
'article' => $article,
|
||||
'related' => $related,
|
||||
'relatedEntities' => $this->news->resolveRelatedEntities($article, $request->user()),
|
||||
] + $this->sidebarData());
|
||||
}
|
||||
|
||||
@@ -140,13 +190,6 @@ class NewsController extends Controller
|
||||
|
||||
private function sidebarData(): array
|
||||
{
|
||||
return [
|
||||
'categories' => NewsCategory::active()->withCount('publishedArticles')->ordered()->get(),
|
||||
'trending' => NewsArticle::published()
|
||||
->orderByDesc('views')
|
||||
->limit(config('news.trending_limit', 5))
|
||||
->get(['id', 'title', 'slug', 'views', 'published_at']),
|
||||
'tags' => NewsTag::has('articles')->orderBy('name')->get(),
|
||||
];
|
||||
return $this->news->sidebarData();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user