-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
105 lines (73 loc) · 1.99 KB
/
index.js
File metadata and controls
105 lines (73 loc) · 1.99 KB
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
const express = require("express");
const app = express();
const bodyParser = require("body-parser")
const conn = require("./database/db");
const Pergunta = require("./database/Pergunta")
const Resposta = require("./database/Resposta")
//DATABASE
conn.authenticate().then(()=>{
console.log("Conexão feita com sucesso!")
}).catch((error)=>{
console.log(error)
})
//DIZ PARA O EXPRESS USAR O EJS COMO RENDERIZADOR
app.set("view engine","ejs");
app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended:false}))
app.use(bodyParser.json())
app.get("/",(req,res)=>{
Pergunta.findAll({raw:true,order:[
['id','DESC']
]}).then((perguntas)=>{
res.render("index",{
perguntas: perguntas
})
});
})
app.get("/perguntar",(req,res)=>{
res.render("perguntar")
})
app.post("/salvarpergunta",(req,res)=>{
var titulo = req.body.titulo
var descricao = req.body.descricao
Pergunta.create({
titulo: titulo,
descricao: descricao
}).then(()=>{
res.redirect("/")
})
})
app.get("/pergunta/:id",(req,res)=>{
var id = req.params.id
Pergunta.findOne({
where: {id: id}
}).then(pergunta =>{
if(pergunta != undefined ){
Resposta.findAll({
where: {perguntaId: pergunta.id},
raw: true,
order: [['id','DESC']]
}).then((resposta)=>{
res.render("pergunta",{
pergunta: pergunta,
resposta: resposta
})
})
}else{
res.redirect("/")
}
})
})
app.post("/salvarresposta",(req,res)=>{
var resposta = req.body.resposta
var id = req.body.id
Resposta.create({
corpo: resposta,
perguntaId: id
}).then(()=>{
res.redirect("/pergunta/"+id)
})
})
app.listen(3001,()=>{
console.log("Listening in port 3001...")
})