Blog Python API code and use of List and Dictionaries.
APIs and JSON
The provided code snippet is part of a Flask RESTful API handling HTTP requests for managing user data related to colleges. It supports POST, GET, and PUT methods.
The post function retrieves a list of colleges associated with a specific user by extracting data from the JSON body, querying the database, and returning JSON data of the colleges.
The get function retrieves a list of all colleges from the database and returns JSON data of all colleges.
The update function updates the list of colleges associated with a specific user by extracting data from the JSON body, querying the database, updating the user's record with the new college list, and returning a success message with the updated user ID and college list.
The post method first extracts JSON data from the request body, retrieving the user's ID from the data. It checks if the username is missing, returning an error message and status code 400 if so. Then, it queries the database for the user's record using the obtained ID, returning a 404 error if the user is not found. Next, it decodes the user's college list from the database, queries for colleges matching the list names, converts the query results to JSON format, and returns the data as a response. These steps ensure data validity and provide appropriate error handling during the POST request process.
Frontend
Blog JavaScript API fetch code and formatting code to display JSON.
This code will make a request to get a list of colleges, parse, and then use the forEach function to loop through all of the elements in the array then put them into a li tag which will be appended to ul and shows up in HTML.
When this fetch receives an error it will handle it by giving a popup telling the user that he/she have done something wrong. If everything works, the popup will tell the user that he/she did everything correctly and the user was updated.
Collections
Blog Python Model code and SQLite Database![image](https://github.com/RIFT24/cspquizbackend/assets/142541946/eb80612f-4fd8-4e36-a90e-1438922aa99e)
Lists and Dictionaries
Blog Python API code and use of List and Dictionaries.
![image](https://github.com/RIFT24/cspquizbackend/assets/142541946/ca5e5c84-c94e-4050-9ff7-acc7aad3eb83)
APIs and JSON
The provided code snippet is part of a Flask RESTful API handling HTTP requests for managing user data related to colleges. It supports POST, GET, and PUT methods.
post
function retrieves a list of colleges associated with a specific user by extracting data from the JSON body, querying the database, and returning JSON data of the colleges.get
function retrieves a list of all colleges from the database and returns JSON data of all colleges.update
function updates the list of colleges associated with a specific user by extracting data from the JSON body, querying the database, updating the user's record with the new college list, and returning a success message with the updated user ID and college list.The
![image](https://github.com/RIFT24/cspquizbackend/assets/142541946/10918912-cc6d-467b-b52a-c732e49647f7)
post
method first extracts JSON data from the request body, retrieving the user's ID from the data. It checks if the username is missing, returning an error message and status code 400 if so. Then, it queries the database for the user's record using the obtained ID, returning a 404 error if the user is not found. Next, it decodes the user's college list from the database, queries for colleges matching the list names, converts the query results to JSON format, and returns the data as a response. These steps ensure data validity and provide appropriate error handling during the POST request process.Frontend
Blog JavaScript API fetch code and formatting code to display JSON.
![image](https://github.com/RIFT24/cspquizbackend/assets/142541946/b6ef72eb-ccca-4a16-9350-918a14c9fd1c)
This code will make a request to get a list of colleges, parse, and then use the forEach function to loop through all of the elements in the array then put them into a![image](https://github.com/RIFT24/cspquizbackend/assets/142541946/8363930b-4185-48e9-b6ac-8eae1c07ec07)
li
tag which will be appended toul
and shows up in HTML.When this fetch receives an error it will handle it by giving a popup telling the user that he/she have done something wrong. If everything works, the popup will tell the user that he/she did everything correctly and the user was updated.
![image](https://github.com/RIFT24/cspquizbackend/assets/142541946/5df39947-6076-4d54-8953-9e6125bdbb85)