Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

devhub-az/devhub

24 stars
1 code files
View devhub-az/devhub on GitHub

app/Http/Controllers/Api/ArticleController.php

Open in GitHub
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
 
class ArticleController extends Controller
{
//
public static function upOrDownVote(User $user, $target, string $type = 'up'): bool
{
$hasVoted = $user->{'has'.ucfirst($type).'Voted'}($target);
DB::beginTransaction();
 
try {
$user->{$type.'Vote'}($target);
if ($hasVoted) {
$user->cancelVote($target);
foreach ($target->hubs as $hub) {
$type === 'up' ? $hub->rating-- : $hub->rating++;
$hub->save();
}
$type === 'up' ? $target->creator->rating-- : $target->creator->rating++;
$target->author->save();
DB::commit();
 
return false;
}
foreach ($target->hubs as $hub) {
$type === 'up' ? $hub->rating++ : $hub->rating--;
$hub->save();
}
$type === 'up' ? $target->creator->rating++ : $target->creator->rating--;
$target->author->save();
DB::commit();
} catch (Exception $e) {
DB::rollback();
abort(500);
}
 
return true;
}
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.