-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ccd60ab
commit c2ad5c3
Showing
7 changed files
with
284 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Update Address Record with REST API</title> | ||
<link rel="stylesheet" href="/static/style.css"> | ||
</head> | ||
<body> | ||
<h1>Address Data from API</h1> | ||
<div id="data-container"></div> | ||
|
||
<h2>Update Address Data</h2> | ||
<form id="insert-form"> | ||
<label for="id">Id:</label> | ||
<input type="text" id="id" name="id"> | ||
<label for="city">City:</label> | ||
<input type="text" id="city" name="city"> | ||
<label for="street">Street:</label> | ||
<input type="text" id="street" name="street"> | ||
<input type="submit" value="Submit"> | ||
</form> | ||
|
||
<h4><a href="add">Add Data</a></h4> | ||
<h4><a href="update">Update Data</a></h4> | ||
<h4><a href="update-address">Add Address</a></h4> | ||
<h4><a href="delete">Delete Data</a></h4> | ||
|
||
<script> | ||
|
||
// Fetch data from the API and update the UI | ||
function fetchData() { | ||
fetch('/users') | ||
.then(response => response.json()) | ||
.then(data => { | ||
const container = document.getElementById('data-container'); | ||
container.innerHTML = ''; // Clear previous data | ||
|
||
data.forEach(item => { | ||
const userList = document.createElement('div'); | ||
const id = item.id; | ||
const addressList = document.createElement('ul'); | ||
fetch(`/users/${id}/address`) | ||
.then(response2 => response2.json()) | ||
.then(data2 => { | ||
addressList.textContent = JSON.stringify(data2); | ||
}); | ||
userList.textContent = JSON.stringify(item); | ||
container.appendChild(userList); | ||
container.appendChild(addressList); | ||
}); | ||
}) | ||
.catch(error => console.error('Error fetching data:', error)); | ||
} | ||
|
||
// Handle new data insertion form submission | ||
document.getElementById('insert-form').addEventListener('submit', function(event) { | ||
event.preventDefault(); | ||
|
||
const form = event.target; | ||
const formData = new FormData(form); | ||
const formDataObject = {}; | ||
let userId = formData.get('id'); // Get the user id from the form data | ||
formData.forEach((key, value) => { | ||
if (value !== 'id') { | ||
formDataObject[value] = key; | ||
} | ||
}); | ||
|
||
// Send POST request to insert new data | ||
fetch(`/users/${userId}/address`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify(formDataObject) | ||
}) | ||
.then(response => response.text()) | ||
.then(data => { | ||
console.log('Data inserted:', data); | ||
fetchData(); //refresh after adding | ||
form.reset(); //reset form fields to default values | ||
document.getElementById('name').focus(); //move focus to the first input field of the form | ||
}) | ||
.catch(error => console.error('Error inserting data:', error)); | ||
}); | ||
|
||
// Initial data fetch | ||
fetchData(); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Update Address Record with REST API</title> | ||
<link rel="stylesheet" href="/static/style.css"> | ||
</head> | ||
<body> | ||
<h1>Address Data from API</h1> | ||
<div id="data-container"></div> | ||
|
||
<h2>Update Address Data</h2> | ||
<form id="insert-form"> | ||
<label for="id">Id:</label> | ||
<input type="text" id="id" name="id"> | ||
<label for="city">City:</label> | ||
<input type="text" id="city" name="city"> | ||
<label for="street">Street:</label> | ||
<input type="text" id="street" name="street"> | ||
<input type="submit" value="Submit"> | ||
</form> | ||
|
||
<h4><a href="add">Add Data</a></h4> | ||
<h4><a href="update">Update Data</a></h4> | ||
<h4><a href="update-address">Add Address</a></h4> | ||
<h4><a href="delete">Delete Data</a></h4> | ||
|
||
<script> | ||
|
||
// Fetch data from the API and update the UI | ||
function fetchData() { | ||
fetch('/users') | ||
.then(response => response.json()) | ||
.then(data => { | ||
const container = document.getElementById('data-container'); | ||
container.innerHTML = ''; // Clear previous data | ||
|
||
data.forEach(item => { | ||
const userList = document.createElement('div'); | ||
const id = item.id; | ||
const addressList = document.createElement('ul'); | ||
fetch(`/users/${id}/address`) | ||
.then(response2 => response2.json()) | ||
.then(data2 => { | ||
addressList.textContent = JSON.stringify(data2); | ||
}); | ||
userList.textContent = JSON.stringify(item); | ||
container.appendChild(userList); | ||
container.appendChild(addressList); | ||
}); | ||
}) | ||
.catch(error => console.error('Error fetching data:', error)); | ||
} | ||
|
||
// Handle new data insertion form submission | ||
document.getElementById('insert-form').addEventListener('submit', function(event) { | ||
event.preventDefault(); | ||
|
||
const form = event.target; | ||
const formData = new FormData(form); | ||
const formDataObject = {}; | ||
let userId = formData.get('id'); // Get the user id from the form data | ||
formData.forEach((key, value) => { | ||
if (value !== 'id') { | ||
formDataObject[value] = key; | ||
} | ||
}); | ||
|
||
// Send POST request to insert new data | ||
fetch(`/users/${userId}/address`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify(formDataObject) | ||
}) | ||
.then(response => response.text()) | ||
.then(data => { | ||
console.log('Data inserted:', data); | ||
fetchData(); //refresh after adding | ||
form.reset(); //reset form fields to default values | ||
document.getElementById('name').focus(); //move focus to the first input field of the form | ||
}) | ||
.catch(error => console.error('Error inserting data:', error)); | ||
}); | ||
|
||
// Initial data fetch | ||
fetchData(); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters