Skip to content

Instantly share code, notes, and snippets.

@alphanetEX
Last active Jul 12, 2017
Embed
What would you like to do?
Codigo Legacy
<?php
$app->get('/producto', function(Request $request, Response $response){
$consulta = "SELECT * FROM productos ORDER BY id_producto DESC;";
try{
$db = new db();
$db = $db->conectar();
$query = $db->query($consulta);\
// el arraid genera unos corchetes demas generando un conflicto a la hora de agregarlo al componente de angular
$productos = array();
while ($fila = $query->fetchAll(PDO::FETCH_OBJ)) {
$productos[] = $fila;
}
$db = null;
$result = array("status" => "success",
"data" => $productos);
echo json_encode($result);
}catch(PDOException $e){
echo '{"error": {"text": '.$e->getMessage().'}';
}
});
$app->get("/random-productos", function() use($db, $app) {
// sleep(3);
$query = $db->query("SELECT * FROM productos ORDER BY RAND() LIMIT 1;");
$productos = $query->fetch_assoc();
if ($query->num_rows == 1) {
$result = array("status" => "success",
"data" => $productos);
} else {
$result = array(
"status" => "error",
"message" => "El productos no existe");
}
echo json_encode($result);
});
$app->post("/productos", function() use($db, $app) {
$json = $app->request->post("json");
$query = "INSERT INTO productos VALUES(NULL,"
. "'{$app->request->post("nombre")}',"
. "'{$app->request->post("precio")}', "
. "'{$app->request->post("descripcion")}'"
. ")";
$insert = $db->query($query);
if ($insert) {
$result = array("status" => "success",
"message" => "Restaurante creado correctamente!!!");
} else {
$result = array("status" => "error", "message" => "Restaurante NO SE HA creado!!!");
}
echo json_encode($result);
});
//PUT para ACTUALIZAR
$app->put('/productos/:id', function ($id) use($db, $app) {
$request = $app->request;
$sql = "UPDATE productos SET
nombre ='{$request->params("nombre")}',
precio ='{$request->params("precio")}',
vlan ='{$request->params("vlan")}',
test ='{$request->params("test")}'
WHERE id_producto=$id";
$update = $db->query($sql);
if ($update) {
$result = array("status" => "true", "message" => "Usuario modificado correctamente");
} else {
$result = array("status" => "false", "message" => "Usuario NO modificado");
}
echo json_encode($result);
});
$app->get("/delete-producto/:id", function($id) use($db, $app) {
$query = "DELETE FROM productos WHERE id_producto = {$id}";
$delete = $db->query($query);
if ($delete) {
$result = array("status" => "success", "message" => "El producto se ha borrado correctamente!!!");
} else {
$result = array("status" => "error", "message" => "El producto NO SE HA borrado!!!");
}
echo json_encode($result);
});
$app->post("/upload-file", function() use($db, $app) {
$result = array("status" => "error", "message" => "The file could not be uploaded");
if (isset($_FILES["uploads"])) {
$piramideUploader = new PiramideUploader();
$upload = $piramideUploader->upload("image", "uploads", "uploads", array("image/jpeg", "image/png", "image/gif"));
$file = $piramideUploader->getInfoFile();
$file_name = $file["complete_name"];
if (isset($upload) && $upload["uploaded"] == false) {
$result = array("status" => "error",
"message" => $upload["error"]);
} else {
$result = array("status" => "success",
"message" => "Fichero subido correctamente",
"filename"=>$file_name);
}
}
echo json_encode($result);
});
>?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment