$response->setJsonContent(array('status' => 'ERROR', 'messages' => $errors)); } return $response; }); // Updates products based on primary key $app->put('/api/products/{id:[0-9]+}', function ($id) use($app) { $product = $app->request->getJsonRawBody(); $phql = "UPDATE Products SET name = :name:, price = :price:, in_stock = :in_stock: WHERE id = :id:"; //table name $status = $app->modelsManager->executeQuery($phql, array('id' => $id, 'name' => $product->name, 'price' => $product->price, 'in_stock' => $product->in_stock)); // create a response $response = new Response(); // check if the update was successful if ($status->success() == true) { $response->setJsonContent(array('status' => 'OK')); } else { // change the http status $response->setStatusCode(409, "Conflict"); $errors = array(); foreach ($status->getMessages() as $message) { $errors[] = $message->getMessage(); } $response->setJsonContent(array('status' => 'ERROR', 'messages' => $errors)); } return $response; }); $app->notFound(function () use($app) { $app->response->setStatusCode(404, "Not Found")->sendHeaders(); echo '404 Page Not Found'; }); $app->handle();
} $response->setJsonContent(array('status' => 'ERROR', 'messages' => $errors)); } return $response; }); // Updates robots based on primary key $app->put('/api/robots/{id:[0-9]+}', function ($id) use($app) { $robot = $app->request->getJsonRawBody(); $phql = "UPDATE Robots SET name = :name:, type = :type:, year = :year: WHERE id = :id:"; $status = $app->modelsManager->executeQuery($phql, array('id' => $id, 'name' => $robot->name, 'type' => $robot->type, 'year' => $robot->year)); // Create a response $response = new Response(); // Check if the insertion was succesful if ($status->success() == true) { $response->setJsonContent(array('status' => 'OK')); } else { // Change the HTTP status $response->setStatusCode(409, "Conflict"); $errors = array(); foreach ($status->getMessage() as $message) { $errors[] = $message->getMessage(); } $response->setJsonContent(array('status' => 'ERROR', 'messages' => $errors)); } return $response; }); // Deletes robots based on primary key $app->delete('/api/robots/{id:[0-9]+}', function ($id) use($app) { $phql = "DELETE FROM Robots WHERE id = :id:"; $status = $app->modelsManager->executeQuery($phql, array('id' => $id)); // Create a response $response = new Response();
$errors = array(); foreach ($newSource->getMessages() as $message) { $errors[] = $message->getMessage(); } $app->response->setJsonContent(array('status' => 'ERROR', 'messages' => $errors))->send(); } }); // Edit existing source $app->put('/sources/{id}', function ($id) use($app) { $source = Tabs\Models\Sources::findById((string) $id); $put = $app->request->getJsonRawBody(); if ($source) { $source->setData($put->data); $source->setType($put->type); $source->setName($put->name); if ($source->save()) { $app->response->setStatusCode(200, 'OK'); $app->response->setJsonContent(array('status' => 'OK'))->send(); } } else { $app->response->setStatusCode(404, 'Not Found'); $app->response->setJsonContent(array('status' => 'Not Found'))->send(); } }); // Delete existing source $app->delete('/sources/{id}', function ($id) use($app) { $source = Tabs\Models\Sources::findById((string) $id); if ($source) { if ($source->delete()) { $app->response->setStatusCode(200, 'OK'); $app->response->setJsonContent(array('status' => 'OK'))->send(); }
<?php use Phalcon\Mvc\Micro; $app = new Micro(); // Retrieves all robots $app->get('/api/robots', function () { //echo 111; }); // 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()->getContent();
/********************************************************** * Updates **********************************************************/ /** * PUT /messages/$id/like */ $app->put('/api/messages/{id:[0-9]+}/like', function ($msg_id) { global $logged_in, $user_id, $err_login; $response = new Response(); if (!$logged_in) { $response->setStatusCode(403, 'Authentication error'); $response->setContentType('application/json'); $response->setJsonContent(array('status' => 'ERROR', 'messages' => array(is_null($err_login) ? "User not logged in" : $err_login))); return $response; } $new_value = like($user_id, $msg_id, 1); if ($new_value === false) { // error $response->setStatusCode(400, 'Error'); $response->setJsonContent(array('status' => 'ERROR', 'messages' => array(mysql_error()))); } else { $response->setJsonContent(array('value' => intval($new_value))); } $response->setContentType('application/json'); return $response; }); /** * DELETE /messages/$id/like */ $app->delete('/api/messages/{id:[0-9]+}/like', function ($msg_id) { global $logged_in, $user_id, $err_login; $response = new Response();
} $response->setJsonContent(array('status' => 'ERROR', 'messages' => $errors)); } return $response; }); // Updates Car based on primary key $app->put('/api/autos/{id:[0-9]+}', function ($id) use($app) { $car = $app->request->getJsonRawBody(); $phql = "UPDATE Cars SET Make = :make:, Model = :model:, Colour = :colour:, Engine = :engine:, Year = :year: WHERE id = :id:"; $status = $app->modelsManager->executeQuery($phql, array('id' => $id, 'make' => $car->make, 'model' => $car->model, 'colour' => $car->colour, 'engine' => $car->engine, 'year' => $car->year)); // Create a response $response = new Response(); // Check if the insertion was successful if ($status->success() == true) { $response->setJsonContent(array('status' => 'OK')); } else { // Change the HTTP status $response->setStatusCode(409, "Conflict"); $errors = array(); foreach ($status->getMessages() as $message) { $errors[] = $message->getMessage(); } $response->setJsonContent(array('status' => 'ERROR', 'messages' => $errors)); } return $response; }); // Deletes Car based on primary key $app->delete('/api/autos/{id:[0-9]+}', function ($id) use($app) { $phql = "DELETE FROM Cars WHERE id = :id:"; $status = $app->modelsManager->executeQuery($phql, array('id' => $id)); // Create a response $response = new Response();
$app->put('/feed/config', function () use($app, $response, $utils) { $code = 200; $responseText = 'ok'; $requestData = json_decode($app->request->getRawBody(), true); $data = array(); if (empty($requestData)) { $code = 400; $responseText = 'Bad Request'; $data['result'] = 'Data Error'; } else { try { //delete enlisting venue aliases $deleteVenueAliasSQL = "DELETE FROM venueAlias WHERE feed_id = :feed_id: AND venue_id = :venue_id:"; $insertVenueAliasSQL = "INSERT INTO venueAlias (feed_id,venue_id,name) VALUES(:feed_id:,:venue_id:,:venue_name:)"; $db = $app->getService('db'); foreach ($requestData as $row) { $aliases = $utils->index_set($row, 'aliases'); $venue_id = (int) $utils->index_set($row, 'venue_id'); $feed_id = (int) $utils->index_set($row, 'feed_id'); $priority = (int) $utils->index_set($row, 'priority'); if (empty($venue_id) || empty($feed_id)) { throw new Exception('Data Error'); } $existingVenueAlias = venueAlias::find(array('feed_id' => $feed_id, 'venue_id' => $venue_id)); if (count($existingVenueAlias) == 1) { $existingVenueAlias->delete(); } foreach ($aliases as $alias) { $app->modelsManager->executeQuery($insertVenueAliasSQL, array('feed_id' => $feed_id, 'venue_id' => $venue_id, 'venue_name' => trim($alias))); } //doing this in round about way since phalcon adpator does not support on duplicate key updates $existingFeedVenuePriority = feedVenuePriority::find(array('feed_id' => $feed_id, 'venue_id' => $venue_id)); if (count($existingFeedVenuePriority) == 1) { $updateSQL = "UPDATE feedVenuePriority set priority = :priority: WHERE venue_id =:venue_id: AND feed_id=:feed_id:"; $app->modelsManager->executeQuery($updateSQL, array('priority' => $priority, 'venue_id' => $venue_id, 'feed_id' => $feed_id)); } else { $insertSQL = "INSERT INTO feedVenuePriority (feed_id,venue_id,priority) VALUES(:feed_id:,:venue_id:,:priority:)"; $app->modelsManager->executeQuery($insertSQL, array('feed_id' => $feed_id, 'venue_id' => $venue_id, 'priority' => $priority)); } } $data = array('result' => 'successful'); } catch (Exception $e) { $code = 400; $responseText = 'Bad Request'; $data['result'] = $e->getMessage(); } } $response->send($code, $responseText, $data); });