This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feedback.js
53 lines (45 loc) · 1.81 KB
/
feedback.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { responseBody } from '../helpers/base_response.js';
import { prisma } from '../index.js'
import { statusCode } from '../helpers/constant.js'
const postFeedback = async (req, res) => {
const { name, email, feedback } = req.body
try {
await prisma.feedback.create({
data: {
name, email, feedback
}
})
} catch (e) {
return res.status(statusCode.INTERNAL_SERVER_ERROR.code).send(responseBody(statusCode.INTERNAL_SERVER_ERROR.constant, e.message))
}
return res.status(statusCode.CREATED.code).send(responseBody(statusCode.CREATED.constant, 'Feedback berhasil dikirim'))
}
const getFeedback = async (req, res) => {
const skip = req.query.offset ? parseInt(req.query.offset) : 0
const take = req.query.limit ? parseInt(req.query.limit) : 10
const keyword = req.query.keyword
const where = {
feedback: {
contains: keyword,
mode: 'insensitive'
}
}
try {
const [ feedback, totalData ] = await prisma.$transaction([
prisma.feedback.findMany({
skip,
take,
orderBy: {
createdAt: 'desc'
},
where
}),
prisma.feedback.count({ where })
])
if(feedback.length === 0) return res.status(statusCode.NOT_FOUND.code).send(responseBody(statusCode.NOT_FOUND.constant, 'Tidak ada feedback ditemukan'))
return res.status(statusCode.OK.code).send(responseBody(statusCode.OK.constant, 'Feedback berhasil ditampilkan', {feedback, totalData}))
} catch(e) {
return res.status(statusCode.INTERNAL_SERVER_ERROR.code).send(responseBody(statusCode.INTERNAL_SERVER_ERROR.constant, e.message))
}
}
export { postFeedback, getFeedback }