-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
115 lines (93 loc) · 3.1 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
const express = require('express')
const exphbs = require('express-handlebars')
const app = express()
const conn = require('./db/conn')
const User = require('./model/User')
const Address = require('./model/Address')
app.use(
express.urlencoded({
extended: true,
}),
)
app.use(express.json())
app.engine('handlebars', exphbs())
app.set('view engine', 'handlebars')
app.use(express.static('public'))
app.get('/', async (_request, response) => {
//raw facilita o uso em trazer os dados de forma mais simples,
//sendo mais facil de trabalhar com o handlebars
const users = await User.findAll({raw: true}) // SELECT * FROM
response.render('home',{ users: users})} //passa disponibilidade pelo engine para jogar em home
)
app.get('/users/create', (_request, response) => {
response.render('adduser')})
app.post('/users/create', async (request, response) => {
// requisitando o dados da page
const name = request.body.name
const occupation = request.body.occupation
let newsletter = request.body.newsletter //on
if(newsletter === 'on') {
newsletter = true
}
// criando valores para tabela user
await User.create({name, occupation, newsletter})
// console.log(request.body)
//redicionar para home
response.redirect('/')
})
app.get('/users/:id', async (request, response) => {
const id = request.params.id // id da url
const user = await User.findOne({raw: true, where: { id: id }})
response.render('userview',{ user })
})
app.post('/users/delete/:id', async(request, response) => {
const id = request.params.id
await User.destroy({ where: { id: id } }) // querie DELETE
response.redirect('/')
})
app.get('/users/edit/:id', async (request, response) => { // get para o useredit
const id = request.params.id
try{
const user = await User.findOne({ include: Address, where: {id: id} })
response.render('useredit', { user: user.get({ plain: true }) })
}
catch(error) {
console.log(error)
}
})
app.post('/users/update', async(request, response) => {
const id = request.body.id
const name = request.body.name
const occupation = request.body.occupation
let newsletter = request.body.newsletter
if (newsletter === 'on') { newsletter = true }
else { newsletter = false }
const data = { id, name, occupation, newsletter, }
await User.update(data, { where: {id: id } })
.then((user) => {console.log(user)
response.redirect('/')
})
.catch((error) => console.log(error))
})
app.post('/address/create', async (request, response) => {
const UserId = request.body.UserId
const street = request.body.street
const number = request.body.number
const city = request.body.city
const data = { UserId, street, number, city }
await Address.create(data)
response.redirect(`/users/edit/${UserId}`)
})
app.post('/address/delete', (request, response) => {
const UserId = request.body.UserId
const id = request.body.id
Address.destroy({
where: {id: id}
})
response.redirect(`/users/edit/${UserId}`)
})
// aplicação é inicializada sincrona junto com a tabela
conn.sync().then(() => {
app.listen(3001)
})
.catch((err) => console.log(err))