Skip to content

invertostew/book-library-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MCR Codes Book Library API

This is my Book Library API project for Manchester Codes as part of the Backend module.

Navigation

Description

The main purpose of this project is to:

  • Learn how to take user stories and turn them into requirements.
  • Expand my knowledge of database design.
  • Learn how to use Sequelize to perform SQL queries.
  • Get real world practice with documentation by figuring out how to implement validation on models.

Back to Top

Features

  • Create, read, update and delete readers, authors, genres and books.
  • Associate books with their authors.
  • Associate books with their genres.
  • Search for books based on author, or genre.
  • The readers password is never returned to the users of the API.

Back to Top

Requirements

  • Node JS - JavaScript runtime environment

Application Dependencies

  • Express - to set up the server and manage routes and responses
  • Helmet - to add some security middleware to Express
  • MySQL2 - to work with the MySQL database engine
  • Sequelize - to work with MySQL using a JavaScript ORM

Development Dependencies

  • Faker - to generate fake data used for testing
  • Mocha - testing framework of choice for this project
  • Chai - testing assertion library used to provide the testing framework (in this case Mocha) with assertions
  • SuperTest - HTTP assertion library used to test HTTP servers
  • Dotenv - to store environment variables
  • Nodemon - to run the npm run start:dev script, allowing for hot reloading
  • ES Lint - to analyze the code for style guide problems
  • Prettier - to format code and keep styling consistent

Back to Top

Getting Started

  • Pull down a MySQL image from DockerHub
    • docker pull bbatm9/music_library_mysql
    • more info: DockerHub
  • Set up the container
    • docker run -d -p <OUTSIDE_PORT>:<INSIDE_PORT> --name <NAME> -e MYSQL_ROOT_PASSWORD=<PASSWORD> <IMAGE_NAME>
  • Clone this repo
    • e.g. git clone https://github.com/invertostew/book-library-api.git
  • Change into the repo directory
    • cd book-library-api (or the directory you specified the repo to be cloned into)
  • Install the dependencies from package-lock.json
    • npm install
  • Create an .env and .env.test file in the project root
    • touch .env && touch .env.test
  • Add the following to both .env and .env.test, switching out the <VALUE> as necessary
    • DB_HOST=<YOUR_DB_HOST>
    • DB_USER=<YOUR_DB_USER>
    • DB_PASSWORD=<YOUR_DB_PASSWORD>
    • DB_PORT=<YOUR_DB_PORT> (as specified in the docker run command)
    • DB_NAME=<YOUR_DB_NAME>
    • APP_PORT=<YOUR_APP_PORT> (optional, will default to 4000)
    • IMPORTANT: Ensure DB_NAME is different in .env than it is in .env.test, the database in .env.test will be dropped each time.
  • To start the production server:
    • npm start
  • To start the development server:
    • npm run start:dev

Back to Top

Documentation

Readers

HTTP Verb Route Required Fields
GET /readers
POST /readers name, email, password
GET /readers/:id
PATCH /readers/:id
DELETE /readers/:id

Example Request Body

// Endpoint: POST /readers
{
    "name": "First Last",
    "email": "[email protected]",
    "password": "VerySecurePassword"
}

// Endpoint: PATCH /readers/1
{
    "name": "First Last Updated!"
}
Options and Validation
  • name (required)
    • must be provided
  • email (required)
    • must be provided
    • must be an email address
  • password (required)
    • must be provided
    • must be more than 8 characters
    • must be less than 64 characters

Back to Top

Authors

HTTP Verb Route Required Fields
GET /authors
POST /authors author
GET /authors/:id
PATCH /authors/:id
DELETE /authors/:id

Example Request Body

// Endpoint: POST /authors
{
    "author": "First Last"
}

// Endpoint: PATCH /authors/1
{
    "author": "First Last Updated!"
}
Options and Validation
  • author (required)
    • must be provided
    • must be unique

Back to Top

Genres

HTTP Verb Route Required Fields
GET /genres
POST /genres genre
GET /genres/:id
PATCH /genres/:id
DELETE /genres/:id

Example Request Body

// Endpoint: POST /genres
{
    "genre": "Sci-Fy"
}

// Endpoint: PATCH /genres/1
{
    "genre": "Sci-Fi"
}
Options and Validation
  • genre (required)
    • must be provided
    • must be unique

Back to Top

Books

HTTP Verb Route Required Fields Optional Fields
GET /books
POST /books title, AuthorId ISBN, ReaderId, GenreId
GET /books/:id
PATCH /books/:id
DELETE /books/:id

Example Request Body

// Endpoint: POST /books
{
    "title": "Book Title",
    "ISBN": 1234567890,
    "ReaderId": 1,
    "AuthorId": 1,
    "GenreId": 1
}

// Endpoint: PATCH /books/1
{
    "title": "New Book Title",
    "ISBN": 1234567890123
}
Options and Validation
  • title (required)
    • must be provided
  • ISBN (optional)
    • must be between 10 and 13 characters to match ISBN-10 or ISBN-13
    • must be unique
  • ReaderId (optional)
    • must be a valid ReaderId if provided
  • AuthorId (required)
    • must be a valid AuthorId
  • GenreId (optional)
    • must be a valid GenreId if provided

Back to Top

Testing

Built using a TDD approach. Tests have been split out into unit tests and integration tests. Where possible tests where written as failing tests first, and then built out as per the tests, and later refactored and tested again. At some points it was necessary to spike, in which case I would delete the code, write the test and then re-implement the code.

All tests are located inside of the ./src/__tests__ directory.

If you wish to run the tests for yourself, you can install Faker, Mocha, Chai and SuperTest by running npm install and from there you can run the tests by running npm test.

I also used Insomnia to send requests to my RESTful API endpoints as documented above.

Insomnia API Testing Passing Tests

Back to Top

Future Plans

  • I would like to condense down the CRUD tests in the future to use the CRUD helper functions where possible to make tests more DRY.

Back to Top

About

Book Library RESTful API (Manchester Codes Project)

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published