Пример #1
0
$app->put('/emoji/:id', function ($id) use($app, $db) {
    $app->response()->header('Content-Type', 'application/json');
    $token = $app->request->headers->get('token');
    if ($token == null) {
        echo "Please provide a token";
        return false;
    }
    $data = $db->users()->where('token', $token)->fetch('time');
    $result = date($data);
    /*
     * To calculate the difference in time between when you log in and when
     * you perform this request.
     */
    $userLoginTime = new DateTime(date('Y-m-d H:i:s', strtotime(str_replace('-', '/', $result))));
    $currentRequestTime = new DateTime(date('Y-m-d H:i:s'));
    $timeDifference = $userLoginTime->diff($currentRequestTime);
    $timeDifferenceInMinutes = $timeDifference->h * 60 + $timeDifference->i;
    if ($token = $db->users()->where('token', $token)->fetch('token')) {
        if ($timeDifferenceInMinutes > 300) {
            $user = $db->users()->where('token', $token)->fetch();
            $token = '';
            $data = ['token' => $token];
            $result = $user->update($data);
            echo "login first";
        } else {
            $user = $db->users()->where('token', $token)->fetch('name');
            $emoji = $db->emojis()->where('id', $id);
            if ($emoji->fetch()) {
                $user = $db->users()->where('token', $token)->fetch('name');
                $name = $app->request()->put('name');
                $keywords = $app->request()->put('keywords');
                $category = $app->request()->put('category');
                $emojiUpdate = ['name' => "{$name}", 'keywords' => "{$keywords}", 'category' => "{$category}", 'user' => "{$user}"];
                $result = $emoji->update($emojiUpdate);
                $emoji = $db->emojis()->where('id', $id);
                $emojis = array();
                foreach ($db->emojis() as $emoji) {
                    $emojis[] = array('id' => $emoji['id'], 'name' => $emoji['name'], 'keywords' => $emoji['keywords'], 'emoji' => $emoji['emoji'], 'category' => $emoji['category'], 'created_at' => $emoji['created_at'], 'updated_at' => $emoji['updated_at'], 'user' => $emoji['user']);
                }
                echo json_encode($emojis, JSON_FORCE_OBJECT);
            } else {
                echo json_encode(array('status' => false, 'message' => "Emoji id {$id} does not exist"));
            }
        }
    } else {
        echo json_encode(array('status' => false, 'message' => 'Please provide a valid token'));
    }
});