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
/
agenda.js
114 lines (97 loc) · 5.07 KB
/
agenda.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { responseBody } from "../helpers/base_response.js"
import { statusCode } from "../helpers/constant.js"
import { prisma } from '../index.js'
import { toDate } from "../helpers/string_util.js"
const postAgenda = async (req, res) => {
if(!req.user.isAdmin) {
return res.status(statusCode.FORBIDDEN.code).send(responseBody(statusCode.FORBIDDEN.constant, 'User tidak diizinkan mengakses resource ini'))
}
const { name, date } = req.body
let agenda
try {
agenda = await prisma.agenda.create({
data: { name, date: toDate(date) }
})
} catch(e) {
if(e.code === 'P1001') return res.status(statusCode.INTERNAL_SERVER_ERROR.code).send(responseBody(statusCode.INTERNAL_SERVER_ERROR.constant, 'Tidak dapat meraih database'))
return res.status(statusCode.INTERNAL_SERVER_ERROR.code).send(responseBody(statusCode.INTERNAL_SERVER_ERROR.constant, e.message))
}
return res.status(statusCode.OK.code).send(responseBody(statusCode.OK.constant, 'Agenda berhasil dibuat', agenda))
}
const getAgenda = async (req, res) => {
const skip = req.query.offset ? parseInt(req.query.offset) : 0
const take = req.query.limit ? parseInt(req.query.limit) : 10
try {
const [ agenda, totalData ] = await prisma.$transaction([
prisma.agenda.findMany({
skip,
take,
orderBy: {
date: 'desc'
}
}),
prisma.agenda.count()
])
if(agenda.length === 0) return res.status(statusCode.NOT_FOUND.code).send(responseBody(statusCode.NOT_FOUND.constant, 'Tidak ada agenda untuk ditampilkan'))
return res.status(statusCode.OK.code).send(responseBody(statusCode.OK.constant, 'Agenda berhasil ditemukan', { agenda, totalData }))
} catch(e) {
if(e.code === 'P1001') return res.status(statusCode.INTERNAL_SERVER_ERROR.code).send(responseBody(statusCode.INTERNAL_SERVER_ERROR.constant, 'Tidak dapat meraih database'))
return res.status(statusCode.INTERNAL_SERVER_ERROR.code).send(responseBody(statusCode.INTERNAL_SERVER_ERROR.constant, e.message))
}
}
const getAgendaById = async (req, res) => {
let agenda
try {
agenda = await prisma.agenda.findUnique({
where: {
id: req.params.id
}
})
} catch(e) {
if(e.code === 'P1001') return res.status(statusCode.INTERNAL_SERVER_ERROR.code).send(responseBody(statusCode.INTERNAL_SERVER_ERROR.constant, 'Tidak dapat meraih database'))
return res.status(statusCode.INTERNAL_SERVER_ERROR.code).send(responseBody(statusCode.INTERNAL_SERVER_ERROR.constant, e.message))
}
if(!agenda) return res.status(statusCode.NOT_FOUND.code).send(responseBody(statusCode.NOT_FOUND.constant, 'Agenda tidak ditemukan'))
return res.status(statusCode.OK.code).send(responseBody(statusCode.OK.constant, 'Agenda berhasil ditemukan', agenda))
}
const updateAgenda = async (req, res) => {
if(!req.user.isAdmin) {
return res.status(statusCode.FORBIDDEN.code).send(responseBody(statusCode.FORBIDDEN.constant, 'User tidak diizinkan mengakses resource ini'))
}
let agenda
try {
agenda = await prisma.agenda.update({
where: {
id: req.params.id
},
data: {
name: req.body.name,
date: req.body.date ? toDate(req.body.date) : undefined
}
})
} catch(e) {
if(e.code === 'P1001') return res.status(statusCode.INTERNAL_SERVER_ERROR.code).send(responseBody(statusCode.INTERNAL_SERVER_ERROR.constant, 'Tidak dapat meraih database'))
if(e.code === 'P2025') return res.status(statusCode.NOT_FOUND.code).send(responseBody(statusCode.NOT_FOUND.constant, 'Agenda tidak ditemukan'))
return res.status(statusCode.INTERNAL_SERVER_ERROR.code).send(responseBody(statusCode.INTERNAL_SERVER_ERROR.constant, e.message))
}
return res.status(statusCode.OK.code).send(responseBody(statusCode.OK.constant, 'Agenda berhasil diupdate'))
}
const deleteAgenda = async (req, res) => {
if(!req.user.isAdmin) {
return res.status(statusCode.FORBIDDEN.code).send(responseBody(statusCode.FORBIDDEN.constant, 'User tidak diizinkan mengakses resource ini'))
}
let agenda
try {
agenda = await prisma.agenda.delete({
where: {
id: req.params.id
}
})
} catch(e) {
if(e.code === 'P1001') return res.status(statusCode.INTERNAL_SERVER_ERROR.code).send(responseBody(statusCode.INTERNAL_SERVER_ERROR.constant, 'Tidak dapat meraih database'))
if(e.code === 'P2025') return res.status(statusCode.NOT_FOUND.code).send(responseBody(statusCode.NOT_FOUND.constant, 'Agenda tidak ditemukan'))
return res.status(statusCode.INTERNAL_SERVER_ERROR.code).send(responseBody(statusCode.INTERNAL_SERVER_ERROR.constant, e.message))
}
return res.status(statusCode.OK.code).send(responseBody(statusCode.OK.constant, 'Agenda berhasil dihapus'))
}
export { postAgenda, getAgenda, getAgendaById, updateAgenda, deleteAgenda }