mirror of
https://github.com/AnimeThemes/animethemes-server.git
synced 2026-07-11 01:24:46 +02:00
42 lines
1001 B
PHP
42 lines
1001 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 SyncViewAggregatesAction
|
|
{
|
|
/**
|
|
* Handles the action.
|
|
*
|
|
* @throws Exception
|
|
*/
|
|
public function handle(): ActionResult
|
|
{
|
|
try {
|
|
DB::statement('
|
|
INSERT INTO view_aggregates (viewable_id, viewable_type, value)
|
|
SELECT viewable_id, viewable_type, COUNT(*) as value
|
|
FROM views
|
|
GROUP BY viewable_type, viewable_id
|
|
ON DUPLICATE KEY UPDATE value = VALUES(value);
|
|
');
|
|
} catch (Exception $e) {
|
|
return new ActionResult(
|
|
ActionStatus::FAILED,
|
|
$e->getMessage()
|
|
);
|
|
}
|
|
|
|
return new ActionResult(
|
|
ActionStatus::PASSED,
|
|
'View aggregates synced successfully',
|
|
);
|
|
}
|
|
}
|