This project is a simple API and database setup that allows for the creation, retrieval, and management of superheroes and their roles. The project fulfills the following requirements:
- API Endpoints:
- A POST endpoint to add new superheroes to the database.
- A GET endpoint to retrieve superheroes and their roles.
- A GET endpoint to retrieve superheroes by their ID.
- Database:
- A SQL database with two tables:
superheroesandroles.
- A SQL database with two tables:
- Join Tables:
- A GET endpoint that retrieves superheroes with their associated roles by performing a SQL join.
- Flexible Data:
- Data stored includes superhero names, power levels, and their role (hero or villain).
-
Clone the Repository:
git clone https://github.com/TomChrister/database-ca cd database-ca -
Install Dependencies: Ensure you have Node.js installed, then run:
npm install
-
Set Up the Database:
- Use the following SQL statements to create the database tables:
CREATE TABLE roles ( id INT AUTO_INCREMENT PRIMARY KEY, role_type VARCHAR(255) NOT NULL ); CREATE TABLE superheroes ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, power INT NOT NULL, role_id INT, FOREIGN KEY (role_id) REFERENCES roles(id) );
- Populate the
rolestable:INSERT INTO roles (role_type) VALUES ('hero'), ('villain');
- Use the following SQL statements to create the database tables:
-
Start the Server:
npm start
The server will run on
http://localhost:3010by default.
Endpoint: POST /superheroes
Request Body:
{
"name": "string",
"power": "number",
"role_type": "string"
}Response:
{
"status": "success",
"id": "number"
}Endpoint: GET /superheroes
Response:
[
{
"name": "string",
"power": "number",
"role_type": "string"
}
]Endpoint: GET /superheroes/:id
Response:
{
"id": "number",
"name": "string",
"power": "number",
"role_id": "number"
}- Node.js
- Express.js
- MySQL
- Ensure you update the
.envfile with your database credentials:DB_HOST=your-database-host DB_USER=your-database-username DB_PASSWORD=your-database-password DB_NAME=your-database-name - If deploying, configure the
BASE_URLin your frontend code to point to the deployed API URL.