mirror of
https://github.com/AnimeThemes/animethemes-server.git
synced 2026-09-04 11:45:00 +02:00
40 lines
967 B
PHP
40 lines
967 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Actions\Models\Aggregate;
|
|
|
|
use App\Actions\ActionResult;
|
|
use App\Enums\Actions\ActionStatus;
|
|
use Exception;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class SyncLikeAggregatesAction
|
|
{
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public function handle(): ActionResult
|
|
{
|
|
try {
|
|
DB::statement('
|
|
INSERT INTO like_aggregates (likeable_id, likeable_type, value)
|
|
SELECT likeable_id, likeable_type, COUNT(*) as value
|
|
FROM likes
|
|
GROUP BY likeable_type, likeable_id
|
|
ON DUPLICATE KEY UPDATE value = VALUES(value);
|
|
');
|
|
} catch (Exception $e) {
|
|
return new ActionResult(
|
|
ActionStatus::FAILED,
|
|
$e->getMessage()
|
|
);
|
|
}
|
|
|
|
return new ActionResult(
|
|
ActionStatus::PASSED,
|
|
'Like aggregates synced successfully',
|
|
);
|
|
}
|
|
}
|