-
app/Enums/NotificationType.php
Open in GitHubenum NotificationType : int { case LIKE = 1; case COMMENT = 2; public function name() : string { return match ($this) { static::LIKE => 'like', static::COMMENT => 'comment', }; } }
-
app/Models/Notification.php
Open in GitHubuse App\Types\Model; use App\Enums\NotificationType; class Notification extends Model { // protected $casts = [ 'id' => 'integer', 'user_id' => 'integer', 'tip_id' => 'integer', 'type' => NotificationType::class, 'message' => 'string', 'read_at' => 'datetime', ]; // }
-
app/Actions/Like/StoreAction.php
Open in GitHubuse App\Models\Tip; use App\Models\Like; use App\Models\User; use App\Types\Action; use App\Models\Notification; use App\Enums\NotificationType; use App\Actions\Notification\StoreAction as StoreNotificationAction; class StoreAction extends Action { public static function execute(User $user, Tip $tip) : Like { $payload = [ 'tip_id' => $tip->id, ]; if (static::notify($user, $tip)) { StoreNotificationAction::execute($tip->user, $user, $tip, NotificationType::LIKE); } return attempt(fn() => $user->likes()->create($payload)); } protected static function notify(User $user, Tip $tip) : bool { return Notification::query() ->where('tip_id', $tip->id) ->where('student_id', $user->id) ->doesntExist(); } }