diff --git a/README.md b/README.md index e4e7bb0..8e76ae6 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ It told me to install Flask (Python framework) and gave me app.py & index.html & 1. Fire up the crud-app docker compose stack, from its directory, `docker compose up -d` 2. from this project's directory, run `python3 app.py` -3. in your browser, open the page: [http://localhost:8000](http:localhost:8000) +3. in your browser, open the page: [http://localhost:8080](http:localhost:8080) 4. you should see the inital data, and be able to create, update or delete any of the records ## Misconceptions diff --git a/app.py b/app.py index a0dcf51..cceced7 100644 --- a/app.py +++ b/app.py @@ -18,6 +18,14 @@ def update(): def create(): return render_template('add.html') +@app.route('/update-address') +def update_address(): + return render_template('update-address.html') + +@app.route('/add-address') +def create_address(): + return render_template('add-address.html') + @app.route('/delete') def delete(): return render_template('delete.html') @@ -37,6 +45,17 @@ def get_data(): # Handle errors or unsuccessful responses return jsonify({'error': 'Failed to fetch data from API'}), response.status_code +@app.route('/users//address', methods=['GET']) +def get_address(data_id): + api_url = f'https://api.demojoyto.win/users/{data_id}/address' + response = requests.get(api_url) + if response.status_code == 200: + return response.json() + else: + # Handle errors or unsuccessful responses + return jsonify({'error': 'Failed to fetch data from API'}), response.status_code + + # Define the route for updating data @app.route('/users/', methods=['PUT']) def update_data(data_id): @@ -57,6 +76,26 @@ def update_data(data_id): # Handle errors or unsuccessful responses return jsonify({'error': 'Failed to update user data'}), response.status_code +# Define another route for updating data +@app.route('/users', methods=['PUT']) +def update_data2(): + + # URL of the RESTful API endpoint for creating a new user + api_url = 'https://api.demojoyto.win/users' + + # Extract the data to be created from the incoming request + user_data = request.json + + # Make a PUT request to the RESTful API + response = requests.put(api_url, json=user_data) + + # Check if the request was successful + if response.status_code == 201: + # Return the created user data response + return response.text + else: + # Handle errors or unsuccessful responses + return jsonify({'error': 'Failed to update user'}), response.status_code # Define the route for creating data @app.route('/users', methods=['POST']) @@ -79,6 +118,48 @@ def create_data(): # Handle errors or unsuccessful responses return jsonify({'error': 'Failed to create new user'}), response.status_code +# Define the route for creating address data +@app.route('/users//address', methods=['POST']) +def create_address_data(data_id): + + # URL of the RESTful API endpoint for creating a new user + api_url = f'https://api.demojoyto.win/users/{data_id}/address' + + # Extract the data to be created from the incoming request + address_data = request.json + + # Make a POST request to the RESTful API + response = requests.post(api_url, json=address_data) + + # Check if the request was successful + if response.status_code == 201: + # Return the created address data response + return response.text + else: + # Handle errors or unsuccessful responses + return jsonify({'error': 'Failed to create new address'}), response.status_code + +# Define the route for updating address data +@app.route('/users//address', methods=['PUT']) +def update_address_data(data_id): + + # URL of the RESTful API endpoint for creating a new user + api_url = f'https://api.demojoyto.win/users/{data_id}/address' + + # Extract the data to be created from the incoming request + address_data = request.json + + # Make a PUT request to the RESTful API + response = requests.put(api_url, json=address_data) + + # Check if the request was successful + if response.status_code == 204: + # Return the created address data response + return response.text + else: + # Handle errors or unsuccessful responses + return jsonify({'error': 'Failed to update address'}), response.status_code + # Define the route for deleting data @app.route('/users/', methods=['DELETE']) def delete_data(data_id): diff --git a/templates/add-address.html b/templates/add-address.html new file mode 100644 index 0000000..c285abe --- /dev/null +++ b/templates/add-address.html @@ -0,0 +1,92 @@ + + + + + + Update Address Record with REST API + + + +

Address Data from API

+
+ +

Update Address Data

+
+ + + + + + + +
+ +

Add Data

+

Update Data

+

Add Address

+

Delete Data

+ + + + diff --git a/templates/add.html b/templates/add.html index 9e3ae29..8ff07cc 100644 --- a/templates/add.html +++ b/templates/add.html @@ -12,14 +12,20 @@

Data from API

Add Data

+ + + +

Update Data

+

Update Address

+

Add Address

Delete Data

- \ No newline at end of file + diff --git a/templates/update-address.html b/templates/update-address.html new file mode 100644 index 0000000..c285abe --- /dev/null +++ b/templates/update-address.html @@ -0,0 +1,92 @@ + + + + + + Update Address Record with REST API + + + +

Address Data from API

+
+ +

Update Address Data

+
+ + + + + + + +
+ +

Add Data

+

Update Data

+

Add Address

+

Delete Data

+ + + + diff --git a/templates/update.html b/templates/update.html index 5a520ae..509182e 100644 --- a/templates/update.html +++ b/templates/update.html @@ -25,10 +25,15 @@

Update Data

+ + +

Add Data

+

Add Address Data

+

Update Address Data

Delete Data

@@ -55,8 +60,9 @@

Delete Data

const id = document.getElementById('edit-id').value; const name = document.getElementById('edit-name').value; const email = document.getElementById('edit-email').value; + const password = document.getElementById('edit-password').value; - fetch('/users/' + id, { + fetch('/users', { method: 'PUT', headers: { 'Content-Type': 'application/json', @@ -65,6 +71,7 @@

Delete Data

id: id, name: name, email: email, + password: password, }), }) .then(response => response.text())