For this assessment, you'll be working on an API for tracking heroes and their superpowers.
In this repo, there is a Rails application with some features built out. There is also a fully built React frontend application, so you can test if your API is working.
Your job is to build out the Rails API to add the functionality described in the deliverables below.
To download the dependencies for the frontend and backend, run:
bundle install
npm install --prefix client
There is some starter code in the db/seeds.rb
file so that once you've
generated the models, you'll be able to create data to test your application.
You can run your Rails API on localhost:3000
by running:
rails s
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.
There are also tests included which you can run using rspec
to check your work.
Depending on your preference, you can either check your progress by:
- Running
rspec
and seeing if your code passes the tests - Running the React application in the browser and interacting with the API via the frontend
- Running the Rails server and using Postman to make requests
You need to create the following relationships:
- A
Hero
has manyPower
s throughHeroPower
- A
Power
has manyHero
s throughHeroPower
- A
HeroPower
belongs to aHero
and belongs to aPower
Start by creating the models and migrations for the following database tables:
If you use a Rails generator to create the models, make sure to use the
--no-test-framework
flag to avoid overwriting the test files.
Add any code needed in the model files to establish the relationships.
Then, run the migrations and seed file:
rails db:migrate db:seed
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.
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" }
]
If the Hero
exists, return JSON data in the format below:
{
"id": 1,
"name": "Kamala Khan",
"super_name": "Ms. Marvel",
"powers": [
{
"id": 1,
"name": "super strength",
"description": "gives the wielder super-human strengths"
},
{
"id": 2,
"name": "flight",
"description": "gives the wielder the ability to fly through the skies at supersonic speed"
}
]
}
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:
[
{
"id": 1,
"name": "super strength",
"description": "gives the wielder super-human strengths"
},
{
"id": 1,
"name": "flight",
"description": "gives the wielder the ability to fly through the skies at supersonic speed"
}
]
If the Power
exists, return JSON data in the format below:
{
"id": 1,
"name": "super strength",
"description": "gives the wielder super-human strengths"
}
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": "Updated description"
}
If the Power
exists and is updated successfully (passes validations), update
its description and return JSON data in the format below:
{
"id": 1,
"name": "super strength",
"description": "Updated description"
}
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 Hero
:
{
"id": 1,
"name": "Kamala Khan",
"super_name": "Ms. Marvel",
"powers": [
{
"id": 1,
"name": "super strength",
"description": "gives the wielder super-human strengths"
},
{
"id": 2,
"name": "flight",
"description": "gives the wielder the ability to fly through the skies at supersonic speed"
}
]
}
If the HeroPower
is not created successfully, return the following
JSON data, along with the appropriate HTTP status code:
{
"errors": ["validation errors"]
}