Code Monkey home page Code Monkey logo

byob's Introduction

Build Your Own Backend: Authors + Books

This is a week-long solo project to practice building databases using Express, Knex, and PostgreSQL. I scraped the book publisher FSG Originals website to create a one-to-many relational database schema of authors and books. I built a RESTful API with 4 GET endpoints, 2 POST endpoints, and 1 DELETE endpoint (detailed below).

API endpoints

Authors

GET /api/v1/authors

Response sends all authors in the database. If the authors table is populated, the response has a 200 status and sends the following:

  [{
      "id": 168,
      "name": "BEN LERNER",
      "bio": "Ben Lerner was born in Topeka, Kansas, in 1979. He has received fellowships from the Fulbright, Guggenheim, Howard, and MacArthur Foundations.",
      "created_at": "2019-05-01T16:33:27.240Z",
      "updated_at": "2019-05-01T16:33:27.240Z"
    },
    {
      "id": 176,
      "name": "RICHARD LLOYD PARRY",
      "bio": "Richard Lloyd Parry is the Asia editor and Tokyo bureau chief of The Times (London) and the author of People Who Eat Darkness and In the Time of Madness.",
      "created_at": "2019-05-01T16:33:27.245Z",
      "updated_at": "2019-05-01T16:33:27.245Z"
    },
    {
      "id": 142,
      "name": "CLAY BYARS",
      "bio": "Clay Byars attended The School of Letters at Sewanee, Tennessee, and is the assistant editor for Narrative Magazine. He lives with his two dogs on a farm outside Birmingham, Alabama.",
      "created_at": "2019-05-01T16:33:27.218Z",
      "updated_at": "2019-05-01T16:33:27.218Z"
    }]

If the authors table is not populated, the response has a 404 status and sends the following:

    {
      error: 'No authors found'
    }

GET /api/v1/authors/:id

Response sends a single author from the database with the id that matches the parameter in the request. For example, for the request /api/v1/authors/183 the response has a 200 status and sends the following:

   [{
     "id": 183,
     "name": "JEFF VANDERMEER",
     "bio": "Jeff VanderMeer is an award-winning novelist and editor.",
     "created_at": "2019-05-01T16:33:27.249Z",
     "updated_at": "2019-05-01T16:33:27.249Z"
   }]

If the no id matches the parameter in the request, the response has a 404 status and sends an error. For example, the request /api/v1/authors/999 sends the following:

    {
      error: 'Could not find author with id 999'
    }

POST /api/v1/authors

Allows users to post a new author to the authors table with the following parameters:

Name Type Description
name string Name of new author
bio string Biography of new author

The response has a 201 status and sends the unique id created for the new record. For example, the request body

{ "name": "Joan Didion", "bio": "Joan Didion is an award-winning novelist and critic" }

sends the following response:

   {
     "id": 187
   }

If any of the required paramaters are missing from the request, the response has a 422 status and sends an error. For example, the request

{ "name": "Joan Didion" }

sends the following response:

  {
    error: `Expected format: { name: <String>, bio: <String> }. You're missing a bio property.`
  }

Books

GET /api/v1/books

Response sends all books in the database. If the books table is populated, the response has a 200 status and sends the following:

   [{
      "id": 39,
      "title": "The Hatred of Poetry",
      "pub": "2016",
      "author_id": 168,
      "created_at": "2019-05-01T16:33:27.277Z",
      "updated_at": "2019-05-01T16:33:27.277Z"
    },
    {
      "id": 51,
      "title": "People Who Eat Darkness",
      "pub": "2012",
      "author_id": 176,
      "created_at": "2019-05-01T16:33:27.284Z",
      "updated_at": "2019-05-01T16:33:27.284Z"
    },
    {
      "id": 3,
      "title": "Will & I",
      "pub": "2016",
      "author_id": 142,
      "created_at": "2019-05-01T16:33:27.252Z",
      "updated_at": "2019-05-01T16:33:27.252Z"
    }]

If the books table is not populated, the response has a 404 status and sends the following:

    {
      error: 'No books found'
    }

GET /api/vi/authors/:id/books

