Skip to content

Commit 552f6e8

Browse files
committed
Added missing model
1 parent b494981 commit 552f6e8

1 file changed

Lines changed: 119 additions & 0 deletions

File tree

app/Models/Idea.php

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use App\Models\Concerns\HasAuthor;
6+
use App\Models\Enums\IdeaStatusEnum;
7+
use Illuminate\Database\Eloquent\Builder;
8+
use Illuminate\Database\Eloquent\Factories\HasFactory;
9+
use Illuminate\Database\Eloquent\Model;
10+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
11+
use Laravel\Scout\Searchable;
12+
use Orchid\Filters\Filterable;
13+
use Orchid\Filters\Types\Like;
14+
use Orchid\Screen\AsSource;
15+
16+
class Idea extends Model
17+
{
18+
use AsSource, Filterable, HasAuthor, HasFactory, Searchable;
19+
20+
protected $fillable = [
21+
'title',
22+
'description',
23+
'status',
24+
'user_id',
25+
'votes_count',
26+
];
27+
28+
protected $casts = [
29+
'status' => IdeaStatusEnum::class,
30+
'votes_count' => 'integer',
31+
];
32+
33+
protected $attributes = [
34+
'status' => 'proposed',
35+
'votes_count' => 0,
36+
];
37+
38+
protected $allowedFilters = [
39+
'title' => Like::class,
40+
'description' => Like::class,
41+
];
42+
43+
protected $allowedSorts = [
44+
'title',
45+
'votes_count',
46+
'created_at',
47+
];
48+
49+
public function scopePublished(Builder $query): Builder
50+
{
51+
return $query->where('status', IdeaStatusEnum::Published);
52+
}
53+
54+
public function isProposed(): bool
55+
{
56+
return $this->status === IdeaStatusEnum::Proposed;
57+
}
58+
59+
public function isImplemented(): bool
60+
{
61+
return $this->status === IdeaStatusEnum::Implemented;
62+
}
63+
64+
public function isRejected(): bool
65+
{
66+
return $this->status === IdeaStatusEnum::Rejected;
67+
}
68+
69+
public function isPublished(): bool
70+
{
71+
return $this->status === IdeaStatusEnum::Published;
72+
}
73+
74+
public function voters(): BelongsToMany
75+
{
76+
return $this->belongsToMany(User::class, 'idea_votes')
77+
->withPivot('vote')
78+
->withTimestamps();
79+
}
80+
81+
public function hasVotedBy(?User $user): bool
82+
{
83+
if (! $user) {
84+
return false;
85+
}
86+
87+
return $this->voters()->where('user_id', $user->id)->exists();
88+
}
89+
90+
public function getUserVote(?User $user): ?int
91+
{
92+
if (! $user) {
93+
return null;
94+
}
95+
96+
$vote = $this->voters()->where('user_id', $user->id)->first();
97+
98+
return $vote?->pivot->vote;
99+
}
100+
101+
public function toggleVote(User $user, int $voteValue = 1): void
102+
{
103+
$existingVote = $this->voters()->where('user_id', $user->id)->first();
104+
105+
if ($existingVote) {
106+
return;
107+
}
108+
109+
$this->voters()->attach($user->id, ['vote' => $voteValue]);
110+
$this->increment('votes_count', $voteValue);
111+
}
112+
113+
public function toSearchableArray(): array
114+
{
115+
return [
116+
'title' => $this->title,
117+
];
118+
}
119+
}

0 commit comments

Comments
 (0)