Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- header('Access-Control-Allow-Origin: *');
- require 'Slim/Slim.php';
- \Slim\Slim::registerAutoloader();
- $app = new \Slim\Slim();
- $app->get('/cursos', 'getCursos');
- $app->get('/cursos/:id', 'getCurso');
- $app->post('/cursos', 'insertar');
- $app->put('/cursos', 'actualizar');
- $app->delete('/cursos/:id', 'eliminar');
- $app->run();
- function getCursos() {
- $sql = "select * FROM curso ORDER BY nombre";
- try {
- $db = getConnection();
- $stmt = $db->query($sql);
- $wines = $stmt->fetchAll(PDO::FETCH_OBJ);
- $db = null;
- echo '{"cursos":' . json_encode($wines) . '}';
- } catch(PDOException $e) {
- echo '{"error":{"text":'. $e->getMessage() .'}}';
- }
- }
- function actualizar() {
- $app = \Slim\Slim::getInstance();
- $curso = json_decode($app->request()->getBody());
- $sql = "UPDATE curso SET nombre=:nombre, descripcion=:descripcion, imagen=:imagen, duracion=:duracion WHERE idcurso = :idcurso";
- $db = getConnection();
- $stmt = $db->prepare($sql);
- $stmt->bindParam("idcurso", $curso->idcurso);
- $stmt->bindParam("nombre", $curso->nombre);
- $stmt->bindParam("descripcion", $curso->descripcion);
- $stmt->bindParam("imagen", $curso->imagen);
- $stmt->bindParam("duracion", $curso->duracion);
- $stmt->execute();
- $sql = "SELECT * FROM curso WHERE idcurso= :idcurso";
- $stmt = $db->prepare($sql);
- $stmt->bindParam("idcurso", $curso->idcurso);
- $stmt->execute();
- $curso = $stmt->fetchObject();
- echo json_encode($curso);
- }
- function insertar() {
- $app = \Slim\Slim::getInstance();
- $curso = json_decode($app->request()->getBody());
- $sql = "INSERT INTO curso(nombre, descripcion,imagen,duracion) VALUES(:nombre,:descripcion,:imagen,:duracion)";
- $db = getConnection();
- $stmt = $db->prepare($sql);
- $stmt->bindParam("nombre", $curso->nombre);
- $stmt->bindParam("descripcion", $curso->descripcion);
- $stmt->bindParam("imagen", $curso->imagen);
- $stmt->bindParam("duracion", $curso->duracion);
- $stmt->execute();
- $sql = "SELECT * FROM curso WHERE idcurso= (SELECT MAX(idCurso) FROM curso)";
- $stmt = $db->prepare($sql);
- $stmt->execute();
- $curso = $stmt->fetchObject();
- echo json_encode($curso);
- }
- function eliminar($id) {
- $sql = "DELETE FROM curso WHERE idcurso=:id";
- $db = getConnection();
- $stmt = $db->prepare($sql);
- $stmt->bindParam("id", $id);
- $stmt->execute();
- echo 'OK';
- }
- function getCurso($id) {
- $sql = "SELECT * FROM curso WHERE idcurso=:id";
- try {
- $db = getConnection();
- $stmt = $db->prepare($sql);
- $stmt->bindParam("id", $id);
- $stmt->execute();
- $wine = $stmt->fetchObject();
- $db = null;
- echo json_encode($wine);
- } catch(PDOException $e) {
- echo '{"error":{"text":'. $e->getMessage() .'}}';
- }
- }
- function getConnection() {
- $dbhost="tgrest.db.10810290.hostedresource.com";
- $dbuser="tgrest";
- $dbpass="TecFest#2015";
- $dbname="tgrest";
- $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
- $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- return $dbh;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement