This is a REST API for a portfolio publication site where users can sign up, create portfolios, upload images, and manage their profiles. It supports features such as viewing image feeds, logging in/out, and deleting profiles, portfolios, and images. The API is built using Node.js, Express.js as a framework and utilizes PostgreSQL database with Sequelize as an ORM.
-
Clone the Repository
git clone https://github.com/AlexRazor1337/portfolio-api.git cd portfolio-api
-
Install Dependencies
npm install
-
Set Up PostgreSQL Database
Run the following command to start a PostgreSQL container OR setup it manually:
docker run --name portfolio-pg --rm -e POSTGRES_USER=portfolio -e POSTGRES_PASSWORD=examplepass -e PGDATA=/var/lib/postgresql/data/pgdata -v /tmp:/var/lib/postgresql/data -p 5432:5432 -it postgres:14.1-alpine
-
Set Up Environment Variables
Create an
.env
file in the root directory of the project and populate it with the following content:APP_PORT=3000 DB_USER=portfolio DB_PASS=examplepass DB_NAME=portfolio-db DB_HOST=localhost JWT_SECRET=jwtsecret
Adjust the values according to your preferences or existing database configuration.
-
Set Up Database tables
To set up the database, run the following commands:
npx sequelize-cli db:create npx sequelize-cli db:migrate
This will create the database and all the necessary tables.
-
Start the API
Finally, start the API server by running the following command:
npm run start
The server will start running on the specified port and you can access the API endpoints.
Authorization is done using JWT tokens. To access the protected routes, you need to provide the token in the Authorization
header of the request with Bearer
prefix.
POST /api/auth/signup
Description: This route is used to sign up a new user.
Is Auth required?: No
Request data:
body:
{
"email": "test@mail.com",
"password": "examplepass",
"confirmPassword": "examplepass"
}
Response:
{
"id": 2,
"email": "test@mail.com",
"updatedAt": "2023-06-11T11:54:59.606Z",
"createdAt": "2023-06-11T11:54:59.606Z"
}
POST /api/auth/login
Description: This route is used to log in a user.
Is Auth required?: No
Request data:
body:
{
"email": "test@mail.com",
"password": "examplepass"
}
Response:
{
"id": 2,
"email": "test@mail.com",
"createdAt": "2023-06-11T11:54:59.606Z",
"updatedAt": "2023-06-11T11:54:59.606Z",
"token": "jwt_token"
}
POST /api/auth/logout
Description: This route is used to log out the currently authenticated user.
Is Auth required?: Yes
Request data: None
Response:
{
"message": "Logged out"
}
GET /api/profile
Description: This route is used to retrieve the user's profile information.
Is Auth required?: Yes
Request data: None
Response:
{
"id": 2,
"email": "test@mail.com",
"createdAt": "2023-06-11T11:54:59.606Z",
"updatedAt": "2023-06-11T11:54:59.606Z"
}
DELETE /api/profile
Description: This route is used to delete the user's profile.
Is Auth required?: Yes
Request data: None
{
"message": "Profile deleted"
}
GET /api/portfolio
Description: This route is used to get all your portfolios.
Is Auth required?: Yes
Request data: None
Response:
{
"data": [
{
"id": 1,
"name": "Portfolio 1",
"description": "my cool portfolio",
"createdAt": "2023-06-11T11:55:29.044Z",
"updatedAt": "2023-06-11T11:55:29.044Z",
"userId": 2
}
]
}
GET /api/portfolio/:id
Description: This route is used to get information about a specific portfolio.
Is Auth required?: Yes
Request data:
params:
- id
Response:
{
"id": 1,
"name": "Portfolio 1",
"description": "my cool portfolio",
"createdAt": "2023-06-11T11:55:29.044Z",
"updatedAt": "2023-06-11T11:55:29.044Z",
"userId": 2,
"Images": [
{
"id": 2,
"name": "my image",
"description": "this is my image",
"filename": "4512359e-4a35-4e9b-bf45-9251ce7ea33c.jpeg",
"createdAt": "2023-06-11T21:33:44.138Z",
"updatedAt": "2023-06-11T21:33:44.138Z",
"url": "/images/4512359e-4a35-4e9b-bf45-9251ce7ea33c.jpeg"
}
]
}
POST /api/portfolio
Description: This route is used to create a new portfolio.
Is Auth required?: Yes
Request data:
body:
{
"name": "Portfolio 1",
"description": "my cool portfolio"
}
Response:
{
"id": 1,
"userId": 2,
"name": "Portfolio 1",
"description": "my cool portfolio",
"updatedAt": "2023-06-11T11:55:29.044Z",
"createdAt": "2023-06-11T11:55:29.044Z"
}
DELETE /api/portfolio/:id
Description: This route is used to delete a specific portfolio.
Is Auth required?: Yes
Request data:
params:
- id
Response:
{
"message": "Portfolio deleted"
}
POST /api/images
Description: This route is used to create a new image.
Is Auth required?: Yes
Request data:
multipart/form-data:
- portfolioId: "1"
- name: my image
- description: this is my image
- image: [file]
Response:
{
"id": 1,
"portfolioId": 1,
"name": "my image",
"description": "this is my image",
"filename": "2a2472b6-a526-4adb-8e8a-ffcfe3580393.jpeg",
"updatedAt": "2023-06-11T11:55:55.179Z",
"createdAt": "2023-06-11T11:55:55.179Z"
}
GET /api/images/:id
Description: This route is used to get information about a specific image.
Is Auth required?: No
Request data:
params:
- id
Response:
{
"id": 1,
"name": "my image",
"description": "this is my image",
"filename": "2a2472b6-a526-4adb-8e8a-ffcfe3580393.jpeg",
"createdAt": "2023-06-11T11:55:55.179Z",
"updatedAt": "2023-06-11T11:55:55.179Z",
"portfolioId": 1,
"Portfolio": {
"id": 1,
"name": "Portfolio 1"
},
"Comments": [],
"url": "/images/2a2472b6-a526-4adb-8e8a-ffcfe3580393.jpeg"
}
DELETE /api/images/:id
Description: This route is used to delete a specific image.
Is Auth required?: Yes
Request data:
params:
- id
Response:
{
"message": "Image deleted!"
}
GET /api/feed
Description: This route is used to get the feed of images. It contains all the images from all the portfolios, sorted by date. Supports pagination.
Is Auth required?: No
Request data:
Query:
- offset
- limit
Response:
{
"data": [
{
"id": 1,
"name": "my image",
"description": "this is my image",
"filename": "2a2472b6-a526-4adb-8e8a-ffcfe3580393.jpeg",
"createdAt": "2023-06-11T11:55:55.179Z",
"updatedAt": "2023-06-11T11:55:55.179Z",
"Portfolio": {
"name": "Portfolio 1"
},
"Comments": [
{
"id": 1,
"text": "nice image",
"createdAt": "2023-06-11T11:56:06.563Z",
"updatedAt": "2023-06-11T11:56:06.563Z",
"User": {
"id": 2,
"email": "test@mail.com"
}
}
],
"url": "/images/2a2472b6-a526-4adb-8e8a-ffcfe3580393.jpeg"
}
]
}
POST /api/comments
Description: This route is used to create a new comment for an image.
Is Auth required?: Yes
Request data:
body:
{
"imageId": 1,
"text": "nice image"
}
Response:
{
"id": 1,
"imageId": 1,
"userId": 2,
"text": "nice image",
"updatedAt": "2023-06-11T11:56:06.563Z",
"createdAt": "2023-06-11T11:56:06.563Z"
}
DELETE /api/comments/:id
Description: This route is used to delete a specific comment.
Is Auth required?: Yes
Request data:
params:
- id
Response:
{
"message": "Comment deleted"
}