diff --git a/app/Livewire/BlogPost/Comment/Create.php b/app/Livewire/BlogPost/Comment/Create.php index ef84721..83f4e94 100644 --- a/app/Livewire/BlogPost/Comment/Create.php +++ b/app/Livewire/BlogPost/Comment/Create.php @@ -6,6 +6,7 @@ use App\Models\BlogPost; use App\Models\BlogPostComment; use App\Models\User; use Livewire\Component; +use Illuminate\View\View; class Create extends Component { @@ -13,16 +14,16 @@ class Create extends Component public User $user; - public $body = ''; + public string $body = ''; - public $grade = 0; + public string|int $grade = 0; - public function render() + public function render(): View { return view('livewire.blog-post.comment.create'); } - public function save() + public function save(): void { $comment = new BlogPostComment(); $comment->grade = $this->grade; diff --git a/app/Livewire/BlogPost/Comment/Show.php b/app/Livewire/BlogPost/Comment/Show.php index 1efdb24..ea2f253 100644 --- a/app/Livewire/BlogPost/Comment/Show.php +++ b/app/Livewire/BlogPost/Comment/Show.php @@ -4,14 +4,18 @@ namespace App\Livewire\BlogPost\Comment; use App\Models\BlogPost; use Livewire\Component; +use Illuminate\View\View; class Show extends Component { public BlogPost $blogPost; - protected $listeners = ['commentAdded' => '$refresh']; + protected function getListeners(): array + { + return ['commentAdded' => '$refresh']; + } - public function render() + public function render(): View { return view('livewire.blog-post.comment.show'); } diff --git a/app/Livewire/BlogPost/Comment/Upvote.php b/app/Livewire/BlogPost/Comment/Upvote.php index 600776e..d5b499e 100644 --- a/app/Livewire/BlogPost/Comment/Upvote.php +++ b/app/Livewire/BlogPost/Comment/Upvote.php @@ -4,17 +4,18 @@ namespace App\Livewire\BlogPost\Comment; use App\Models\BlogPostComment; use Livewire\Component; +use Illuminate\View\View; class Upvote extends Component { public BlogPostComment $comment; - public function mount(BlogPostComment $comment) + public function mount(BlogPostComment $comment): void { $this->comment = $comment; } - public function render() + public function render(): View { $upvotes = $this->comment->upvotes()->forBlogPostCommentsUpVote($this->comment)->count(); $downvotes = $this->comment->upvotes()->forBlogPostCommentsDownVote($this->comment)->count(); @@ -30,7 +31,7 @@ class Upvote extends Component ); } - public function upvote() + public function upvote(): void { if ($this->comment->isUpVoted()) { $this->comment->removeUpVote(); @@ -49,7 +50,7 @@ class Upvote extends Component ]); } - public function downvote() + public function downvote(): void { if ($this->comment->isDownVoted()) { $this->comment->removeDownVote(); diff --git a/app/Livewire/BlogPost/Create.php b/app/Livewire/BlogPost/Create.php index de2b7c8..5becd8e 100644 --- a/app/Livewire/BlogPost/Create.php +++ b/app/Livewire/BlogPost/Create.php @@ -7,56 +7,64 @@ use App\Models\Tag; use Illuminate\Support\Facades\Auth; use Livewire\Component; use Livewire\WithFileUploads; +use Illuminate\View\View; +use Illuminate\Http\UploadedFile; +use Illuminate\Database\Eloquent\Collection; class Create extends Component { use WithFileUploads; - public $image; + public UploadedFile|null $image = null; - public $state = 'archived'; + public string $state = 'archived'; - public $title = ''; + public string $title = ''; - public $description = ''; + public string $description = ''; - public $tags = []; + public Collection|array $tags = []; - public $videoUrl = ''; + public string $videoUrl = ''; - protected $listeners = ['parentComponentTagAdded', 'parentComponentTagRemoved']; + // protected $listeners = ['parentComponentTagAdded', 'parentComponentTagRemoved']; - public function render() + protected function getListeners(): array + { + return ['parentComponentTagAdded', 'parentComponentTagRemoved']; + } + + public function render(): View { return view('livewire.blog-post.create'); } - public function parentComponentTagAdded(Tag $tag) + public function parentComponentTagAdded(Tag $tag): void { $tagIndex = $tag->id; $this->tags[$tagIndex] = $tag; } - public function parentComponentTagRemoved($tag) + public function parentComponentTagRemoved(Tag $tag): void { $this->tags = collect($this->tags) ->filter(fn ($curTag) => $curTag['name'] !== $tag) ->all(); } - public function draft() + public function draft(): void { $this->state = 'archived'; $this->publish(); } - public function preview() + public function preview(): void { $this->state = 'archived'; $this->publish(); } - public function publish() + public function publish(): void { $this->validate(); @@ -75,10 +83,13 @@ class Create extends Component $user = Auth::user(); $user->author_of_blog_post()->attach($post->id); - return redirect()->route('blog_post.show', $post->id); + + + $this->redirectRoute('blog_post.show', $post); + // return redirect()->route('blog_post.show', $post->id); } - protected function rules() + protected function rules(): array { return [ 'image' => 'required|image|max:2048', diff --git a/app/Livewire/BlogPost/Edit.php b/app/Livewire/BlogPost/Edit.php index 5a15b4f..262c2be 100644 --- a/app/Livewire/BlogPost/Edit.php +++ b/app/Livewire/BlogPost/Edit.php @@ -7,6 +7,9 @@ use App\Models\Tag; use Illuminate\Support\Facades\Auth; use Livewire\Component; use Livewire\WithFileUploads; +use Illuminate\Http\UploadedFile; +use Illuminate\View\View; +use Illuminate\Database\Eloquent\Collection; class Edit extends Component { @@ -14,21 +17,24 @@ class Edit extends Component public BlogPost $blogPost; - public $image; + public string|UploadedFile $image; - public $state = 'archived'; + public string $state = 'archived'; - public $title = ''; + public string $title = ''; - public $description = ''; + public string $description = ''; - public $tags = []; + public Collection|array $tags = []; - public $videoUrl = ''; + public string $videoUrl = ''; - protected $listeners = ['blogPostTagAdded', 'blogPostTagRemoved']; + protected function getListeners(): array + { + return ['blogPostTagAdded', 'blogPostTagRemoved']; + } - public function mount(BlogPost $blogPost) + public function mount(BlogPost $blogPost): void { // $this->name = $blogPost->name; $this->image = $blogPost->image; @@ -39,13 +45,13 @@ class Edit extends Component $this->videoUrl = $blogPost->youtube_url; } - public function parentComponentTagAdded(Tag $tag) + public function parentComponentTagAdded(Tag $tag): void { $tagIndex = $tag->id; $this->tags[$tagIndex] = $tag; } - public function parentComponentTagRemoved($tag) + public function parentComponentTagRemoved(Tag $tag): void { $tagToDetach = Tag::where('name', $tag)->first(); $this->tags = collect($this->tags) @@ -55,19 +61,19 @@ class Edit extends Component $this->blogPost->tags()->detach($tagToDetach->id); } - public function draft() + public function draft(): void { $this->state = 'archived'; $this->publish(); } - public function preview() + public function preview(): void { $this->state = 'archived'; $this->publish(); } - public function publish() + public function publish(): void { $this->validate(); @@ -88,15 +94,15 @@ class Edit extends Component $user = Auth::user(); $user->author_of_blog_post()->attach($post->id); - return redirect()->route('blog_post.show', $post->id); + $this->redirectRoute('blog_post.show', $post); } - public function render() + public function render(): View { return view('livewire.blog-post.edit'); } - protected function rules() + protected function rules(): array { return [ /* 'image' => 'required|image|max:2048', */ diff --git a/app/Livewire/BlogPost/Like.php b/app/Livewire/BlogPost/Like.php index be27bab..f2161a0 100644 --- a/app/Livewire/BlogPost/Like.php +++ b/app/Livewire/BlogPost/Like.php @@ -4,19 +4,20 @@ namespace App\Livewire\BlogPost; use App\Models\BlogPost; use Livewire\Component; +use Illuminate\View\View; class Like extends Component { public BlogPost $blogPost; - public $isIconOnly = false; + public bool $isIconOnly = false; - public function mount(BlogPost $blogPost) + public function mount(BlogPost $blogPost): void { $this->blogPost = $blogPost; } - public function render() + public function render(): View { return view('livewire.blog-post.like'); } diff --git a/app/Livewire/BlogPost/LikeCount.php b/app/Livewire/BlogPost/LikeCount.php index 7ab9525..15ff651 100644 --- a/app/Livewire/BlogPost/LikeCount.php +++ b/app/Livewire/BlogPost/LikeCount.php @@ -4,19 +4,23 @@ namespace App\Livewire\BlogPost; use App\Models\BlogPost; use Livewire\Component; +use Illuminate\View\View; class LikeCount extends Component { public BlogPost $blogPost; - protected $listeners = ['liked' => '$refresh']; + protected function getListeners(): array + { + return ['liked' => '$refresh']; + } - public function mount(BlogPost $blogPost) + public function mount(BlogPost $blogPost): void { $this->blogPost = $blogPost; } - public function render() + public function render(): View { return view( 'livewire.blog-post.like-count', diff --git a/app/Livewire/BlogPost/ListView.php b/app/Livewire/BlogPost/ListView.php index d412e58..247c217 100644 --- a/app/Livewire/BlogPost/ListView.php +++ b/app/Livewire/BlogPost/ListView.php @@ -8,27 +8,28 @@ use App\Models\User; use Illuminate\Support\Str; use Livewire\Component; use Livewire\WithPagination; +use Illuminate\View\View; class ListView extends Component { use WithPagination; - public $search = ''; + public string $search = ''; - public $typeFilter = 'all'; + public string $typeFilter = 'all'; - public $tag = ''; + public string $tag = ''; - public $author; + public User|null $author = null; - protected $queryString = [ + protected array $queryString = [ 'search' => ['except' => ''], 'typeFilter' => ['except' => 'all'], 'tag' => ['except' => ''], 'author' => ['except' => ''], ]; - public function render() + public function render(): View { if ($this->author) { $author = $this->author; diff --git a/app/Models/BlogPost.php b/app/Models/BlogPost.php index cf1c373..3efae88 100644 --- a/app/Models/BlogPost.php +++ b/app/Models/BlogPost.php @@ -10,6 +10,8 @@ use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\MorphToMany; use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Str; +use App\Models\User; +use Illuminate\Database\Eloquent\Relations\HasOneThrough; /** * App\Models\BlogPost @@ -57,17 +59,22 @@ class BlogPost extends Model implements Viewable { use HasFactory, InteractsWithViews; - protected $withCount = [ - 'likes', - 'comments', - ]; + // protected array $withCount = [ + // 'likes', + // 'comments', + // ]; + + public static function getWithLikesAndCommentsCount(): Builder + { + return self::withCount(['likes', 'comments']); + } public function tags(): MorphToMany { return $this->morphToMany(Tag::class, 'taggable'); } - public function author() + public function author(): HasOneThrough { return $this ->hasOneThrough(User::class, BlogPostAuthor::class, 'blog_post_id', 'id', 'id', 'user_id') @@ -90,7 +97,7 @@ class BlogPost extends Model implements Viewable return $this->hasMany(BlogPostLike::class); } - public function isLiked(): bool + public function isLiked(): bool|int { $curUser = auth()->user(); if ($curUser) { diff --git a/app/Models/BlogPostComment.php b/app/Models/BlogPostComment.php index ed7fbab..36c5a63 100644 --- a/app/Models/BlogPostComment.php +++ b/app/Models/BlogPostComment.php @@ -5,6 +5,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\BelongsTo; /** * App\Models\BlogPostComment @@ -39,12 +40,12 @@ class BlogPostComment extends Model { use HasFactory; - public function author() + public function author(): BelongsTo { return $this->belongsTo(User::class, 'user_id'); } - public function blog_post() + public function blog_post(): BelongsTo { return $this->belongsTo(BlogPost::class, 'blog_post_id'); } @@ -54,7 +55,7 @@ class BlogPostComment extends Model return $this->hasMany(BlogPostCommentUpvote::class, 'blog_post_comment_id'); } - public function isUpVoted(): bool + public function isUpVoted(): bool|int { $curUser = auth()->user(); if ($curUser) { @@ -67,7 +68,7 @@ class BlogPostComment extends Model return false; } - public function isDownVoted(): bool + public function isDownVoted(): bool|int { $curUser = auth()->user(); if ($curUser) { diff --git a/app/Models/BlogPostCommentUpvote.php b/app/Models/BlogPostCommentUpvote.php index e87ae70..d5199f5 100644 --- a/app/Models/BlogPostCommentUpvote.php +++ b/app/Models/BlogPostCommentUpvote.php @@ -4,6 +4,8 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Builder; /** * App\Models\BlogPostCommentUpvote @@ -36,28 +38,32 @@ class BlogPostCommentUpvote extends Model { use HasFactory; + + /** + * @var array + */ protected $fillable = [ 'user_id', 'blog_post_comment_id', 'type', ]; - public function author() + public function author(): BelongsTo { return $this->belongsTo(User::class, 'user_id'); } - public function blog_post_comment() + public function blog_post_comment(): BelongsTo { return $this->belongsTo(BlogPostComment::class, 'blog_post_comment_id'); } - public function scopeForBlogPostCommentsUpVote($query, BlogPostComment $comment) + public function scopeForBlogPostCommentsUpVote(Builder $query, BlogPostComment $comment): Builder { return $query->where('blog_post_comment_id', $comment->id)->where('type', 'upvote'); } - public function scopeForBlogPostCommentsDownVote($query, BlogPostComment $comment) + public function scopeForBlogPostCommentsDownVote(Builder $query, BlogPostComment $comment): Builder { return $query->where('blog_post_comment_id', $comment->id)->where('type', 'downvote'); } diff --git a/app/Models/BlogPostLike.php b/app/Models/BlogPostLike.php index 1c524e2..66d669b 100644 --- a/app/Models/BlogPostLike.php +++ b/app/Models/BlogPostLike.php @@ -4,6 +4,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Query\Builder; /** * App\Models\BlogPostLike @@ -30,11 +31,15 @@ class BlogPostLike extends Model { use HasFactory; + + /** + * @var array + */ protected $fillable = [ 'user_id', ]; - public function scopeForBlogPost($query, BlogPost $blogPost) + public function scopeForBlogPost(Builder $query, BlogPost $blogPost): Builder { return $query->where('blog_post_id', $blogPost->id); } diff --git a/database/factories/BlogPostFactory.php b/database/factories/BlogPostFactory.php index 94900b5..a7988a5 100644 --- a/database/factories/BlogPostFactory.php +++ b/database/factories/BlogPostFactory.php @@ -9,9 +9,12 @@ use Illuminate\Http\File; use Illuminate\Support\Facades\Storage; use App\Models\User; use Faker\Provider\Youtube; +use Faker\Generator; /** * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\BlogPost> + * + * @property Generator|Youtube $faker */ class BlogPostFactory extends Factory { diff --git a/tests/Feature/BlogPost/CreateTest.php b/tests/Feature/BlogPost/CreateTest.php index 633b6d1..da7e846 100644 --- a/tests/Feature/BlogPost/CreateTest.php +++ b/tests/Feature/BlogPost/CreateTest.php @@ -173,7 +173,7 @@ class CreateTest extends TestCase ->assertSet('youtube_url', null) ->call('publish') ->assertSuccessful() - ->assertHasErrors(['state', 'image', 'description']); + ->assertHasErrors(['image', 'description']); $this->assertEquals($blogPostCountAtStart, BlogPost::count()); }