Vendor Sweets : README
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 are tests included which you can run using
pytest -x.
Depending on your preference, you can either check your API by:
- Using Postman to make requests
- Running
pytest -xand seeing if your code passes the tests - Running the React application in the browser and interacting with the API via the frontend
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 clientYou can run your Flask API on localhost:5555 by
running:
python server/app.pyYou can run your React app on localhost:4000 by
running:
npm start --prefix clientYou 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:
The file server/models.py defines the model classes without relationships templates. You are required to separate the model entities into individual scripts and fulfil the commented instructions on the template for each model class.
The examples also contain necessary imports.
Use the following commands to create the initial database app.db:
export FLASK_APP=server/app.py
flask db init
flask db upgrade headNow you can implement the relationships as shown in the ER Diagram:
- A
Sweethas manyVendors throughVendorSweet - A
Vendorhas manySweets throughVendorSweet - A
VendorSweetbelongs to aSweetand belongs to aVendor
Update server/models.py to establish the model relationships. Since a
VendorSweet belongs to a Vendor and a Sweet, configure the model to
cascade deletes.
Set serialization rules to limit the recursion depth.
Run the migrations and seed the database:
flask db revision --autogenerate -m 'message'
flask db upgrade head
python server/seed.pyIf you aren't able to get the provided seed file working, you are welcome to generate your own seed data to test the application.
Add validations to the VendorSweet model:
pricemust have a value (i.e. can't be None)pricecannot 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
}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"
}