Question: In this assignment you will need to set up one GET and one POST method. When GET is called, return all objects from the global

In this assignment you will need to set up one GET and one POST method. When GET is called, return all objects from the global list. When POST is called, the user will pass a request object (using swagger) identical to the object in the list with different values. If Product id exists in the list, then modify the values of the product in the list otherwise just add that object to the list. Also, you must define a request object on the top of the class (above app initialization)
without using Flask- lest use pydantic
After you created the app=FastAPI() variable for our API you need to declare our global list of products. We are using a list for now as our database.
In future assignments we will be integrating a database and not be using the list.
Products =[{'Name': 'Apple', 'Price': 4.99, 'Type': 'Fruit'},
{'Name': 'Orange', 'Price': 8.99, 'Type': 'Fruit'},
{'Name': 'Tomato', 'Price': 3.99, 'Type': 'Fruit'},
{'Name': 'Cabbage', 'Price': 1.99, 'Type': 'Vegetable'},
{'Name': 'Potato', 'Price': 2.50, 'Type': 'Vegetable'},
{'Name': 'Potato', 'Price': 2.50, 'Type': 'Vegetable'}]
Request Object
The request object will be used in the POST methods to modify or add new data. The object must contain all variables of the "Products" object from the dictionary list.
Endpoints
/products
Write a function that is mapped to the following endpoint: "/products"
Type: GET
Method Name: get_products
Parameters: NONE
Return: Products[]
The above method should return a list of all products.
/products/mod
Write a function that is mapped to the following endpoint: "/products/mod"
Type: POST
Method Name: update_product
Parameters: product : Product <-(the Request Object)
Return: string
The above method should return "Added New Product" if the request object didn't exist in the global list or it should return "Modified Product" if the product already exists. For this you need to check if the item is already in the list. Remember that typically one field is a unique identifier or ID for an object in a list/database.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!