about summary refs log tree commit diff stats
path: root/classes/Handler/TogglefavHandler.php
blob: 83e41591cd8f3f13743e1d760f2382cb95cedb0d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php

namespace Handler;

use ORM;
use Request;
use RequestHandlerInterface;
use Response;

class TogglefavHandler implements RequestHandlerInterface
{
    public function handle(Request $request): Response
    {
        $bookid = (int) $request->getAttribute('id');
        $owner = $_SESSION["owner"] ?? "";

        $fav = ORM::for_table('epube_favorites')
            ->where('bookid', $bookid)
            ->where('owner', $owner)
            ->find_one();

        if ($fav) {
            $fav->delete();
            $status = 0;
        } else {
            $fav = ORM::for_table('epube_favorites')->create();
            $fav->bookid = $bookid;
            $fav->owner = $owner;
            $fav->save();
            $status = 1;
        }

        return (new Response())->withBody(json_encode(["id" => $bookid, "status" => $status]));
    }
}