Hang In There API Requirements

Back to Hang In There API Home

1. Setup

  1. Create a Rails API project called hang_in_there_api with the command rails new hang_in_there_api -T -d="postgresql" --api

  2. Add your gems to your development, test section in the Gemfile:
    • rspec-rails
    • pry
    • simplecov
  3. Run the following commands to get started:
     $ cd hang_in_there_api
     $ bundle install
     $ rails db:create
     $ rails g rspec:install
    
  4. Your database will have one table: posters. Use migrations to create this table with the following attributes:

    • id as integer
    • name as string
    • description as string
    • price as float
    • year as integer
    • vintage as boolean
    • img_url as string
    • created_at as timestamp
    • updated_at as timestamp
  5. It will make it easier to test as you develop if you create some seeds in your seeds.rb file. You can use the following pattern to create seed data.
     Poster.create(name: "REGRET",
                   description: "Hard work rarely pays off.",
                   price: 89.00,
                   year: 2018,
                   vintage: true,
                   img_url:  "https://plus.unsplash.com/premium_photo-1661293818249-fddbddf07a5d")
    
  6. Finally, commit your setup steps and push to a new repo. Share that new repo with your project partner(s). Be sure to add them as a collaborator.

2. API Endpoints, general definitions

You will need to expose the data through a multitude of API endpoints. All of your endpoints should follow these technical expectations:

  • All endpoints will expect to return JSON data only
  • All endpoints should be exposed under an api and version (v1) namespace (e.g. /api/v1/posters)
  • API will be compliant to the JSON API spec and match our requirements below precisely
  • Controller actions should be limited to only the standard Rails actions and follow good RESTful convention.

You will need to expose the following RESTful API endpoints for the following:

You may choose to divide these up between your project partners in whatever way seems best; you may also choose to implement the first story/stories together to both have a solid understanding first, before dividing & conquering if you choose.


3. Using ActiveRecord and SQL

Now we’re going to make our app work a little harder for us by returning a count of items returned per request and allowing users to sort and filter results through the use of query params in their requests. As in the previous section:

  • All endpoints will expect to return JSON data only
  • All endpoints should be exposed under an api and version (v1) namespace (e.g. /api/v1/posters)
  • API will be compliant to the JSON API spec and match our requirements below precisely
  • Controller actions should be limited to only the standard Rails actions and follow good RESTful convention.

4. Extensions and Exploration - Validations and Errors

Validations

Add some validations to your Poster model:

  • name should be required AND unique
  • description should be required
  • year should be required AND an integer
  • price should be required AND a float
  • vintage should be required

Write tests for these validations. You can add the shoulda-matchers gem to help with testing if you’d like.

Errors

Right now you are only solving for what happens when everything goes right. We call this the Happy Path. But what about when things go wrong? And what can go wrong here anyways? Think about what your code would do in the following scenarios. Try it out in Postman. Is this what you want to happen?

  • GET /api/v1/posters/:id
    • For an ID that doesn’t exist
  • POST /api/v1/posters
    • When we’re missing some attributes
    • If someone tries to create a poster with a duplicate name?
  • PATCH /api/v1/posters/:id
    • If we try to update the name to a name that already exists in our database?
    • If we try to delete a required attribute?

We have to assume our users will sometimes try to do things we don’t want or expect them to. We call these scenarios Sad Paths and Edge Cases. Update your endpoints to handle these errors.

Examples

Lesson Search Results

Showing top 10 results