Skip to content

Commit

Permalink
Merge branch 'main' into lureus/feat-response-status
Browse files Browse the repository at this point in the history
  • Loading branch information
EL5225 authored Nov 15, 2024
2 parents 87fb61e + 75bb24f commit bc88c96
Show file tree
Hide file tree
Showing 19 changed files with 139 additions and 119 deletions.
4 changes: 2 additions & 2 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"semi": false,
"singleQuote": true,
"singleQuote": false,
"useTabs": false,
"tabWidth": 2
"tabWidth": 1
}
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable prettier/prettier */
import globals from 'globals'
import pluginJs from '@eslint/js'
// import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended'
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"swagger-ui-express": "^5.0.1",
"zod": "^3.23.8"
},

"devDependencies": {
"@eslint/js": "^9.14.0",
"eslint": "^9.14.0",
Expand Down
2 changes: 1 addition & 1 deletion src/database.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { PrismaClient } from '@prisma/client'
import { PrismaClient } from "@prisma/client"

export const client = new PrismaClient()
1 change: 1 addition & 0 deletions src/docs/swagger.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

import swaggerJSDoc from 'swagger-jsdoc'

const swaggerDefinition = {
Expand Down
7 changes: 4 additions & 3 deletions src/handler/auth/registering-new-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { registerSchema } from '../../schemas/auth/registerSchema.js'
import { register } from '../../service/auth.js'
import { responseStatus } from '../../utils/response.js'


/**
* @swagger
* /api/v1/auth/register:
Expand Down Expand Up @@ -71,10 +72,10 @@ import { responseStatus } from '../../utils/response.js'
* @param {import("express").NextFunction} next
*/
export default async function registeringNewUser(req, res, next) {
try {
const data = registerSchema.parse(req.body)
try {
const data = registerSchema.parse(req.body)

await register(data)
await register(data)

return res.json({
status: responseStatus.OK,
Expand Down
27 changes: 15 additions & 12 deletions src/handler/kamus/create-kamus-data.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createKamusOnDB } from '../../service/kamus.js'
import { responseStatus } from '../../utils/response.js'


/**
* @swagger
* /api/v1/kamus:
Expand Down Expand Up @@ -81,13 +82,14 @@ import { responseStatus } from '../../utils/response.js'
* @param {import("express").NextFunction} next
*/
export default async function createKamus(req, res, next) {
try {
/**
* @typedef CreateKamus
* @type {object}
* @property {string} word
* @property {string} wordDescription
*/
try {
/**
* @typedef CreateKamus
* @type {object}
* @property {string} word
* @property {string} wordDescription
*/


/** @type {CreateKamus} */
const { word, wordDescription } = req.body
Expand All @@ -99,11 +101,11 @@ export default async function createKamus(req, res, next) {
message: 'word atau description tidak boleh kosong',
})
}

const kamus = await createKamusOnDB({
word,
wordDescription,
})
const kamus = await createKamusOnDB({
word,
wordDescription,
})

return res.status(201).json({
status: responseStatus.CREATED,
Expand All @@ -113,4 +115,5 @@ export default async function createKamus(req, res, next) {
} catch (error) {
next(error)
}

}
13 changes: 7 additions & 6 deletions src/handler/kamus/delete-kamus-data.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { deleteKamusOnDB } from '../../service/kamus.js'
import { responseStatus } from '../../utils/response.js'


/**
* @swagger
* /api/v1/kamus/{id}:
Expand Down Expand Up @@ -53,19 +54,19 @@ import { responseStatus } from '../../utils/response.js'
* @param {import("express").NextFunction} next
*/
export default async function deleteKamus(req, res, next) {
try {
const { id } = req.params

// Delete Kamus entry
await deleteKamusOnDB(id)
try {
const { id } = req.params

// Delete Kamus entry
await deleteKamusOnDB(id)
// Respond with success message
return res.status(200).json({
return res.status(200).json({
status: responseStatus.DELETED,
message: 'Kamus berhasil dihapus',
})
} catch (error) {
// Handle errors
next(error)
}

}
1 change: 1 addition & 0 deletions src/handler/kamus/find-kamus-by-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { findKamusById } from '../../service/kamus.js'
import { Prisma } from '@prisma/client'
import { responseStatus } from '../../utils/response.js'


/**
* @swagger
* /api/v1/kamus/{id}:
Expand Down
17 changes: 13 additions & 4 deletions src/handler/kamus/get-all-kamus-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ import { responseStatus } from '../../utils/response.js'
* @returns
*/
export default async function getAllKamusData(req, res, next) {
try {
const query = req.query.q
let kamusData

try {
const query = req.query.q
let kamusData
if (query) {
kamusData = await findAllKamusDataBy(query)
} else {
Expand All @@ -73,4 +73,13 @@ export default async function getAllKamusData(req, res, next) {
} catch (error) {
next(error)
}

return res.json({
status: "OK",
data: kamusData,
message: "Data kamus berhasil diambil",
})
} catch (error) {
next(error)
}
}
11 changes: 5 additions & 6 deletions src/handler/kamus/update-kamus-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,9 @@ import { responseStatus } from '../../utils/response.js'
* @param {import("express").NextFunction} next
*/
export default async function updateKamus(req, res, next) {
try {
const { id } = req.params
const { word, wordDescription } = req.body

try {
const { id } = req.params
const { word, wordDescription } = req.body
// Validate request body
if (!word || !wordDescription) {
return res.status(400).json({
Expand All @@ -110,9 +109,9 @@ export default async function updateKamus(req, res, next) {
})
}

// Update Kamus entry
const updatedKamus = await updateKamusOnDB(id, { word, wordDescription })

// Update Kamus entry
const updatedKamus = await updateKamusOnDB(id, { word, wordDescription })
// Respond with updated Kamus entry
return res.status(200).json({
status: responseStatus.OK,
Expand Down
7 changes: 5 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

import express from 'express'
import { kamusRoute } from './routes/kamus.js'
import { cors } from './middleware/cors.js'
Expand All @@ -12,6 +13,7 @@ app.use(express.json())
app.use(cors)

app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec))

/**
* TODO :
* Untuk saat ini hanya endpoint berikut yang perlu dibuat
Expand All @@ -21,14 +23,15 @@ app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec))
*
*/

app.use('/api/v1/kamus', kamusRoute)
app.use("/api/v1/kamus", kamusRoute)

/**
* Buatin error handlingnya rek
* middlewarenya buat di ./middleware
* o iya sekalian buatin handling 404
*/

app.use('*', pathNotFound)
app.use(serverError)

app.listen(PORT, '0.0.0.0', () => console.log(`Listening ${PORT}`))
app.listen(PORT, "0.0.0.0", () => console.log(`Listening ${PORT}`))
14 changes: 7 additions & 7 deletions src/middleware/verify-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import * as jose from 'jose'
import { responseStatus } from '../utils/response'

export async function verifyToken(req, res, next) {
try {
const token = req.headers.authorization?.replace('Bearer ', '')
try {
const token = req.headers.authorization?.replace("Bearer ", "")

if (!token) {
if (!token) {
return res.status(401).json({ status: responseStatus.UNAUTHORIZED })
}
}

// TODO: Check if token is blacklisted
// TODO: Check if token is blacklisted

const secret = new TextEncoder().encode(process.env.JWT_SECRET)
const secret = new TextEncoder().encode(process.env.JWT_SECRET)

const { payload } = await jose.jwtVerify(token, secret)
const { payload } = await jose.jwtVerify(token, secret)

req.user = payload
next()
Expand Down
10 changes: 5 additions & 5 deletions src/routes/auth.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Router } from 'express'
import authenticateSession from '../handler/auth/authenticate-session.js'
import registeringNewUser from '../handler/auth/registering-new-user.js'
import { Router } from "express"
import authenticateSession from "../handler/auth/authenticate-session.js"
import registeringNewUser from "../handler/auth/registering-new-user.js"

export const authRoute = Router()

authRoute.post('/login', authenticateSession)
authRoute.post('/register', registeringNewUser)
authRoute.post("/login", authenticateSession)
authRoute.post("/register", registeringNewUser)
6 changes: 3 additions & 3 deletions src/routes/kamus.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import findKamusId from '../handler/kamus/find-kamus-by-id.js'
export const kamusRoute = Router()

// Route to get all Kamus data
kamusRoute.get('/', getAllKamusData)
kamusRoute.get("/", getAllKamusData)

// Route to create a new Kamus entry
kamusRoute.post('/', createKamus)
kamusRoute.post("/", createKamus)

// Route to update an existing Kamus entry
kamusRoute.put('/:id', updateKamus)
kamusRoute.put("/:id", updateKamus)

// Route to delete an existing Kamus entry
kamusRoute.delete('/:id', deleteKamus)
Expand Down
6 changes: 3 additions & 3 deletions src/schemas/auth/loginSchema.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { z } from 'zod'
import { z } from "zod"

/**
* @typedef {z.infer<typeof loginSchema>} Login
*/
export const loginSchema = z.object({
email: z.string().email('Email tidak valid'),
password: z.string('Password wajib di isi').trim(),
email: z.string().email("Email tidak valid"),
password: z.string("Password wajib di isi").trim(),
})
10 changes: 5 additions & 5 deletions src/schemas/auth/registerSchema.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { z } from 'zod'
import { z } from "zod"

/**
* @typedef {z.infer<typeof registerSchema>} Register
*/
export const registerSchema = z.object({
email: z.string().email('Email tidak valid'),
username: z.string().trim().min(1),
fullname: z.string().trim().min(1),
password: z.string('Password wajib di isi').trim().min(1),
email: z.string().email("Email tidak valid"),
username: z.string().trim().min(1),
fullname: z.string().trim().min(1),
password: z.string("Password wajib di isi").trim().min(1),
})
Loading

0 comments on commit bc88c96

Please sign in to comment.