Response sends all books with the author_id that matches the parameter in the request. For example, for the request /api/v1/authors/183/books, the response has a 200 status and sends the following:

   [{
      "id": 59,
      "title": "The Strange Bird",
      "pub": "2018",
      "author_id": 183,
      "created_at": "2019-05-01T16:33:27.289Z",
      "updated_at": "2019-05-01T16:33:27.289Z"
    },
    {
      "id": 60,
      "title": "Annihilation",
      "pub": "2018",
      "author_id": 183,
      "created_at": "2019-05-01T16:33:27.290Z",
      "updated_at": "2019-05-01T16:33:27.290Z"
    },
    {
      "id": 61,
      "title": "Area X",
      "pub": "2014",
      "author_id": 183,
      "created_at": "2019-05-01T16:33:27.290Z",
      "updated_at": "2019-05-01T16:33:27.290Z"
    },
    {
      "id": 62,
      "title": "Acceptance",
      "pub": "2014",
      "author_id": 183,
      "created_at": "2019-05-01T16:33:27.291Z",
      "updated_at": "2019-05-01T16:33:27.291Z"
    },
    {
      "id": 63,
      "title": "Authority",
      "pub": "2014",
      "author_id": 183,
      "created_at": "2019-05-01T16:33:27.292Z",
      "updated_at": "2019-05-01T16:33:27.292Z"
    }]

If no id matches the parameter in the request, the response has a 404 status and sends an error. For example, the request /api/v1/authors/999/books sends the following:

    {
      error: 'Could not find any books with an author_id of 999'
    }

POST /api/v1/books

Allows users to post a new book to the books table with the following parameters:

Name Type Description
title string Title of new book
pub integer Year of publication
author_id integer Foreign key associated with new book's author

The response has a 201 status and sends the unique id created for the new record. For example, the request body

{ "title": "Slouching Towards Bethlehem", "pub": 1968, "author_id": 187 }

sends the following response:

   {
     "id": 83
   }

If any of the required paramaters are missing from the request, the response has a 422 status and sends an error. For example, the request

{ "title": "Slouching Towards Bethlehem", "pub": 1968 }

sends the following response:

  {
    error: `Expected format: { title: <String>, pub: <integer>, author_id: <integer> }. You're missing the author_id property.`
  }

DELETE /api/v1/books/:id

Allows users to delete a single book that matches the id parameter in the request. For example, the request /api/v1/books/83 has a 200 status and sends the following:

"Deleted title 'Slouching Towards Bethlehem' with id 83"

If no id matches the parameter in the request, the response has a 404 status and sends an error. For example, the request /api/v1/books/999 sends the following:

    {
      error: 'Could not find book with id 999'
    }

byob's People

Contributors

taylorsperry avatar

byob's Issues

checklist

Abstract

BYOB is a one week long solo project. It is ungraded and intended as a way to get comfortable with building databases using Express, Knex, and PostgreSQL. You will be building a RESTful API, complete with professional-grade documentation. This knowledge will be directly applicable to the next paired project, Palette Picker.

  • Each table must have no fewer than three columns.

Required endpoints

  • 4 GET endpoints

  • 2 GET endpoints for all of one resource (i.e. '/api/v1/merchants')

  • 2 GET endpoints for a specific resource (i.e. '/api/v1/merchants/:id')

  • 2 POST endpoints

  • 1 DELETE endpoint

  • Status Codes & Error Handling

  • All endpoints should respond with the minimum status code results:
    200/201: Success
    404: Not Found

  • If POST request fails to save an entity due to bad information being sent from the client, you should respond with
    422: Unprocessable Entity

  • If you have a critical server error, you should respond with
    500: Internal Server Error

In addition to responding with the appropriate status code, you are expected to...

  • send back clear, informative error messages when something goes wrong. Do not simply console.log 'WHATEVER'. If a POST request fails because the request didn't include a required parameter, respond with something like 'Entity requires a but none was provided.'

Documentation

  • In the README, developer should provide documentation on the API endpoints that can be hit. Here is a great example of in-depth documentation Pay attention to the information provided and the format that it's presented in.

Some things you want to considering having in your API documentation:

  • Endpoints available (e.g. GET /api/v1/students, POST api/v1/students)
  • What parameters can be used in certain requests (e.g. For a POST request, what should be put in the request body?)
  • Sample responses from endpoints (What does the response object look like for a request?)
  • You can put your documentation in the README of your BYOB GitHub repository. Remember, improperly formatted information can make it very difficult to read even if it's all accurate, so be sure to utilize markdown syntax styling/formatting (here is a markdown style cheatsheet).

Deployment

  • Your application should be deployed to Heroku

Articulation Requirement

  • In addition to the functional requirement, on a separate dedicated git branch, go through each line of the server file and put a comment on each line that explains what that line of code is doing. Be as explicit as you can.

Extension: Create a FE documentation in your BE repo

If all other expectations and requirements are met, you may:

  • Create a front-end (in the same repository as BYOB) that will document the API.
  • Provide a page that documents the available endpoints, the data that will be received, and the data the user must send.
  • The site should be interactive, allowing a developer to "try out" endpoints. Think of building out something like Postman specifically for your back-end.
  • This front-end documentation page should be a single page and can be written in Javascript, jQuery, or React.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.