forked from eatvio/eatvio-infra
68 lines
1.8 KiB
PHP
68 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\BlogPost;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
use Illuminate\Support\Facades\Gate;
|
|
|
|
/* use Spatie\QueryBuilder\AllowedFilter; */
|
|
/* use Spatie\QueryBuilder\QueryBuilder; */
|
|
|
|
class BlogPostController extends Controller
|
|
{
|
|
public function index(Request $request): View
|
|
{
|
|
// if ($request->has('author')) {
|
|
// $author = $request->get('author');
|
|
// $user = User::find($author);
|
|
// $blogPosts = $user ? $user->author_of_blog_post()->distinct()->limit(10)->get()->unique() : [];
|
|
// } elseif ($request->has('tag')) {
|
|
// $tagId = Str::of($request->get('tag'))->split('/-/')->first();
|
|
// $blogPosts = Tag::find($tagId)->blogPosts()->orderBy('created_at', 'desc')->limit(10)->get()->unique();
|
|
// } else {
|
|
// $blogPosts = BlogPost::orderBy('created_at', 'desc')->limit(10)->get();
|
|
// }
|
|
|
|
return view('blog_post/index');
|
|
}
|
|
|
|
public function create(): View
|
|
{
|
|
return view('blog_post/edit_form');
|
|
}
|
|
|
|
public function show(BlogPost $blogPost): View
|
|
{
|
|
$data['author'] = $blogPost->author()->first();
|
|
$data['tags'] = $blogPost->tags()->get();
|
|
$data['blogPost'] = $blogPost;
|
|
|
|
$expiresAt = now()->addHours(24);
|
|
|
|
views($blogPost)
|
|
->cooldown($expiresAt)
|
|
->record();
|
|
|
|
return view('blog_post/show', $data);
|
|
}
|
|
|
|
public function edit(BlogPost $blogPost): View
|
|
{
|
|
if (! $this->canEditBlogPost($blogPost)) {
|
|
abort(403, 'Unauthorized action.');
|
|
}
|
|
|
|
return view('blog_post/edit_form', ['blogPost' => $blogPost]);
|
|
}
|
|
|
|
|
|
private function canEditBlogPost(BlogPost $blogPost): bool
|
|
{
|
|
return Gate::allows('edit', $blogPost);
|
|
}
|
|
|
|
|
|
}
|