function getAllProducts() { include_once "products.php"; $product = new products(); if (!$product->getProducts()) { echo '{"result":0,"message":"Could not get products"}'; return; } $row = $product->fetchArray(); echo '{"result":1,"products":['; while ($row) { echo json_encode($row); if ($row = $product->fetchArray()) { echo ','; } } echo ']}'; }
$app->delete('/products/:id', 'authenticate', function ($idProduct) use($app) { global $user_id; $response = array(); $db = new products(); $res = $db->delete($idProduct); if ($res != NULL) { echo json_encode(array('error' => false, 'message' => 'Producto eliminado satisfactoriamente')); } else { echo json_encode(array('error' => true, 'message' => 'Error en el borrado del producto. Por favor intente de nuevo')); } }); $app->get('/products', 'authenticate', function () { global $user_id; $response = array(); $db = new products(); $result = $db->getProducts(); echo json_encode($result); }); $app->get('/products/:id', 'authenticate', function ($idProduct) use($app) { global $user_id; $response = array(); $start = $app->request()->params('start'); $end = $app->request()->params('end'); $db = new products(); $result = $db->getHistoryProducts($idProduct, $start, $end); if ($result != NULL) { echo json_encode(array('id' => $idProduct, 'name' => $db->getNameById($idProduct)['name'], 'prices' => $result)); } else { echo json_encode(array('error' => true, 'message' => 'Error en la obtencion del producto. Por favor intente de nuevo')); } });