about summary refs log tree commit diff stats
path: root/classes/Handler/TogglefavHandler.php
blob: d6a3913437ad37bae96dafba4a985b21aa5260bc (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
36
<?php

namespace Handler;

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

class TogglefavHandler implements RequestHandlerInterface, AuthRequired
{
    public function handle(Request $request): Response
    {
        $bookid = $request->getInt('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]));
    }
}