Skip to content

Latest commit

 

History

31 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BLOOM Inventory REST API

A small Express and PostgreSQL REST API for managing books, genres, and authors.

Tech Stack

  • Node.js
  • Express
  • PostgreSQL
  • zod
  • pg
  • dotenv
  • EJS

Setup

Install dependencies:

npm install

Create a .env file with your local PostgreSQL connection details:

DB_HOST=localhost
DB_USER=your_postgres_user
DB_NAME=your_database_name
DB_PORT=5432

If your local Postgres user requires a password, add:

DB_PASSWORD=your_password

You can also use a connection string instead:

DATABASE_URL=postgres://your_postgres_user@localhost:5432/your_database_name

Start the dev server:

npm run dev

By default, the app runs at:

http://localhost:3000

API Overview

All request bodies should be sent as JSON with this header:

Content-Type: application/json

Empty collection responses use []. Missing single resources return {} with a 404 status code.

Request bodies and route IDs are validated with Zod before they reach the controllers. Route IDs must be positive integers. Required string fields are trimmed and must not be empty.

Validation failures return 400 with details:

{
	"status": "error",
	"message": "Validation Failed",
	"errors": [
		{
			"field": "genreName",
			"message": "Genre name is required"
		}
	]
}

Unknown routes return 404:

{
	"message": "Route doesn't exist. See API Documentation"
}

Books

Get All Books

GET /books

Returns an array of books:

[
	{
		"id": 1,
		"title": "Dune",
		"genreId": 1,
		"genreName": "Science Fiction"
	}
]

Get One Book

GET /books/:id

Example:

GET /books/1

Returns:

{
	"id": 1,
	"title": "Dune",
	"genreId": 1,
	"genreName": "Science Fiction"
}

Create a Book

POST /books

Required fields:

  • title
  • genreName

Optional fields:

  • authorName must not be empty if included

Body:

{
	"title": "Dune",
	"genreName": "Science Fiction",
	"authorName": "Frank Herbert"
}

If the genre or author does not exist yet, the app creates it before linking it to the book.

Success response:

{
	"id": 1,
	"title": "Dune",
	"genreId": 1,
	"genreName": "Science Fiction"
}

Update a Book

PUT /books/:id

Required fields:

  • title
  • genreName

Body:

{
	"title": "Dune Messiah",
	"genreName": "Science Fiction"
}

If the genre does not exist yet, the app creates it before updating the book.

Success response:

{
	"id": 1,
	"title": "Dune Messiah",
	"genreId": 1,
	"genreName": "Science Fiction"
}

Delete a Book

DELETE /books/:id

Example:

DELETE /books/1

Success response:

{
	"id": 1,
	"title": "Dune",
	"genreId": 1,
	"genreName": "Science Fiction"
}

Genres

Get All Genres

GET /genres

Returns an array of genre objects:

[
	{
		"id": 1,
		"genreName": "Science Fiction"
	},
	{
		"id": 2,
		"genreName": "Fantasy"
	}
]

Get One Genre

GET /genres/:id

Example:

GET /genres/1

Returns:

{
	"id": 1,
	"genreName": "Science Fiction"
}

Create a Genre

POST /genres

Required fields:

  • genreName

Body:

{
	"genreName": "Science Fiction"
}

Success response:

{
	"id": 1,
	"genreName": "Science Fiction"
}

Update a Genre

PUT /genres/:id

Required fields:

  • genreName

Body:

{
	"genreName": "Speculative Fiction"
}

Success response:

{
	"id": 1,
	"genreName": "Speculative Fiction"
}

Delete a Genre

DELETE /genres/:id

Example:

DELETE /genres/1

Success response:

{
	"id": 1,
	"genreName": "Science Fiction"
}

Authors

Get All Authors

GET /authors

Returns an array of author objects:

[
	{
		"id": 1,
		"authorName": "Frank Herbert"
	},
	{
		"id": 2,
		"authorName": "Ursula K. Le Guin"
	}
]

Get One Author

GET /authors/:id

Example:

GET /authors/1

Returns:

{
	"id": 1,
	"authorName": "Frank Herbert"
}

Create an Author

POST /authors

Required fields:

  • authorName

Body:

{
	"authorName": "Frank Herbert"
}

Success response:

{
	"id": 1,
	"authorName": "Frank Herbert"
}

Update an Author

PUT /authors/:id

Required fields:

  • authorName

Body:

{
	"authorName": "Franklin Herbert"
}

Success response:

{
	"id": 1,
	"authorName": "Franklin Herbert"
}

Delete an Author

DELETE /authors/:id

Example:

DELETE /authors/1

Success response:

{
	"id": 1,
	"authorName": "Frank Herbert"
}

Example Requests

Create a genre:

Method: POST

URL: http://localhost:3000/genres

Headers:

Content-Type: application/json

Body:

{
	"genreName": "Science Fiction"
}

Create an author:

Method: POST

URL: http://localhost:3000/authors

Headers:

Content-Type: application/json

Body:

{
	"authorName": "Frank Herbert"
}

Create a book with a genre and author:

Method: POST

URL: http://localhost:3000/books

Headers:

Content-Type: application/json

Body:

{
	"title": "Dune",
	"genreName": "Science Fiction",
	"authorName": "Frank Herbert"
}

Update a book:

Method: PUT

URL: http://localhost:3000/books/1

Headers:

Content-Type: application/json

Body:

{
	"title": "Dune Messiah",
	"genreName": "Science Fiction"
}

Delete a book:

Method: DELETE

URL: http://localhost:3000/books/1

Body: none

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages