-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
175 lines (147 loc) · 3.92 KB
/
app.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
const express = require('express')
const expressLayoouts = require('express-ejs-layouts')
const {
loadContact,
uniqueCheck,
storeContact,
showContact,
updateContact,
deleteContact
} = require('./utils/contacts')
const {
query,
validationResult,
check
} = require('express-validator');
const session = require('express-session')
const cookieParser = require('cookie-parser')
const flash = require('connect-flash')
const app = express()
const port = 3000
app.set('view engine', 'ejs')
app.use(expressLayoouts)
app.use(express.static('public'))
// Parse URL-encoded
app.use(express.urlencoded({ extended: true }))
app.use(cookieParser('secret'))
app.use(session({
cookie: { maxAge: 6000 },
secret: 'secret',
resave: true,
saveUninitialized: true
}))
app.use(flash())
app.get('/', (req, res) => {
res.render('home', {
layout: 'layouts/main',
title: 'Home',
})
})
// Contact
// Index
app.get('/contact', (req, res) => {
const contacts = loadContact()
res.render('contact/index', {
layout: 'layouts/main',
title: 'Kontak',
contacts,
msg: req.flash('msg'),
})
})
// Create
app.get('/contact/create', (req, res) => {
res.render('contact/create', {
layout: 'layouts/main',
title: 'Tambah Kontak',
})
})
// Store
app.post('/contact', [
query('nama').custom((value, { req }) => {
const duplikat = uniqueCheck(req.body.nama)
if (duplikat) {
throw new Error('Nama sudah terdaftar')
}
return true
}),
check('email', 'Email tidak valid').isEmail(),
check('telepon', 'Nomor telepon tidak valid').isMobilePhone('id-ID'),
], (req, res) => {
const result = validationResult(req)
if (result.isEmpty()) {
storeContact(req.body)
req.flash('msg', 'Data kontak berhasil ditambahkan')
res.redirect('/contact')
} else {
res.render('contact/create', {
layout: 'layouts/main',
title: 'Tambah Kontak',
errors: result.array(),
})
}
})
// Delete
app.get('/contact/delete/:nama', (req, res) => {
const contact = showContact(req.params.nama)
if (!contact) {
res.status(404)
res.send('<h1>404 Not Found</h1>')
} else {
deleteContact(req.params.nama)
req.flash('msg', 'Data kontak berhasil dihapus')
res.redirect('/contact')
}
})
// Edit
app.get('/contact/edit/:nama', (req, res) => {
const contact = showContact(req.params.nama)
res.render('contact/edit', {
layout: 'layouts/main',
title: `Edit Kontak: ${req.params.nama}`,
contact,
})
})
// Update
app.post('/contact/update', [
query('nama').custom((value, { req }) => {
const duplikat = uniqueCheck(req.body.nama)
const result = req.body.nama !== req.body.id && duplikat
if (result) {
throw new Error('Nama sudah terdaftar')
}
return true
}),
check('email', 'Email tidak valid').isEmail(),
check('telepon', 'Nomor telepon tidak valid').isMobilePhone('id-ID'),
], (req, res) => {
const result = validationResult(req)
if (result.isEmpty()) {
updateContact(req.body)
req.flash('msg', 'Data kontak berhasil diedit')
res.redirect('/contact')
} else {
res.render('contact/edit', {
layout: 'layouts/main',
title: 'Edit Kontak',
errors: result.array(),
contact: req.body
})
}
})
// Show
app.get('/contact/:nama', (req, res) => {
const contact = showContact(req.params.nama)
res.render('contact/detail', {
layout: 'layouts/main',
title: `Detail Kontak: ${req.params.nama}`,
contact,
})
})
// End Contact
app.use((req, res) => {
res.status(404)
res.send('<h1>Page Not Found</h1>')
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})