-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.js
155 lines (139 loc) · 5.2 KB
/
controller.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
143
144
145
146
147
148
149
150
151
152
153
154
155
const utils = require('./utils.js');
const model = require('./model.js');
function error404(req, res, next) {
res.status(404);
res.render('404');
}
function error500(err, req, res, next) {
console.error(err)
res.status(500);
res.render('500');
}
async function create_championship(req, res, next) {
let location = req.body.location
let type = req.body.type
let id_player = req.body.id_player
let organizer = req.body.organizer
let name = req.body.name
let comment = req.body.comment
let active_championship = await model.get_championships_in_progress(undefined)
let response = ""
if (active_championship.length != 0) {
response = {
"championship_approved": false,
"error": "already_exists",
"id_championship": active_championship[0].id,
}
} else {
response = await model.create_championship(location, type, id_player, organizer, name, comment)
}
res.type("application/json")
res.send(JSON.stringify(response))
}
async function get_players(req, res, next) {
let id_players = req.body.id_players
let players = await model.get_players(id_players)
res.type("application/json")
res.send(JSON.stringify(players))
}
async function get_locations(req, res, next) {
let id_locations = req.body.id_locations
let locations = await model.get_locations(id_locations)
res.type("application/json")
res.send(JSON.stringify(locations))
}
async function login(req, res, next) {
let id_player = req.body.id_player
let password = req.body.password
let player = await model.login(id_player, password)
if (player.length == 0) {
player = {
"ok": false
}
}
res.type("application/json")
res.send(JSON.stringify(player))
}
async function get_championships_in_progress(req, res, next) {
let id_player = req.id_player
let championships = await model.get_championships_in_progress(id_player)
res.type("application/json")
res.send(JSON.stringify(championships))
}
async function get_championship_info(req, res, next) {
let id_championship = req.query.id_championship
console.log("id_championship: " + id_championship)
let championship_info = await model.get_championship_info(id_championship)
res.type("application/json")
res.send(JSON.stringify(championship_info))
}
async function get_championship_matches(req, res, next) {
let id_championship = req.query.id_championship
let matches = await model.get_championship_matches(id_championship)
res.type("application/json")
res.send(JSON.stringify(matches))
}
async function get_championship_details(req, res, next) {
let id_championship = req.query.id_championship
let details = await model.get_championship_details(id_championship)
res.type("application/json")
res.send(JSON.stringify(details))
}
async function update_championship_status(req, res, next) {
let id_match = req.query.id_match
let id_championship = req.query.id_championship
let team1_score = parseInt(req.query.team1_score)
let team2_score = parseInt(req.query.team2_score)
let update_success = await model.update_match_result(id_match, team1_score, team2_score)
let result
if (update_success) {
result = await utils.championship_manager(id_championship)
} else {
result = {
"error": "error in the update of the match score"
}
}
res.type("application/json")
res.send(JSON.stringify(result))
}
async function get_chart(req, res, next) {
id_players = req.body.id_players
let chart = await model.get_chart(id_players)
res.type("application/json")
res.send(JSON.stringify(chart))
}
async function get_championship_chart(req, res, next) {
let id_championship = req.query.id_championship
let chart = await utils.chart_manager(id_championship)
res.type("application/json")
res.send(JSON.stringify(chart))
}
async function delete_championship(req, res, next) {
let id_championship = req.query.id_championship
let result = await model.delete_championship(id_championship)
res.type("application/json")
res.send(JSON.stringify(result))
}
async function get_prizes(req, res, next) {
let id_player = req.query.id_player
let prizes = await model.get_prizes(id_player)
res.type("application/json")
res.send(JSON.stringify(prizes))
}
exports.dispatcher = function (app) {
app.post('/create_championship', create_championship)
app.post('/get_players', get_players)
app.post('/get_locations', get_locations)
app.post('/get_chart', get_chart)
app.post('/login', login)
app.get('/get_championships_in_progress', get_championships_in_progress)
app.get('/get_championship_info', get_championship_info)
app.get('/get_championship_matches', get_championship_matches)
app.get('/get_championship_details', get_championship_details)
app.get('/update_championship_status', update_championship_status)
app.get('/get_championship_chart', get_championship_chart)
app.get('/delete_championship', delete_championship)
app.get('/get_prizes', get_prizes);
app.use(error404) // 404 catch-all handler (middleware)
app.use(error500) // 500 error handler (middleware)
}