-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
206 lines (176 loc) · 5.89 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
const express = require('express');
const expressLayouts = require('express-ejs-layouts')
const {body, validationResult,check} = require('express-validator')
const methodOverride = require('method-override');
const session = require('express-session');
const cookieParser = require('cookie-parser');
const flash = require('connect-flash');
require('./utils/db'); // konfigurasi koneksi
const Contact = require('./model/contact'); // model
const app = express();
const port = 3000;
//set up method override
app.use(methodOverride('_method'))
//set up view engine
app.set('view engine', 'ejs');// Using set ejs
app.use(expressLayouts); // third-party-middleware
app.use(express.static('public')); // built-in-middleware
app.use(express.urlencoded({extended: true})); // built-in-middleware untuk post data kirim
//konsigarasi flash
app.use(cookieParser('secret'));
app.use(session({
cookie : {maxAge: 6000},
secret:'secret',
resave:true,
saveUninitialized:true,
}));
app.use(flash());
//view home
app.get('/', (req, res) => {
res.render('index', {
content : 'Hasbi Radifan L',
title: 'Halaman Home',
layout : 'layouts/main-layout',
});
});
//view about
app.get('/about', (req, res) => {
res.render('about', {
layout : 'layouts/main-layout',
title : 'Halaman About',
});
});
//view contact alumni
app.get('/contact', async (req,res) => {
const contacts = await Contact.find();
res.render('contact', {
title : 'Halaman Daftar Contact',
layout: 'layouts/main-layout',
contacts,
msg:req.flash('msg')
})
});
//menampilkan view Form add data
app.get('/contact/add', (req,res) => {
res.render('add-contact', {
title: 'Form Tambah Data Contact',
layout: 'layouts/main-layout',
});
});
// //process add data contact
app.post('/contact', [
body('nama').custom( async (value) => {
const duplikat = await Contact.findOne({nama: value});
if(duplikat) {
throw new Error('Nama Contact sudah terdaftar!');
}
return true; // jika namanya beda, maka dibiarkan
}),
check('lulusan', 'Lulusan tidak valid!'),
check('email', 'Email tidak valid!').isEmail(), // apabila emainya salah
check('nohp','No HP tidak valid !').isMobilePhone('id-ID'),
check('alamat','Alamat tidak valid!'),
], (req,res) => {
//tangkap dulu errors nya
const errors = validationResult(req);
if(!errors.isEmpty()) {
res.render('add-contact', {
title: 'Form Tambah Data Contact',
layout: 'layouts/main-layout',
errors: errors.array(),
});
} else {
Contact.insertMany(req.body, (error, result) => {
req.flash('msg', 'Data contact berhasil ditambahkan!'); // maka menampilkan pesan alert
res.redirect('/contact');
});
}
});
// procces hapus halaman contact
// app.get('/contact/delete/:nama', async (req,res) => {
// const contact = await Contact.findOne({nama : req.params.nama}) //mencari data kontak ingin dihapus
// //jika contact tidak ada
// if(!contact) {
// res.status(404);
// res.send('<h1>404</h1>')
// } else {
// Contact.deleteOne({_id: contact.id}).then((result) => {
// req.flash('msg', 'Data contact berhasil dihapus!'); // maka menampilkan pesan alert
// res.redirect('/contact'); // kirim ke halaman contact
// });
// }
// });
//form process delete
app.delete('/contact', (req, res) => {
Contact.deleteOne({nama : req.body.nama}).then((result) => {
req.flash('msg', 'Data contact berhasil dihapus!');
res.redirect('/contact');
});
});
//view Update
// Halaman form ubah contact
app.get('/contact/edit/:nama', async (req,res) => {
const contact = await Contact.findOne({nama : req.params.nama});
res.render('edit-contact', {
title: 'Form Ubah Data Contact',
layout: 'layouts/main-layout',
contact,
});
});
//process update data
app.put('/contact', [
body('nama').custom(async(value, {req}) => {
const duplikat = await Contact.findOne({nama: value});
if(value !== req.body.oldNama && duplikat) {
throw new Error('Nama Contact sudah terdaftar!');
}
return true;
}),
check('lulusan', 'Lulusan tidak valid!'),
check('email', 'Email tidak valid!').isEmail(),
check('nohp','No HP tidak valid !').isMobilePhone('id-ID'),
check('alamat','Alamat tidak valid!'),
], (req,res) => {
//tangkap dulu errors nya
const errors = validationResult(req);
if(!errors.isEmpty()) {
// return res.status(400).json({errors: errors.array()});
// jika error maka kembali ke halaman /contact
res.render('edit-contact', {
title: 'Form Ubah Data Contact',
layout: 'layouts/main-layout',
errors: errors.array(),
contact : req.body, // walaupun ada error, supaya tetap ada isi setiap coloumn (value)
});
} else {
Contact.updateOne({_id: req.body._id},
{
$set: {
nama: req.body.nama,
lulusan: req.body.lulusan,
email: req.body.email,
nohp: req.body.nohp,
alamat: req.body.alamat,
},
}
).then((result) =>{
req.flash('msg', 'Data contact berhasil diubah!'); // maka menampilkan pesan alert
res.redirect('/contact'); // kirim ke halaman contact
});
}
}
);
//view Detail Contact
app.get('/contact/:nama', async (req,res) => {
const contact = await Contact.findOne({ // mencari berdasarkan nama
nama: req.params.nama
})
res.render('detail', {
title : 'Halaman Detail Contact',
layout : 'layouts/main-layout',
contact,
});
});
app.listen(port, () => {
console.log(`Database App | listening at http://localhost:${port}`);
});