foreach ($status->getMessages() as $errorMessage) { $errors[] = $errorMessage->getMessage(); } $response->setJsonContent(array('status' => 'ERROR', 'messages' => $errors)); } return $response; }); // Exit the chat, making the user 'inactive'. Chat data is anyway saved in the database. $app->put('/users/{username}/exit', function ($id) use($app) { $phql = "UPDATE Users \n \t\tSET active = :active:\n \t\tWHERE id = :id:"; $status = $app->modelsManager->executeQuery($phql, array('active' => 0, 'id' => $id)); $response = new Phalcon\Http\Response(); if ($status->success() == true) { $response->setJsonContent(array('status' => "OK")); } else { $response->setStatusCode(400, "Conflict"); $errors = array(); foreach ($status->getMessages() as $message) { $errors[] = $message->getMessage(); } $response->setJsonContent(array('status' => 'ERROR', 'messages' => $errors)); } return $response; }); // Send message to a selected user. $app->post('/chat/{sender}/{receiver}', function ($sender, $receiver) use($app) { $message = $app->request->getJsonRawBody(); $phql = "INSERT INTO Messages (sender_id, receiver_id, message_content)\n \t\tVALUES (:sender_id:, :receiver_id:, :message_content:)"; $status = $app->modelsManager->executeQuery($phql, array('sender_id' => $sender, 'receiver_id' => $receiver, 'message_content' => $message->message)); $response = new Phalcon\Http\Response(); if ($status->success() == true) { $phql = "SELECT * \n \t\t\tFROM Messages \n \t\t\tWHERE message_id = :message_id:";
return 'Powered By Phalcon API'; }); $app->post('/ajax/recover', function () use($app) { $mongo = $app->getDI()->getShared('mongo'); $json = $app->request->getJsonRawBody(); $user = $mongo->users->find(array('email' => $json->email), array('email' => 1)); $user = iterator_to_array($user, false); if (count($user) > 0) { $app->response->setJsonContent(array('success' => true)); } else { $app->response->setJsonContent(array('success' => false, 'message' => "Email doesn't exist.")); } $app->response->send(); }); $app->put('/ajax/recover/change', function () use($app) { $mongo = $app->getDI()->getShared('mongo'); }); /* Embedly */ $app->get('/ajax/token', function () use($app) { session_start(); $tokenKey = $app->security->getTokenKey(); $token = $app->security->getToken(); session_write_close(); $app->response->setJsonContent(array('tokens' => array('key' => $tokenKey, 'token' => $token))); $app->response->send(); }); $app->post('/ajax/embed', function () use($app) { //if($app->security->checkToken($tokenKey, $tokenVal)) //{ $source = $app->request->getJsonRawBody(); if (filter_var($source->url, FILTER_VALIDATE_URL) === false) {
$name = $request->name; $short = $request->shortDescription; $long = $request->longDescription; $day = $request->day; $phql = "INSERT INTO Tasks(name, short_description, long_description, day, started, completed) VALUES(:name:, :short:, :long:, :day:, null, null)"; $status = $app->modelsManager->executeQuery($phql, ['name' => $name, 'short' => $short, 'long' => $long, 'day' => $day]); $response = new \Phalcon\Http\Response(); if ($status->success()) { $response->setJsonContent(['status' => 'OK']); } }); $app->put('/tasks/{id:[0-9]+}', function ($id) use($app) { $task = $app->request->getJsonRawBody()->task; $phql = "UPDATE Tasks SET name = :name:, short_description = :short:, long_description = :long:, started = :start:, completed = :complete: WHERE id = :id:"; $status = $app->modelsManager->executeQuery($phql, ['name' => $task->name, 'short' => $task->shortDescription, 'long' => $task->longDescription, 'start' => $task->started, 'complete' => $task->completed, 'id' => $id]); $response = new \Phalcon\Http\Response(); if ($status->success()) { $response->setJsonContent(['status' => 'OK']); } }); $app->delete('/tasks/{id:[0-9]+}', function ($id) use($app) { $status = $app->modelsManager->executeQuery('DELETE FROM Tasks WHERE id = :id:', ['id' => $id]); $response = new \Phalcon\Http\Response(); if ($status->success()) { $response->setJsonContent(['status' => 'OK']); } return $response; }); /** * Ideas */
<?php $app = new Phalcon\Mvc\Micro(); //Retrieves all robots $app->get('/api/robots', function () { }); //Searches for robots with $name in their name $app->get('/api/robots/search/{name}', function ($name) { }); //Retrieves robots based on primary key $app->get('/api/robots/{id:[0-9]+}', function ($id) { }); //Adds a new robot $app->post('/api/robots', function () { }); //Updates robots based on primary key $app->put('/api/robots/{id:[0-9]+}', function () { }); //Deletes robots based on primary key $app->delete('/api/robots/{id:[0-9]+}', function () { }); $app->handle();