-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.js
84 lines (71 loc) · 2.09 KB
/
main.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
const sequelize = require("sequelize");
const Charada = require("./model/charada").Charada;
function crawl() {
const osvigaristas = require("./crawlers/osvigaristas");
osvigaristas.crawl();
}
function get_charada_aleatoria() {
return Charada.findAll({
limit: 1,
order: [sequelize.fn('RANDOM')],
attributes: {exclude: ['createdAt', 'updatedAt']}
})
.then(charadas => charadas[0])
.then(charada => charada.dataValues);
}
charada_to_html = (charada) =>
`<!DOCTYPE html>
<html lang="pt">
<head>
<title>Charadas</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
body>div {
text-align: center
}
body>div>div {
margin-top: 20px;
}
input {
display: none;
}
label p {
display: none;
}
input[type=checkbox]:checked+label p {
display: block;
}
input[type=checkbox]:checked+label div {
display: none;
}
</style>
</head>
<body>
<div>
<h1>${charada.pergunta}</h1>
<input type="checkbox" name="resposta" id="resposta"/>
<label for="resposta">
<div>Mostrar resposta!</div>
<p>${charada.resposta}</p>
</label>
<div>
<a href="javascript:window.location.reload();">Nova Charada</a>
</div>
</div>
</body>`;
function charadaAleatoria(req, res) {
let aceita = req.get('Accept');
get_charada_aleatoria().then(charada => {
if (aceita.includes('application/json')) {
res.json(charada).end();
}
res.send(charada_to_html(charada)).end()
})
}
exports.charadaAleatoria = charadaAleatoria;