For this assessment, you'll be working with a vendors and sweets domain.
In this repo:
- There is a Flask application with some features built out.
- There is a fully built React frontend application.
- There is a file
challenge-3-sweets.postman_collection.json
that contains a Postman collection of requests for testing each route you will implement.
Depending on your preference, you can either check your API by:
- Using Postman to make requests
- Running the React application in the browser and interacting with the API via the frontend
You can import challenge-3-sweets.postman_collection.json
into Postman by
pressing the Import
button.
Select Upload Files
, navigate to this repo folder, and select
challenge-3-sweets.postman_collection.json
as the file to import.
The instructions assume you changed into the code-challenge
folder prior
to opening the code editor.
To download the dependencies for the frontend and backend, run:
pipenv install
pipenv shell
npm install --prefix client
You can run your Flask API on localhost:5555
by
running:
python server/app.py
You can run your React app on localhost:4000
by
running:
npm start --prefix client
You are not being assessed on React, and you don't have to update any of the React code; the frontend code is available just so that you can test out the behavior of your API in a realistic setting.
Your job is to build out the Flask API to add the functionality described in the deliverables below.
You will implement an API for the following data model:
Add validations to the VendorSweet
model:
price
must have a value (i.e. can't be None)price
cannot be a negative number
Set up the following routes. Make sure to return JSON data in the format specified along with the appropriate HTTP verb.
Return JSON data in the format below:
[
{ "id": 1, "name": "Insomnia Cookies" },
{ "id": 2, "name": "Cookies Cream" }
]
If the Vendor
exists, return JSON data in the format below:
{
"id": 1,
"name": "Insomnia Cookies",
"vendor_sweets": [
{
"id": 2,
"price": 45,
"sweet": {
"id": 2,
"name": "Chocolate Chunk Cookie"
},
"sweet_id": 2,
"vendor_id": 1
}
]
}
If the Vendor
does not exist, return the following JSON data, along with the
appropriate HTTP status code:
{
"error": "Vendor not found"
}
Return JSON data in the format below:
[
{
"id": 1,
"name": "Chocolate Chip Cookie"
},
{
"id": 2,
"name": "Brownie"
}
]
If the Sweet
exists, return JSON data in the format below:
{
"id": 1,
"name": "Chocolate Chip Cookie"
}
If the Sweet
does not exist, return the following JSON data, along with the
appropriate HTTP status code:
{
"error": "Sweet not found"
}
This route should create a new VendorSweet
that is associated with an existing
Vendor
and Sweet
. It should accept an object with the following properties
in the body of the request:
{
"price": 300,
"vendor_id": 1,
"sweet_id": 3
}
If the VendorSweet
is created successfully, send back a response with the
following data:
{
"id": 7,
"price": 300,
"sweet": {
"id": 3,
"name": "M&Ms Cookie"
},
"sweet_id": 3,
"vendor": {
"id": 1,
"name": "Insomnia Cookies"
},
"vendor_id": 1
}
Stretch Goal: If the VendorSweet
is not created successfully, return the following JSON
data, along with the appropriate HTTP status code:
{ "errors": ["validation errors"] }
This route should delete an existing VendorSweet
. If the VendorSweet
exists
and is deleted successfully, return an empty object as a response:
{}
If the VendorSweet
does not exist, return the following JSON data, along with
the appropriate HTTP status code:
{
"error": "VendorSweet not found"
}