Backend REST API for Nusatawan project
Install all the necessary packages using NPM.
npm install
Assign value to all the necessary variable to environment variable, look at the reference in .env.dev
PORT
DATABASE_URL
JWT_SECRET
JWT_ADMIN_SECRET
SUPABASE_PROJECT_URL
SUPABASE_ANON_KEY
This project uses MySQL as a database, make sure that your MySQL service has been running.
We also uses supabase for storing images files, you have to also make sure to store all the necessary database and supabase environment variables first.
After storing all the necessary database and the supabase information, do the migration and seeding using Prisma.
npm run migrate
npm run seed
or
npm run populate-db
Then generate the prisma client so you can use prisma's query engine inside your code
npm run postinstall
After all the setup has completed, you can start the application.
npm run start
or
npm run dev
https://nusatawan-api.vercel.app
Endpoint : /auth/register
Method : POST
Request body (JSON):
username: string
email: string
password: string
phone: string
Response (JSON):
error: false,
message: "user has been successfully registered",
data: { id, username, email, password, phone, isAdmin }
token: { userToken }
Endpoint : /auth/login
Method : POST
Request body (JSON):
email: string
password: string
Response (JSON):
error: false,
message: "user1 has been successfully logged in",
data: { id, username, email, password, phone, isAdmin }
token: { userToken, adminToken }
Endpoint : /users?isAdmin=boolean
Method : GET
Request (cookies) : user-token
admin-token
Response (JSON):
error: false,
message: "Success",
data: [
{ id, username, email, password, phone, isAdmin },
{ id, username, email, password, phone, isAdmin },
]
Endpoint : /users/:id
Method : GET
Request Headers :
auth-user: `Bearer token`
auth-admin: `Bearer token`
Response (JSON):
error: false,
message: "Success",
data: { id, username, email, password, phone, isAdmin }
Endpoint : /users/role/:id
Method : PATCH
Request Headers :
auth-user: `Bearer token`
auth-admin: `Bearer token`
Request body (JSON):
isAdmin: boolean
Response (JSON):
error: false,
message: "User role has been successfully updated",
data: { id, username, email, password, phone, isAdmin }
Endpoint : /users/:id
Method : DELETE
Request Headers :
auth-user: `Bearer token`
auth-admin: `Bearer token`
Request body (JSON):
isAdmin: boolean
Response (JSON):
error: false,
message: "User has been successfully removed",
data: { id, username, email, password, phone, isAdmin }
Endpoint : /categories
Method : GET
Response (JSON):
error: false,
message: "Success",
data: [
{ name, image },
{ name, image }
]
Endpoint : /category/:name
Method : GET
Response (JSON):
error: false,
message: "Success",
data: { name, image }
Endpoint : /articles?search=string&filter=string
Method : GET
Response (JSON):
error: false,
message: "Success",
data: {
id, title, content, image, location, createdAt, categoryName, userId,
cetegory: { name, image }
}
Endpoint : /articles/:id
Method : GET
Response (JSON):
error: false,
message: "Success",
data: {
id, title, content, image, location, createdAt, categoryName, userId,
cetegory: { name, image }
user: { id, username, email, phone }
comments: []
}
Endpoint : /articles
Method : POST
Request Headers :
auth-user: `Bearer token`
Request body (form-data):
title: string
content: string
image: image(png/jpg/jpeg)
location: string
categoryName: string
userId: string
Response (JSON):
error: false,
message: "Article added successfully",
data: {
id, title, content, image, location, createdAt, categoryName, userId
}
Endpoint : /articles/:id
Method : DELETE
Request Headers :
auth-user: `Bearer token`
auth-admin: `Bearer token`
Response (form-data):
error: false,
message: "Article deleted successfully",
data: {
id, title, content, image, location, createdAt, categoryName, userId,
cetegory: { name, image }
user: { id, username, email, phone }
comments: []
}
Endpoint : /articles/:id/comments
Method : GET
Response (JSON):
error: false,
message: "Success",
data: { id, body, userId, articleId, createdAt, user }
Endpoint : /articles/:id/comments
Method : POST
Request Headers :
auth-user: `Bearer token`
Request body (JSON):
comment: string
userId: string
Response (JSON):
error: false,
message: "Successfully added comment",
data: { id, body, userId, articleId, createdAt }
Endpoint : /articles/:articleId/comments/:commentId
Method : DELETE
Request Headers :
auth-user: `Bearer token`
auth-admin: `Bearer token`
Request body (JSON):
comment: string
userId: string
Response (JSON):
error: false,
message: "Successfully deleted comment",
data: { id, body, userId, articleId, createdAt }
Endpoint : /ratings/:id
Method : GET
Response (JSON):
error: false,
message: "Success get all ratings by article id",
data: { id, rating, userId, articleId }
Endpoint : /ratings/:id/average
Method : GET
Response (JSON):
error: false,
message: "Success get average rating by article id",
data: { _avg }
Endpoint : /ratings/:id/average
Method : GET
Response (JSON):
error: false,
message: "Success",
data: { _avg }
Endpoint : /ratings/:id/total
Method : GET
Response (JSON):
error: false,
message: "Success get total user rating by article id",
data: total
Endpoint : /ratings/:id
Method : POST
Request Headers :
auth-user: `Bearer token`
Request Body (JSON):
rating: number
userId: string
Response (JSON):
error: false,
message: "Successfully added rating",
data: { id, rating, userId, articleId }
Endpoint : /campaigns
Method : GET
Response (JSON):
error: false,
message: "Success",
data: [
{
id, title, content, image, createdAt,
user: id, username
},
{
id, title, content, image, createdAt,
user: id, username
}
]
Endpoint : /campaigns/:id
Method : GET
Response (JSON):
error: false,
message: "Success",
data: {
id, title, content, image, createdAt,
user: id, username
}
Endpoint : /campaigns
Method : POST
Request Headers :
auth-user: `Bearer token`
Request body (form-data):
title: string
content: string
image: image.jpg/jpeg/png
userId: string
Response (JSON):
error: false,
message: "Campaign added successfully",
data: {
id, title, content, image, createdAt, userId
}
Endpoint : /campaigns
Method : DELETE
Request Headers :
auth-user: `Bearer token`
auth-admin: `Bearer token`
Response (JSON):
error: false,
message: "Campaign deleted successfully",
data: {
id, title, content, image, createdAt, userId
}
Endpoint : /image/articles/:file
Method : GET
Response: image/png
Endpoint : /image/campaigns/:file
Method : GET
Response: image/png
Endpoint : /image/categories/:file
Method : GET
Response: image/png