Toggle favorite working
This commit is contained in:
parent
5e3f4ec621
commit
4ea2871c6d
13 changed files with 300 additions and 169 deletions
33
public/site/config/routes/toggle-favorite.php
Normal file
33
public/site/config/routes/toggle-favorite.php
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
<?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();
|
||||
|
||||
if ($favoriteForUsers->has($user)) {
|
||||
$favoriteForUsers->remove($user);
|
||||
} else {
|
||||
$favoriteForUsers->add($user);
|
||||
}
|
||||
|
||||
$array = $favoriteForUsers->toArray();
|
||||
$yaml = Data::encode($array, 'yaml');
|
||||
|
||||
$file->update([
|
||||
'favoriteForUsers' => $yaml
|
||||
]);
|
||||
|
||||
return $favoriteForUsers->toJson();
|
||||
}
|
||||
];
|
||||
Loading…
Add table
Add a link
Reference in a new issue