diff --git a/API.REST.PHP/api-rest/create_client.php b/API.REST.PHP/api-rest/create_client.php index 8d0dbe4..25c1469 100644 --- a/API.REST.PHP/api-rest/create_client.php +++ b/API.REST.PHP/api-rest/create_client.php @@ -2,8 +2,8 @@ require_once('../includes/Client.class.php'); if($_SERVER['REQUEST_METHOD'] == 'POST' - && isset($_GET['email']) && isset($_GET['name']) && isset($_GET['city']) && isset($_GET['telephone'])){ - Client::create_client($_GET['email'], $_GET['name'], $_GET['city'], $_GET['telephone']); + && isset($_POST['email']) && isset($_POST['name']) && isset($_POST['city']) && isset($_POST['telephone'])){ + Client::create_client($_POST['email'], $_POST['name'], $_POST['city'], $_POST['telephone']); } ?> \ No newline at end of file diff --git a/API.REST.PHP/api-rest/get_all_client.php b/API.REST.PHP/api-rest/get_all_client.php index b4eb3db..6bcb686 100644 --- a/API.REST.PHP/api-rest/get_all_client.php +++ b/API.REST.PHP/api-rest/get_all_client.php @@ -1,8 +1,10 @@ \ No newline at end of file diff --git a/API.REST.PHP/css/styles.css b/API.REST.PHP/css/styles.css new file mode 100644 index 0000000..e881c55 --- /dev/null +++ b/API.REST.PHP/css/styles.css @@ -0,0 +1,70 @@ +body { + font-family: Arial, sans-serif; + background-color: #f4f4f4; + display: flex; + justify-content: center; + align-items: center; + height: 100vh; + margin: 0; +} + +.container { + background-color: #fff; + padding: 20px; + border-radius: 8px; + box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); +} + +h2 { + margin-bottom: 20px; +} + +form { + display: flex; + flex-direction: column; +} + +label { + margin-bottom: 5px; +} + +input { + margin-bottom: 15px; + padding: 10px; + border: 1px solid #ccc; + border-radius: 4px; +} + +button { + padding: 10px; + background-color: #007BFF; + color: #fff; + border: none; + border-radius: 4px; + cursor: pointer; +} + +button:hover { + background-color: #0056b3; +} + +table { + width: 100%; + border-collapse: collapse; + margin-top: 20px; +} + +th, td { + border: 1px solid #ddd; + padding: 8px; + text-align: left; +} + +th { + background-color: #f2f2f2; +} + +#message { + margin-top: 20px; + font-size: 1.2em; +} \ No newline at end of file diff --git a/API.REST.PHP/html/clients.html b/API.REST.PHP/html/clients.html new file mode 100644 index 0000000..5e11b2b --- /dev/null +++ b/API.REST.PHP/html/clients.html @@ -0,0 +1,30 @@ + + + + + + Client List + + + +
+

Client List

+ + + + + + + + + + + + + +
IDEmailNameCityTelephone
+

+
+ + + \ No newline at end of file diff --git a/API.REST.PHP/html/create.html b/API.REST.PHP/html/create.html new file mode 100644 index 0000000..7f1372d --- /dev/null +++ b/API.REST.PHP/html/create.html @@ -0,0 +1,30 @@ + + + + + + Create Client + + + +
+

Create New Client

+
+ + + + + + + + + + + + + +
+
+ + + \ No newline at end of file diff --git a/API.REST.PHP/html/delete.html b/API.REST.PHP/html/delete.html new file mode 100644 index 0000000..23702f5 --- /dev/null +++ b/API.REST.PHP/html/delete.html @@ -0,0 +1,18 @@ + + + + + + Delete Client + + + +
+

Delete Client

+ + +

+
+ + + \ No newline at end of file diff --git a/API.REST.PHP/html/update.html b/API.REST.PHP/html/update.html new file mode 100644 index 0000000..fd7637c --- /dev/null +++ b/API.REST.PHP/html/update.html @@ -0,0 +1,34 @@ + + + + + + Update Client + + + +
+

Update Client

+
+ + + + + + + + + + + + + + + + +
+

+
+ + + diff --git a/API.REST.PHP/includes/Client.class.php b/API.REST.PHP/includes/Client.class.php index 17301dd..c6d59c2 100644 --- a/API.REST.PHP/includes/Client.class.php +++ b/API.REST.PHP/includes/Client.class.php @@ -38,13 +38,16 @@ public static function get_all_clients(){ $conn = $database->getConnection(); $stmt = $conn->prepare('SELECT * FROM listado_clientes'); if($stmt->execute()){ - $result = $stmt->fetchAll(); + $result = $stmt->fetchAll(PDO::FETCH_ASSOC); + header('Content-Type: application/json'); echo json_encode($result); - header('HTTP/1.1 201 OK'); + header('HTTP/1.1 200 OK'); } else { header('HTTP/1.1 404 No se ha podido consultar los clientes'); + echo json_encode(['error' => 'No se ha podido consultar los clientes']); } - } + exit(); + } public static function update_client($id, $email, $name, $city, $telephone){ $database = new Database(); diff --git a/API.REST.PHP/js/create-client/script.js b/API.REST.PHP/js/create-client/script.js new file mode 100644 index 0000000..f0c31b6 --- /dev/null +++ b/API.REST.PHP/js/create-client/script.js @@ -0,0 +1,29 @@ +document.getElementById('clientForm').addEventListener('submit', function(event) { + event.preventDefault(); + + const formData = new FormData(event.target); + + const params = new URLSearchParams(formData); + + fetch('../api-rest/create_client.php?' + params.toString(), { + method: 'POST', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + body: params.toString() + }) + .then(response => { + if (response.status === 201) { + alert('Cliente creado correctamente'); + event.target.reset(); + } else if (response.status === 404) { + alert('Error: Cliente no se ha creado correctamente'); + } else { + alert('Error desconocido'); + } + }) + .catch(error => { + console.error('Error:', error); + alert('Error de red. Por favor, inténtalo de nuevo más tarde.'); + }); +}); diff --git a/API.REST.PHP/js/delete-client/script.js b/API.REST.PHP/js/delete-client/script.js new file mode 100644 index 0000000..34e0d02 --- /dev/null +++ b/API.REST.PHP/js/delete-client/script.js @@ -0,0 +1,25 @@ +function deleteClient() { + const clientId = document.getElementById('clientId').value; + const message = document.getElementById('message'); + + fetch(`../api-rest/delete_client.php?id=${clientId}`, { + method: 'DELETE', + }) + .then(response => { + if (response.status === 201) { + message.textContent = 'Cliente borrado correctamente'; + message.style.color = 'green'; + } else if (response.status === 404) { + message.textContent = 'Error: Cliente no se ha podido borrar correctamente'; + message.style.color = 'red'; + } else { + message.textContent = 'Error desconocido'; + message.style.color = 'red'; + } + }) + .catch(error => { + console.error('Error:', error); + message.textContent = 'Error de red. Por favor, inténtalo de nuevo más tarde.'; + message.style.color = 'red'; + }); +} \ No newline at end of file diff --git a/API.REST.PHP/js/get-all-client/script.js b/API.REST.PHP/js/get-all-client/script.js new file mode 100644 index 0000000..ec40534 --- /dev/null +++ b/API.REST.PHP/js/get-all-client/script.js @@ -0,0 +1,45 @@ +function fetchClients() { + const message = document.getElementById('message'); + const tbody = document.querySelector('#clientTable tbody'); + tbody.innerHTML = ''; + + fetch('../api-rest/get_all_client.php') + .then(response => { + console.log(response); + if (response.status === 200) { + return response.json(); + } else if (response.status === 404) { + message.textContent = 'Error: No se ha podido consultar los clientes'; + message.style.color = 'red'; + return Promise.reject('Client fetch failed'); + } else { + message.textContent = 'Error desconocido'; + message.style.color = 'red'; + return Promise.reject('Unknown error'); + } + }) + .then(data => { + if (data.error) { + message.textContent = `Error: ${data.error}`; + message.style.color = 'red'; + } else { + data.forEach(client => { + const row = document.createElement('tr'); + row.innerHTML = ` + ${client.id} + ${client.email} + ${client.name} + ${client.city} + ${client.telephone} + `; + tbody.appendChild(row); + }); + message.textContent = ''; + } + }) + .catch(error => { + console.error('Error:', error); + message.textContent = 'Error de red. Por favor, inténtalo de nuevo más tarde.'; + message.style.color = 'red'; + }); +} diff --git a/API.REST.PHP/js/update-client/script.js b/API.REST.PHP/js/update-client/script.js new file mode 100644 index 0000000..5a5af81 --- /dev/null +++ b/API.REST.PHP/js/update-client/script.js @@ -0,0 +1,34 @@ +document.getElementById('updateClientForm').addEventListener('submit', function(event) { + event.preventDefault(); + + const formData = new FormData(event.target); + + const params = new URLSearchParams(formData); + + // Make an AJAX request to the PHP API + fetch(`../api-rest/update_client.php?${params.toString()}`, { + method: 'PUT', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + body: params.toString() + }) + .then(response => { + if (response.status === 201) { + document.getElementById('message').textContent = 'Cliente actualizado correctamente'; + document.getElementById('message').style.color = 'green'; + event.target.reset(); + } else if (response.status === 404) { + document.getElementById('message').textContent = 'Error: Cliente no se ha podido actualizar correctamente'; + document.getElementById('message').style.color = 'red'; + } else { + document.getElementById('message').textContent = 'Error desconocido'; + document.getElementById('message').style.color = 'red'; + } + }) + .catch(error => { + console.error('Error:', error); + document.getElementById('message').textContent = 'Error de red. Por favor, inténtalo de nuevo más tarde.'; + document.getElementById('message').style.color = 'red'; + }); +});