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
userswith 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
pgnpm package to connect to your PostgreSQL database. - Create a
pool.jsfile to handle database connections with the appropriate connection details (username, password, database name, etc.).
- Use the
Creating the
userstable:- Ensure the
userstable exists in your PostgreSQL database with the defined schema. - Use the SQL statement
CREATE TABLE IF NOT EXISTSto create the table if it doesn’t already exist.
- Ensure the
Listing all users:
- Implement the
GET /usersendpoint that queries theuserstable and returns all user records in JSON format.
- Implement the
Adding a new user:
- Implement the
POST /usersendpoint that takes user information from the request body and inserts it into theuserstable.
- Implement the
Updating user information:
- Implement the
PUT /users/:idendpoint that updates the user information (name and email) based on the provided user ID.
- Implement the
Retrieving user information:
- Implement the
GET /users/:idendpoint 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
productswith 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
pgnpm package to connect to your PostgreSQL database. - Create a
pool.jsfile to handle database connections with the appropriate connection details (username, password, database name, etc.).
- Use the
Creating the
productstable:- Ensure the
productstable exists in your PostgreSQL database with the defined schema. - Use the SQL statement
CREATE TABLE IF NOT EXISTSto create the table if it doesn’t already exist.
- Ensure the
Listing all products:
- Implement the
GET /productsendpoint that queries theproductstable and returns all product records in JSON format.
- Implement the
Adding a new product:
- Implement the
POST /productsendpoint that takes product information from the request body and inserts it into theproductstable.
- Implement the
Updating product information:
- Implement the
PUT /products/:idendpoint 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/:idendpoint that removes the product with the specified ID from theproductstable.
- 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
bookswith 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
pgnpm package to connect to your PostgreSQL database. - Create a
pool.jsfile to handle database connections with the appropriate connection details (username, password, database name, etc.).
- Use the
Creating the
bookstable:- Ensure the
bookstable exists in your PostgreSQL database with the defined schema. - Use the SQL statement
CREATE TABLE IF NOT EXISTSto create the table if it doesn’t already exist.
- Ensure the
Listing all books:
- Implement the
GET /booksendpoint that queries thebookstable and returns all book records in JSON format.
- Implement the
Adding a new book:
- Implement the
POST /booksendpoint that takes book information from the request body and inserts it into thebookstable.
- Implement the
Updating book details:
- Implement the
PUT /books/:idendpoint 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/:idendpoint that retrieves and returns the book details for a specific book ID.
- Implement the