├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── actualizar_videojuego.php ├── cors.php ├── eliminar_videojuego.php ├── env.ejemplo.php ├── esquema.sql ├── funciones.php ├── guardar_videojuego.php ├── obtener_videojuego.php └── obtener_videojuegos.php /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | env.php -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Luis Cabrera Benito 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # api-php-react 2 | API en PHP para cliente React 3 | -------------------------------------------------------------------------------- /actualizar_videojuego.php: -------------------------------------------------------------------------------- 1 | 29 | 29 | 29 | 2 | MYSQL_DATABASE_NAME = "videojuegos_react" 3 | MYSQL_USER = "root" 4 | MYSQL_PASSWORD = "" -------------------------------------------------------------------------------- /esquema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE IF NOT EXISTS videojuegos( 2 | id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, 3 | nombre VARCHAR(255) NOT NULL, 4 | precio DECIMAL(9,2) NOT NULL, 5 | calificacion TINYINT NOT NULL 6 | ); -------------------------------------------------------------------------------- /funciones.php: -------------------------------------------------------------------------------- 1 | 29 | prepare("DELETE FROM videojuegos WHERE id = ?"); 35 | return $sentencia->execute([$id]); 36 | } 37 | 38 | function actualizarVideojuego($videojuego) 39 | { 40 | $bd = obtenerConexion(); 41 | $sentencia = $bd->prepare("UPDATE videojuegos SET nombre = ?, precio = ?, calificacion = ? WHERE id = ?"); 42 | return $sentencia->execute([$videojuego->nombre, $videojuego->precio, $videojuego->calificacion, $videojuego->id]); 43 | } 44 | 45 | function obtenerVideojuegoPorId($id) 46 | { 47 | $bd = obtenerConexion(); 48 | $sentencia = $bd->prepare("SELECT id, nombre, precio, calificacion FROM videojuegos WHERE id = ?"); 49 | $sentencia->execute([$id]); 50 | return $sentencia->fetchObject(); 51 | } 52 | 53 | function obtenerVideojuegos() 54 | { 55 | $bd = obtenerConexion(); 56 | $sentencia = $bd->query("SELECT id, nombre, precio, calificacion FROM videojuegos"); 57 | return $sentencia->fetchAll(); 58 | } 59 | 60 | function guardarVideojuego($videojuego) 61 | { 62 | $bd = obtenerConexion(); 63 | $sentencia = $bd->prepare("INSERT INTO videojuegos(nombre, precio, calificacion) VALUES (?, ?, ?)"); 64 | return $sentencia->execute([$videojuego->nombre, $videojuego->precio, $videojuego->calificacion]); 65 | } 66 | 67 | function obtenerVariableDelEntorno($key) 68 | { 69 | if (defined("_ENV_CACHE")) { 70 | $vars = _ENV_CACHE; 71 | } else { 72 | $file = "env.php"; 73 | if (!file_exists($file)) { 74 | throw new Exception("El archivo de las variables de entorno ($file) no existe. Favor de crearlo"); 75 | } 76 | $vars = parse_ini_file($file); 77 | define("_ENV_CACHE", $vars); 78 | } 79 | if (isset($vars[$key])) { 80 | return $vars[$key]; 81 | } else { 82 | throw new Exception("La clave especificada (" . $key . ") no existe en el archivo de las variables de entorno"); 83 | } 84 | } 85 | function obtenerConexion() 86 | { 87 | $password = obtenerVariableDelEntorno("MYSQL_PASSWORD"); 88 | $user = obtenerVariableDelEntorno("MYSQL_USER"); 89 | $dbName = obtenerVariableDelEntorno("MYSQL_DATABASE_NAME"); 90 | $database = new PDO('mysql:host=localhost;dbname=' . $dbName, $user, $password); 91 | $database->query("set names utf8;"); 92 | $database->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE); 93 | $database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 94 | $database->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); 95 | return $database; 96 | } 97 | -------------------------------------------------------------------------------- /guardar_videojuego.php: -------------------------------------------------------------------------------- 1 | 29 | 29 | 29 |