4. Examples
Base repos for all the following examples can be found here
Example: User Management System
You are tasked with building a User Management System that connects to a PostgreSQL database and allows users to manage user data. Your app should be able to:
- List all users stored in the database.
- Add a new user with a name and email address.
- Update user information by user ID.
- Retrieve user information by user ID.
Functional Requirements:
The database should have a table called
users
with the following columns:id
(serial primary key): The unique identifier for each user.name
(varchar): The name of the user.email
(varchar): The email address of the user.
The Express app should support the following endpoints:
GET /users
: Returns a list of all users.POST /users
: Adds a new user with the provided name and email.PUT /users/:id
: Updates the user information for the specified ID.GET /users/:id
: Retrieves user information for the specified ID.
Detailed Breakdown
Setting up the database connection:
- Use the
pg
npm package to connect to your PostgreSQL database. - Create a
pool.js
file to handle database connections with the appropriate connection details (username, password, database name, etc.).
- Use the
Creating the
users
table:- Ensure the
users
table exists in your PostgreSQL database with the defined schema. - Use the SQL statement
CREATE TABLE IF NOT EXISTS
to create the table if it doesn’t already exist.
- Ensure the
Listing all users:
- Implement the
GET /users
endpoint that queries theusers
table and returns all user records in JSON format.
- Implement the
Adding a new user:
- Implement the
POST /users
endpoint that takes user information from the request body and inserts it into theusers
table.
- Implement the
Updating user information:
- Implement the
PUT /users/:id
endpoint that updates the user information (name and email) based on the provided user ID.
- Implement the
Retrieving user information:
- Implement the
GET /users/:id
endpoint that retrieves and returns the user information for a specific user ID.
- Implement the
Example: Product Inventory System
You are tasked with building a Product Inventory System that connects to a PostgreSQL database and allows users to manage product data. Your app should be able to:
- List all products stored in the database.
- Add a new product with a name, price, and stock quantity.
- Update product information by product ID.
- Delete a product by product ID.
Functional Requirements:
The database should have a table called
products
with the following columns:id
(serial primary key): The unique identifier for each product.name
(varchar): The name of the product.price
(numeric): The price of the product.stock_quantity
(integer): The quantity of the product in stock.
The Express app should support the following endpoints:
GET /products
: Returns a list of all products.POST /products
: Adds a new product with the provided name, price, and stock quantity.PUT /products/:id
: Updates the product information for the specified ID.DELETE /products/:id
: Deletes the product with the specified ID.
Detailed Breakdown
Setting up the database connection:
- Use the
pg
npm package to connect to your PostgreSQL database. - Create a
pool.js
file to handle database connections with the appropriate connection details (username, password, database name, etc.).
- Use the
Creating the
products
table:- Ensure the
products
table exists in your PostgreSQL database with the defined schema. - Use the SQL statement
CREATE TABLE IF NOT EXISTS
to create the table if it doesn’t already exist.
- Ensure the
Listing all products:
- Implement the
GET /products
endpoint that queries theproducts
table and returns all product records in JSON format.
- Implement the
Adding a new product:
- Implement the
POST /products
endpoint that takes product information from the request body and inserts it into theproducts
table.
- Implement the
Updating product information:
- Implement the
PUT /products/:id
endpoint that updates the product information (name, price, and stock quantity) based on the provided product ID.
- Implement the
Deleting a product:
- Implement the
DELETE /products/:id
endpoint that removes the product with the specified ID from theproducts
table.
- Implement the
Example: Library Book Management System
You are tasked with building a Library Book Management System that connects to a PostgreSQL database and allows users to manage book data. Your app should be able to:
- List all books in the library.
- Add a new book with a title, author, and publication year.
- Update book details by book ID.
- Retrieve book details by book ID.
Functional Requirements:
The database should have a table called
books
with the following columns:id
(serial primary key): The unique identifier for each book.title
(varchar): The title of the book.author
(varchar): The author of the book.publication_year
(integer): The year the book was published.
The Express app should support the following endpoints:
GET /books
: Returns a list of all books.POST /books
: Adds a new book with the provided title, author, and publication year.PUT /books/:id
: Updates the book details for the specified ID.GET /books/:id
: Retrieves book details for the specified ID.
Detailed Breakdown
Setting up the database connection:
- Use the
pg
npm package to connect to your PostgreSQL database. - Create a
pool.js
file to handle database connections with the appropriate connection details (username, password, database name, etc.).
- Use the
Creating the
books
table:- Ensure the
books
table exists in your PostgreSQL database with the defined schema. - Use the SQL statement
CREATE TABLE IF NOT EXISTS
to create the table if it doesn’t already exist.
- Ensure the
Listing all books:
- Implement the
GET /books
endpoint that queries thebooks
table and returns all book records in JSON format.
- Implement the
Adding a new book:
- Implement the
POST /books
endpoint that takes book information from the request body and inserts it into thebooks
table.
- Implement the
Updating book details:
- Implement the
PUT /books/:id
endpoint that updates the book details (title, author, and publication year) based on the provided book ID.
- Implement the
Retrieving book details:
- Implement the
GET /books/:id
endpoint that retrieves and returns the book details for a specific book ID.
- Implement the