For this assessment, you'll be working on an API for tracking heroes and their superpowers.
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
. - There is a file
challenge-2-superheroes.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
pytest -x
and seeing if your code passes the tests - Running the React application in the browser and interacting with the API via the frontend
You can import challenge-2-superheroes.postman_collection.json
into Postman by
pressing the Import
button.
Select Upload Files
, navigate to this repo folder, and select
challenge-2-superheroes.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:
The file server/models.py
defines the model classes without relationships.
Use the following commands to create the initial database app.db
:
export FLASK_APP=server/app.py
flask db init
flask db upgrade head
Now you can implement the relationships as shown in the ER Diagram:
- A
Hero
has manyPower
s throughHeroPower
- A
Power
has manyHero
s throughHeroPower
- A
HeroPower
belongs to aHero
and belongs to aPower
Update server/models.py
to establish the model relationships. Since a
HeroPower
belongs to a Hero
and a Power
, 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.py
If 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 HeroPower
model:
strength
must be one of the following values: 'Strong', 'Weak', 'Average'
Add validations to the Power
model:
description
must be present and at least 20 characters long
Set up the following routes. Make sure to return JSON data in the format specified along with the appropriate HTTP verb.
Recall you can specify fields to include or exclude when serializing a model instance to a dictionary using to_dict() (don't forget the comma if specifying a single field).
NOTE: If you choose to implement a Flask-RESTful app, you need to add code to
instantiate the Api
class in server/app.py.
Return JSON data in the format below:
[
{
"id": 1,
"name": "Kamala Khan",
"super_name": "Ms. Marvel"
},
{
"id": 2,
"name": "Doreen Green",
"super_name": "Squirrel Girl"
},
{
"id": 3,
"name": "Gwen Stacy",
"super_name": "Spider-Gwen"
},
{
"id": 4,
"name": "Janet Van Dyne",
"super_name": "The Wasp"
},
{
"id": 5,
"name": "Wanda Maximoff",
"super_name": "Scarlet Witch"
},
{
"id": 6,
"name": "Carol Danvers",
"super_name": "Captain Marvel"
},
{
"id": 7,
"name": "Jean Grey",
"super_name": "Dark Phoenix"
},
{
"id": 8,
"name": "Ororo Munroe",
"super_name": "Storm"
},
{
"id": 9,
"name": "Kitty Pryde",
"super_name": "Shadowcat"
},
{
"id": 10,
"name": "Elektra Natchios",
"super_name": "Elektra"
}
]
If the Hero
exists, return JSON data in the format below:
{
"id": 1,
"name": "Kamala Khan",
"super_name": "Ms. Marvel",
"hero_powers": [
{
"hero_id": 1,
"id": 1,
"power": {
"description": "gives the wielder the ability to fly through the skies at supersonic speed",
"id": 2,
"name": "flight"
},
"power_id": 2,
"strength": "Strong"
}
]
}
If the Hero
does not exist, return the following JSON data, along with the
appropriate HTTP status code:
{
"error": "Hero not found"
}
Return JSON data in the format below:
[
{
"description": "gives the wielder super-human strengths",
"id": 1,
"name": "super strength"
},
{
"description": "gives the wielder the ability to fly through the skies at supersonic speed",
"id": 2,
"name": "flight"
},
{
"description": "allows the wielder to use her senses at a super-human level",
"id": 3,
"name": "super human senses"
},
{
"description": "can stretch the human body to extreme lengths",
"id": 4,
"name": "elasticity"
}
]
If the Power
exists, return JSON data in the format below:
{
"description": "gives the wielder super-human strengths",
"id": 1,
"name": "super strength"
}
If the Power
does not exist, return the following JSON data, along with the
appropriate HTTP status code:
{
"error": "Power not found"
}
This route should update an existing Power
. It should accept an object with
the following properties in the body of the request:
{
"description": "Valid Updated Description"
}
If the Power
exists and is updated successfully (passes validations), update
its description and return JSON data in the format below:
{
"description": "Valid Updated Description",
"id": 1,
"name": "super strength"
}
If the Power
does not exist, return the following JSON data, along with the
appropriate HTTP status code:
{
"error": "Power not found"
}
If the Power
is not updated successfully (does not pass validations),
return the following JSON data, along with the appropriate HTTP status code:
{
"errors": ["validation errors"]
}
This route should create a new HeroPower
that is associated with an existing
Power
and Hero
. It should accept an object with the following properties in
the body of the request:
{
"strength": "Average",
"power_id": 1,
"hero_id": 3
}
If the HeroPower
is created successfully, send back a response with the data
related to the new HeroPower
:
{
"id": 11,
"hero_id": 3,
"power_id": 1,
"strength": "Average",
"hero": {
"id": 3,
"name": "Gwen Stacy",
"super_name": "Spider-Gwen"
},
"power": {
"description": "gives the wielder super-human strengths",
"id": 1,
"name": "super strength"
}
}
If the HeroPower
is not created successfully, return the following JSON
data, along with the appropriate HTTP status code:
{
"errors": ["validation errors"]
}