For this assessment, you'll be working with Late Night TV show domain.
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
The application keeps track of the guests that have appeared on the show.
There are three models in the domain: Guest
, Episode
, and Appearance
.
You need to create the following relationships:
- An
Episode
has manyGuest
s throughAppearance
- A
Guest
has manyEpisode
s throughAppearance
- An
Appearance
belongs to aGuest
and belongs to anEpisode
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
Note that this seed file uses a CSV file to populate the database. 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 Appearance
model:
- must have a
rating
between 1 and 5 (inclusive - 1 and 5 are okay)
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,
"date": "1/11/99",
"number": 1
},
{
"id": 2,
"date": "1/12/99",
"number": 2
}
]
If the Episode
exists, return JSON data in the format below:
{
"id": 100,
"date": "9/30/99",
"number": 100,
"guests": [
{
"id": 122,
"name": "Bruce McCulloch",
"occupation": "actor"
},
{
"id": 123,
"name": "Mark McKinney",
"occupation": "actor"
}
]
}
If the Episode
does not exist, return the following JSON data, along with the
appropriate HTTP status code:
{
"error": "Episode not found"
}
If the Episode
exists, it should be removed from the database, along with any
Appearance
s that are associated with it (an Appearance
belongs to an
Episode
, so you need to delete the Appearance
s before the Episode
can be
deleted).
After deleting the Episode
, return an empty response body, along with the
appropriate HTTP status code.
If the Episode
does not exist, return the following JSON data, along with the
appropriate HTTP status code:
{
"error": "Episode not found"
}
Return JSON data in the format below:
[
{
"id": 122,
"name": "Bruce McCulloch",
"occupation": "actor"
},
{
"id": 123,
"name": "Mark McKinney",
"occupation": "actor"
}
]
This route should create a new Appearance
that is associated with an existing
Episode
and Guest
. It should accept an object with the following properties
in the body of the request:
{
"rating": 5,
"episode_id": 100,
"guest_id": 123
}
If the Appearance
is created successfully, send back a response with the
following data:
{
"id": 1,
"rating": 5,
"episode": {
"id": 100,
"date": "9/30/99",
"number": 100
},
"guest": {
"id": 123,
"name": "Mark McKinney",
"occupation": "actor"
}
}
If the Appearance
is not created successfully, return the following JSON
data, along with the appropriate HTTP status code:
{
"errors": ["validation errors"]
}