designtopack/public/site/config/routes/toggle-favorite.php

37 lines
922 B
PHP
Raw Normal View History

2024-09-11 07:32:34 +02:00
<?php
return [
'pattern' => '(:all)toggle-favorite.json',
'method' => 'post',
'action' => function () {
$json = file_get_contents('php://input');
$data = json_decode($json);
$fileName = $data->fileName;
$page = page($data->inspirationUri);
$file = $page->files()->find($fileName);
$user = Find::user($data->userUuid);
$favoriteForUsers = $file->favoriteForUsers()->toUsers();
2024-09-11 07:36:00 +02:00
$action = null;
2024-09-11 07:32:34 +02:00
if ($favoriteForUsers->has($user)) {
$favoriteForUsers->remove($user);
2024-09-11 07:36:00 +02:00
$action = 'retirée des';
2024-09-11 07:32:34 +02:00
} else {
$favoriteForUsers->add($user);
2024-09-11 07:36:00 +02:00
$action = 'ajoutée aux';
2024-09-11 07:32:34 +02:00
}
$array = $favoriteForUsers->toArray();
$yaml = Data::encode($array, 'yaml');
$newFile = $file->update([
2024-09-11 07:32:34 +02:00
'favoriteForUsers' => $yaml
]);
return json_encode($newFile->favoriteForUsers()->value());
2024-09-11 07:32:34 +02:00
}
];