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
/
media.js
110 lines (90 loc) · 4.43 KB
/
media.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
import { responseBody } from "../helpers/base_response.js"
import { statusCode } from "../helpers/constant.js"
import { prisma } from '../index.js'
const getMedia = 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 [ media, totalData ] = await prisma.$transaction([
prisma.media.findMany({
skip,
take,
orderBy: {
createdAt: 'desc'
}
}),
prisma.media.count()
])
if(media.length === 0) return res.status(statusCode.NOT_FOUND.code).send(responseBody(statusCode.NOT_FOUND.constant, 'Tidak ada media untuk ditampilkan'))
return res.status(statusCode.OK.code).send(responseBody(statusCode.OK.constant, 'Media berhasil ditampilkan', { media, 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 getMediaById = async (req, res) => {
const media = await prisma.media.findUnique({
where: {
id: req.params.id
}
})
if(!media) return res.status(statusCode.NOT_FOUND.code).send(responseBody(statusCode.NOT_FOUND.constant, 'Media tidak ditemukan'))
return res.status(statusCode.OK.code).send(responseBody(statusCode.OK.constant, 'Media berhasil ditampilkan', { media }))
}
const postMedia = 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 { link, description } = req.body
let media
try {
media = await prisma.media.create({
data: {
link, description
}
})
} 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, 'Media created', media))
}
const updateMedia = 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 description = req.body.description
const link = req.body.link
try {
await prisma.media.update({
where: {
id: req.params.id
},
data: {
description, link
}
})
} catch(e) {
if(e.code === 'P2025') return res.status(statusCode.NOT_FOUND.code).send(responseBody(statusCode.NOT_FOUND.constant, 'Berita tidak ditemukan'))
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, 'Media updated'))
}
const deleteMedia = async (req, res) => {
if(!req.user.isAdmin) {
return res.status(statusCode.FORBIDDEN.code).send(errorResponseBody(statusCode.FORBIDDEN.constant, 'User tidak diizinkan mengakses resource ini'))
}
try {
await prisma.media.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, 'Media 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, 'Media berhasil dihapus'))
}
export { getMedia, getMediaById, postMedia, updateMedia, deleteMedia }