Hello everyoneđź‘‹,
I created an Tokopedia Play API (Backend) for my GoTo Impact Foundation mid term assignment. In this project i use Express JS and MongoDB for the Database. I created readme file for flow API process, Database Schema, and how to run this project.
Database: tokopedia_play_db
Colletions:
-
videos
: This collection will store information about each video data.document structure
{ _id: <ObjectID>, title: string, thumbnail_url: string video_url: string, createdAt: Date, updatedAt: Date }
-
products
: This collection will store information about each product data.document structure
{ _id: <ObjectID>, title: string, product_url: string, price: Number, video_id: <ObjectID>, createdAt: Date, updatedAt: Date }
-
comments
: This collection will store information about each comment data.document structure
{ _id: <ObjectID>, username: string, comment: string, video_id: string, createdAt: Date, updatedAt: Date }
Flow from request to get response.
POST /videos
request -> routes -> controller [create()] -> services [create()] -> repository [create()] -> response
GET /videos
request -> routes -> controller [findAll()] -> services [findAll()] -> repository [findAll()] -> response
GET /videos/:id
request -> routes -> controller [findById()] -> services [findById()] -> repository [findById()] -> response
POST /products
request -> routes -> controller [create()] -> services [create()] -> repository [create()] -> response
GET /products/:id
request -> routes -> controller [findById()] -> services [findById()] -> repository [findById()] -> response
GET /videos/:video_id/products
request -> routes -> controller [findByVideoId()] -> services [findByVideoId()] -> repository [findByVideoId()] -> response
POST /comments
request -> routes -> controller [create()] -> services [create()] -> repository [create()] -> response
GET /comments/:id
request -> routes -> controller [findById()] -> services [findById()] -> repository [findById()] -> response
GET /videos/:video_id/comments
request -> routes -> controller [findByVideoId()] -> services [findByVideoId()] -> repository [findByVideoId()] -> response
Create a new video and return the new object
-
URL Params
None
-
Headers
Content-Type: application/json
-
Body
{
"title" : "string",
"video_url" : "string",
"thumbnail_url" : "string"
}
- Success Response
- Code: 201 (CREATED)
- Content:
{
"title": "string",
"video_url": "string",
"thumbnail_url": "string",
"_id": <ObjectID>,
"createdAt": "Date",
"updatedAt": "Date",
}
Returns all videos in the system.
-
URL Params
None
-
Headers
Content-Type: application/json
-
Body
None
-
Success Response
- Code: 200 (OK)
- Content:
[
{...},
{...},
{
"title": "string",
"video_url": "string",
"thumbnail_url": "string",
"_id": <ObjectID>,
"createdAt": "Date",
"updatedAt": "Date",
},
]
Returns the specified videos.
-
URL Params
Required:
id=[integer]
-
Headers
Content-Type: application/json
-
Body
None
-
Success Response
- Code: 200 (OK)
- Content:
{
"title": "string",
"video_url": "string",
"thumbnail_url": "string",
"_id": <ObjectID>,
"createdAt": "Date",
"updatedAt": "Date",
}
- Error Response
- Code: 404 (NOT FOUND)
- Content:
{ "error": "video with id : <id> not found" }
Create a new product and return the new object
-
URL Params
None
-
Headers
Content-Type: application/json
-
Body
{
"title": "string",
"product_url": "string",
"price": Number,
"video_id": <ObjectID>
}
- Success Response
- Code: 201 (CREATED)
- Content:
{
"_id": <ObjectID>,
"title": "string",
"product_url": "string",
"price": Number,
"video_id": <ObjectID>,
"createdAt": "Date",
"updatedAt": "Date",
}
Returns the specified product by video id params.
-
URL Params
Required:
video_id=[integer]
-
Headers
Content-Type: application/json
-
Body
None
-
Success Response
- Code: 200 (OK)
- Content:
{
"_id": <ObjectID>,
"title": "string",
"product_url": "string",
"price": Number,
"video_id": <ObjectID>,
"createdAt": Date,
"updatedAt": Date,
"__v": 0
},
- Error Response
- Code: 404 (NOT FOUND)
- Content:
{ error: "can't find the video id" }
Create a new comments and return the new object
-
URL Params
None
-
Headers
Content-Type: application/json
-
Body
{
"username": "string",
"comment": "string"
}
- Success Response
- Code: 201 (CREATED)
- Content:
{
"status": "success"
}
- Error Response
- Code: 400 (NOT FOUND)
- Content:
{ "status": "error"}
Returns the specified comment by video id params.
-
URL Params
Required:
video_id=[integer]
-
Headers
Content-Type: application/json
-
Body
None
-
Success Response
- Code: 200 (OK)
- Content:
[
{...},
{...},
{
"_id": <ObjectID>,
"username": "string",
"comment": "string",
"video_id": <ObjectID>,
"createdAt": Date,
"updatedAt": Date,
},
]
- Error Response
- Code: 404 (NOT FOUND)
- Content:
{ error: "can't find the video id" }
To run this server API, you have to follow instruction below:
- Clone this github, run this command line:
git clone https://github.com/linothomas14/tokopedia_play_API.git
- Open your code editor and accessing the project folder.
- Install dependencies on project by running this command line:
npm install
. Make sure you are running in exact same folder project. - Check your mongodb compass or mongodb shell, you can check is it your database doing well or not.
- If you're using mongodb shell just run
mongosh
in the terminal to check. If you're using mongodb compass, you can run the app and connect into the database. - Back to the project folder and rename .env-examples file into .env and do the instruction inside the file for configuration MongoDB connection Host.
- Now you can run the server by following this command line:
npm run dev
. - After make sure the server not getting any error, open Postman or other app to test the API.
- If you are using Postman, you can import the tokopedia-play-api.postman_collection.json file on postman.
- Configure the request to test if you are not using Postman, if you are using postman, you can jump into next step.
- Finally, you can test the API by hit the request as much as you want.