-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
142 lines (103 loc) · 3.11 KB
/
index.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
const exphbs = require('express-handlebars');
const express = require('express');
const conn = require('./db/conn');
const path = require('path');
const app = express();
const User = require('./models/Users') // será mapeado automaticamente pelo sync() e criará a tabela User
const Address = require('./models/Adress')
//Handlebars
app.set('view engine' , 'handlebars');
app.set('views', path.join(__dirname, 'views'));
app.engine('handlebars' , exphbs.engine({
extname:'handlebars' ,
defaultLayout : 'main' ,
layoutsDir: path.join(__dirname + '/views/layouts'),
}))
//Receber Respostas do Body
app.use(
express.urlencoded({
extended:true
})
)
app.use(express.json())
// Config a pasta Public
app.use(express.static(__dirname + '/public'));
app.get("/users/create" , (req , res) =>{
res.render("adduser")
})
app.post('/users/create' , async (req , res)=>{
const name = req.body.name;
const occupation = req.body.occupation;
let newsletter = req.body.newsletter;
if(newsletter === 'on'){
newsletter = true
} else {
newsletter = false
}
await User.create({
name , occupation , newsletter
})
res.redirect('/')
})
app.get('/users/:id' , async (req , res)=> {
const id = req.params.id ;
const user = await User.findOne( {
raw: true , where: { id : id}})
res.render('userview' , { user })
})
app.get("/" , async (req , res) =>{
const user = await User.findAll({raw : true})
res.render("home" , {users : user} )
})
app.post('/users/delete/:id' , async (req , res )=>{
const id = req.params.id;
await User.destroy({where: {id: id} });
res.redirect('/')
})
app.get('/users/edit/:id' , async (req , res )=>{
try {
const id = req.params.id;
const user = await User.findOne({ include: Address , where: {id: id} });
res.render('useredit' , { user : user.get({plain : true}) })
}catch( err ) {
console.log(err)
}
})
app.post('/users/update' , async (req , res) => {
const id = req.body.id ;
const name = req.body.id;
const occupation = req.body.occupation ;
let newsletter = req.body.newsletter ;
if(newsletter === 'on'){
newsletter = true
}else {
newsletter = false }
const userData = {
id , name, occupation , newsletter
}
await User.update(userData , { where : {id : id}})
res.redirect('/')
})
app.post('/address/create' , async (req , res )=> {
const UserId = req.body.UserId;
const street = req.body.street;
const number = req.body.number;
const city = req.body.city;
const address = {
UserId , street , number , city
}
await Address.create(address);
res.redirect(`/users/edit/${UserId}`);
})
app.post('/address/delete' , async (req , res)=>{
const UserId = req.body.UserId;
const id = req.body.id;
await Address.destroy({
where: {id : id}
})
res.redirect(`/users/edit/${UserId}`);
})
conn.sync().then(()=> {
app.listen(3001)
}).catch((err) => console.log(err))
//Perceba que o sync() não irá funcionar se houver um sequelize.authenticate() no conn.js