-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroups.js
More file actions
151 lines (134 loc) · 4.19 KB
/
groups.js
File metadata and controls
151 lines (134 loc) · 4.19 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
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
const express = require('express');
const app = express.Router();
const init = connection => {
app.use((req, res, next) => {
if(!req.session.user) {
res.redirect('/');
} else {
next();
};
});
// Rota Raiz do Projeto.
// Metodo GET
app.get('/', async(req, res) => {
const [ groups, fields ] = await connection.execute('select groups.*, groups_users.role from groups left join groups_users on groups.id = groups_users.group_id and groups_users.user_id = ?', [
req.session.user.id,
]);
res.render('groups', {
groups
});
});
// Lista grupos do usuário logado
app.get('/:id', async(req, res) => {
const [ group ] = await connection.execute('select groups.*, groups_users.role from groups left join groups_users on groups_users.group_id = groups.id and groups_users.user_id = ? where groups.id = ?', [
req.session.user.id,
req.params.id
]);
const [pendings] = await connection.execute('select groups_users.*, users.name from groups_users inner join users on groups_users.user_id = users.id and groups_users.role like "pending" and groups_users.group_id = ?', [
req.params.id
]);
const [games] = await connection.execute(`
SELECT
games.*,
guessings.result_a as guess_a,
guessings.result_b as guess_b,
guessings.score,
(select flag_img from teams where name=games.team_a) as flag_team_a,
(select flag_img from teams where name=games.team_b) as flag_team_b
FROM games
LEFT JOIN guessings
ON games.id = guessings.game_id
AND guessings.user_id = ?
AND guessings.group_id = ?`
, [
req.session.user.id,
req.params.id
]);
res.render('group', {
pendings,
group: group[0],
games
});
});
app.post('/:id', async(req, res) => {
const guessings = [];
Object
.keys(req.body)
.forEach(team => {
const parts = team.split('_');
const game = {
game_id: parts[1],
result_a: req.body[team].a,
result_b: req.body[team].b
};
guessings.push(game);
});
const batch = guessings.map(guess => {
return connection.execute('insert into guessings (result_a, result_b, game_id, group_id, user_id) values (?,?,?,?,?)', [
guess.result_a,
guess.result_b,
guess.game_id,
req.params.id,
req.session.user.id
]);
});
await Promise.all(batch);
res.redirect('/groups/' + req.params.id);
});
// Permite ou Nega a entrada num grupo do usuário logado
app.get('/:id/pending/:idGU/:op', async(req, res) => {
const [ group ] = await connection.execute('select groups.*, groups_users.role from groups left join groups_users on groups_users.group_id = groups.id and groups_users.user_id = ? where groups.id = ?', [
req.session.user.id,
req.params.id
]);
if(group.length === 0 || group[0].role === 'owner'){
if(req.params.op === 'yes'){
await connection.execute('UPDATE groups_users SET role="user" where id = ? limit 1', [
req.params.idGU
]);
res.redirect('/groups/' + req.params.id);
} else {
await connection.execute('DELETE FROM groups_users WHERE id = ? limit 1', [
req.params.idGU
]);
res.redirect('/groups/' + req.params.id);
};
};
});
app.get('/:id/join', async(req, res) => {
const [ rows, fields ] = await connection.execute('select * from groups_users where user_id = ? and group_id = ?', [
req.session.user.id,
req.params.id
]);
if(rows.length > 0) {
res.redirect('/groups');
} else {
await connection.execute('insert into groups_users (group_id, user_id, role) values (?,?,?)', [
req.params.id,
req.session.user.id,
'pending'
]);
res.redirect('/groups');
};
});
app.get('/delete/:id', async(req, res) => {
await connection.execute('delete from groups where id = ? limit 1', [
req.params.id
]);
res.redirect('/groups');
});
// Metodo POST
app.post('/', async(req, res) => {
const [ inserted, insertFields ] = await connection.execute('insert into groups (name) values (?)', [
req.body.name
]);
await connection.execute('insert into groups_users (group_id, user_id, role) values (?,?,?)', [
inserted.insertId,
req.session.user.id,
'owner'
]);
res.redirect('/groups');
});
return app
};
module.exports = init;