PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/api/index.php

https://bitbucket.org/aventer/cellar
PHP | 153 lines | 127 code | 15 blank | 11 comment | 0 complexity | fd16766c712f3b5c3467e67ec49af53b MD5 | raw file
  1. <?php
  2. require 'Slim/Slim.php';
  3. require 'php-activerecord/ActiveRecord.php';
  4. require 'utilities/Utilities.php';
  5. ActiveRecord\Config::initialize(function($cfg)
  6. {
  7. $cfg->set_model_directory('models');
  8. $cfg->set_connections
  9. ( array
  10. (
  11. 'development' => 'mysql://root:root@localhost/cellar'
  12. )
  13. );
  14. });
  15. $app = new Slim();
  16. $app->get('/wines', 'getWines');
  17. $app->get('/wines/:id', 'getWine');
  18. $app->get('/wines/search/:query', 'findByName');
  19. $app->post('/wines', 'addWine');
  20. $app->put('/wines/:id', 'updateWine');
  21. $app->delete('/wines/:id', 'deleteWine');
  22. $app->run();
  23. function getWines() {
  24. try {
  25. $utility = new Utilities();
  26. $dataObjects = Wine::find('all');
  27. echo '[{"wine": ' . json_encode($utility->convertMultiplePhpActiveRecordObjectsToArray($dataObjects)) . '}]'; // sending an array back to match both origional and CanJS code
  28. //echo json_encode($utility->convertMultiplePhpActiveRecordObjectsToArray($dataObjects));
  29. } catch (\ActiveRecord\RecordNotFound $e) {
  30. echo '{"error":{"text":'. $e->getMessage() .'}}';
  31. }
  32. /* $sql = "select * FROM wine ORDER BY name";
  33. try {
  34. $db = getConnection();
  35. $stmt = $db->query($sql);
  36. $wines = $stmt->fetchAll(PDO::FETCH_OBJ);
  37. $db = null;
  38. echo '{"wine": ' . json_encode($wines) . '}';
  39. } catch(PDOException $e) {
  40. echo '{"error":{"text":'. $e->getMessage() .'}}';
  41. }*/
  42. }
  43. function getWine($id) {
  44. $sql = "SELECT * FROM wine WHERE id=:id";
  45. try {
  46. $db = getConnection();
  47. $stmt = $db->prepare($sql);
  48. $stmt->bindParam("id", $id);
  49. $stmt->execute();
  50. $wine = $stmt->fetchObject();
  51. $db = null;
  52. echo json_encode($wine);
  53. } catch(PDOException $e) {
  54. echo '{"error":{"text":'. $e->getMessage() .'}}';
  55. }
  56. }
  57. function addWine() {
  58. error_log('addWine\n', 3, '/var/tmp/php.log');
  59. $request = Slim::getInstance()->request();
  60. $wine = json_decode($request->getBody());
  61. $sql = "INSERT INTO wine (name, grapes, country, region, year, description) VALUES (:name, :grapes, :country, :region, :year, :description)";
  62. try {
  63. $db = getConnection();
  64. $stmt = $db->prepare($sql);
  65. $stmt->bindParam("name", $wine->name);
  66. $stmt->bindParam("grapes", $wine->grapes);
  67. $stmt->bindParam("country", $wine->country);
  68. $stmt->bindParam("region", $wine->region);
  69. $stmt->bindParam("year", $wine->year);
  70. $stmt->bindParam("description", $wine->description);
  71. $stmt->execute();
  72. $wine->id = $db->lastInsertId();
  73. $db = null;
  74. echo json_encode($wine);
  75. } catch(PDOException $e) {
  76. error_log($e->getMessage(), 3, '/var/tmp/php.log');
  77. echo '{"error":{"text":'. $e->getMessage() .'}}';
  78. }
  79. }
  80. function updateWine($id) {
  81. $request = Slim::getInstance()->request();
  82. $body = $request->getBody();
  83. $wine = json_decode($body);
  84. $sql = "UPDATE wine SET name=:name, grapes=:grapes, country=:country, region=:region, year=:year, description=:description WHERE id=:id";
  85. try {
  86. $db = getConnection();
  87. $stmt = $db->prepare($sql);
  88. $stmt->bindParam("name", $wine->name);
  89. $stmt->bindParam("grapes", $wine->grapes);
  90. $stmt->bindParam("country", $wine->country);
  91. $stmt->bindParam("region", $wine->region);
  92. $stmt->bindParam("year", $wine->year);
  93. $stmt->bindParam("description", $wine->description);
  94. $stmt->bindParam("id", $id);
  95. $stmt->execute();
  96. $db = null;
  97. echo json_encode($wine);
  98. } catch(PDOException $e) {
  99. echo '{"error":{"text":'. $e->getMessage() .'}}';
  100. }
  101. }
  102. function deleteWine($id) {
  103. $sql = "DELETE FROM wine WHERE id=:id";
  104. try {
  105. $db = getConnection();
  106. $stmt = $db->prepare($sql);
  107. $stmt->bindParam("id", $id);
  108. $stmt->execute();
  109. $db = null;
  110. } catch(PDOException $e) {
  111. echo '{"error":{"text":'. $e->getMessage() .'}}';
  112. }
  113. }
  114. function findByName($query) {
  115. $sql = "SELECT * FROM wine WHERE UPPER(name) LIKE :query ORDER BY name";
  116. try {
  117. $db = getConnection();
  118. $stmt = $db->prepare($sql);
  119. $query = "%".$query."%";
  120. $stmt->bindParam("query", $query);
  121. $stmt->execute();
  122. $wines = $stmt->fetchAll(PDO::FETCH_OBJ);
  123. $db = null;
  124. echo '{"wine": ' . json_encode($wines) . '}';
  125. } catch(PDOException $e) {
  126. echo '{"error":{"text":'. $e->getMessage() .'}}';
  127. }
  128. }
  129. function getConnection() {
  130. $dbhost="localhost";
  131. $dbuser="root";
  132. $dbpass="root";
  133. $dbname="cellar";
  134. $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
  135. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  136. return $dbh;
  137. }
  138. ?>