Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Voting
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Container Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Dennis Willers
Voting
Commits
1cab9dfa
Commit
1cab9dfa
authored
Aug 23, 2021
by
Matthieu - Greep
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moving Events to multiple files (still in WIP)
parent
fb2145d5
Pipeline
#446
canceled with stage
in 1 minute and 4 seconds
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
78 additions
and
27 deletions
+78
-27
events.js
events.js
+34
-27
getVotes.js
route/noToken/getVotes.js
+36
-0
test.js
route/noToken/test.js
+8
-0
No files found.
events.js
View file @
1cab9dfa
const
express
=
require
(
'
express
'
);
const
fs
=
require
(
'
fs
'
);
function
createRouter
(
db
)
{
const
router
=
express
.
Router
();
...
...
@@ -30,16 +31,16 @@ function createRouter(db) {
(
error
,
results
)
=>
{
if
(
error
)
{
console
.
log
(
error
);
res
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
status
(
500
).
json
({
status
:
'
error in hasMap
'
});
res
.
status
(
500
).
json
({
status
:
'
error in hasMap
'
});
}
else
{
numberResults
=
results
[
0
].
Maps
;
if
(
numberResults
===
1
)
next
();
else
if
(
numberResults
===
0
)
addMap
(
req
,
res
,
next
);
else
res
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
status
(
500
).
json
({
status
:
'
There are multiple maps with the same id
'
});
else
res
.
status
(
500
).
json
({
status
:
'
There are multiple maps with the same id
'
});
}
});
}
else
{
res
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
status
(
500
).
json
({
status
:
'
JSON-Parameter are not valid
'
});
res
.
status
(
500
).
json
({
status
:
'
JSON-Parameter are not valid
'
});
}
};
...
...
@@ -53,10 +54,10 @@ function createRouter(db) {
(
error
,
results
)
=>
{
if
(
error
)
{
console
.
log
(
error
);
res
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
status
(
500
).
json
({
status
:
'
error in addMap
'
});
res
.
status
(
500
).
json
({
status
:
'
error in addMap
'
});
}
else
{
if
(
results
.
affectedRows
===
1
)
next
();
else
res
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
status
(
500
).
json
({
status
:
'
error in insertMap
'
});
else
res
.
status
(
500
).
json
({
status
:
'
error in insertMap
'
});
}
});
};
...
...
@@ -72,15 +73,15 @@ function createRouter(db) {
(
error
,
results
)
=>
{
if
(
error
)
{
console
.
log
(
error
);
res
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
status
(
500
).
json
({
status
:
'
error in hasVote
'
});
res
.
status
(
500
).
json
({
status
:
'
error in hasVote
'
});
}
else
{
if
(
results
.
length
===
0
)
insertVote
(
req
,
res
,
next
);
else
if
(
results
.
length
===
1
)
updateVote
(
req
,
res
,
next
);
else
res
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
status
(
500
).
json
({
status
:
'
too many votes by user
'
});
else
res
.
status
(
500
).
json
({
status
:
'
too many votes by user
'
});
}
});
}
else
{
res
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
status
(
500
).
json
({
status
:
'
Vote Request is not valid
'
});
res
.
status
(
500
).
json
({
status
:
'
Vote Request is not valid
'
});
}
};
...
...
@@ -96,14 +97,14 @@ function createRouter(db) {
(
error
,
results
)
=>
{
if
(
error
)
{
console
.
log
(
error
);
res
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
status
(
500
).
json
({
status
:
'
error in insertVote
'
});
res
.
status
(
500
).
json
({
status
:
'
error in insertVote
'
});
}
else
{
if
(
results
.
affectedRows
===
1
)
next
();
else
res
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
status
(
500
).
json
({
status
:
'
error in insertVote
'
});
else
res
.
status
(
500
).
json
({
status
:
'
error in insertVote
'
});
}
});
}
else
{
res
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
status
(
500
).
json
({
status
:
'
Vote Request is not valid
'
});
res
.
status
(
500
).
json
({
status
:
'
Vote Request is not valid
'
});
}
};
...
...
@@ -119,14 +120,14 @@ function createRouter(db) {
(
error
,
results
)
=>
{
if
(
error
)
{
console
.
log
(
error
);
res
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
status
(
500
).
json
({
status
:
'
error in updateVote
'
});
res
.
status
(
500
).
json
({
status
:
'
error in updateVote
'
});
}
else
{
if
(
results
.
affectedRows
===
1
)
next
();
else
res
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
status
(
500
).
json
({
status
:
'
error in updateVote
'
});
else
res
.
status
(
500
).
json
({
status
:
'
error in updateVote
'
});
}
});
}
else
{
res
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
status
(
500
).
json
({
status
:
'
Vote Request is not valid
'
});
res
.
status
(
500
).
json
({
status
:
'
Vote Request is not valid
'
});
}
};
...
...
@@ -141,21 +142,21 @@ function createRouter(db) {
(
error
,
results
)
=>
{
if
(
error
)
{
console
.
log
(
error
);
res
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
status
(
500
).
json
({
status
:
'
error in getMapInfo
'
});
res
.
status
(
500
).
json
({
status
:
'
error in getMapInfo
'
});
}
else
{
if
(
results
.
length
===
1
)
{
let
average
=
results
[
0
].
average
;
let
votes
=
results
[
0
].
votes
;
if
(
!
average
)
average
=
0
;
if
(
!
votes
)
votes
=
0
;
res
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
status
(
200
).
json
({
votes
:
votes
,
average
:
average
,
vote
:
vote
});
res
.
json
({
votes
:
votes
,
average
:
average
,
vote
:
vote
});
return
;
}
res
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
status
(
500
).
json
({
status
:
'
unexpected result in getMapInfo
'
});
res
.
status
(
500
).
json
({
status
:
'
unexpected result in getMapInfo
'
});
}
});
}
else
{
res
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
status
(
500
).
json
({
status
:
'
Vote Request is not valid
'
});
res
.
status
(
500
).
json
({
status
:
'
Vote Request is not valid
'
});
}
};
...
...
@@ -170,7 +171,7 @@ function createRouter(db) {
(
error
,
results
)
=>
{
if
(
error
)
{
console
.
log
(
error
);
res
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
status
(
500
).
json
({
status
:
'
error in getVote
'
});
res
.
status
(
500
).
json
({
status
:
'
error in getVote
'
});
}
else
{
if
(
results
.
length
<
2
)
{
let
vote
=
-
1
;
...
...
@@ -182,24 +183,30 @@ function createRouter(db) {
getMapInfo
(
req
,
res
);
return
;
}
res
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
status
(
500
).
json
({
status
:
'
unexpected result in getVote
'
});
res
.
status
(
500
).
json
({
status
:
'
unexpected result in getVote
'
});
}
else
{
res
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
status
(
500
).
json
({
status
:
'
too many results in getVote
'
});
res
.
status
(
500
).
json
({
status
:
'
too many results in getVote
'
});
}
}
});
}
else
{
res
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
status
(
500
).
json
({
status
:
'
Vote Request is not valid
'
});
res
.
status
(
500
).
json
({
status
:
'
Vote Request is not valid
'
});
}
};
const
test
=
function
test
(
req
,
res
)
{
res
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
status
(
200
).
json
({
status
:
"
Test valid!
"
});
};
// the routes are defined here
fs
.
readdir
(
'
./route
'
,
(
err
,
files
)
=>
{
if
(
err
)
{
console
.
error
(
err
);
}
else
{
files
.
forEach
(
file
=>
{
if
(
file
.
endsWith
(
'
.js
'
))
{
require
(
'
./route/
'
+
file
)(
router
);
}
});
}
});
router
.
get
(
'
/test
'
,
[
test
]);
router
.
post
(
'
/setVote
'
,
[
hasMap
,
hasVote
,
getMapInfo
]);
router
.
post
(
'
/getMapInfo
'
,
[
hasMap
,
getVote
]);
...
...
route/noToken/getVotes.js
0 → 100644
View file @
1cab9dfa
const
path
=
require
(
'
path
'
),
scriptName
=
path
.
basename
(
__filename
).
replace
(
/
\.
js$/i
,
''
);
/**
* @param {import('express').Express} app
* @param {import('mysql').Connection} sql
*/
module
.
exports
=
function
(
app
,
sql
,
errorHandler
)
{
app
.
get
(
'
/
'
+
scriptName
,
function
(
req
,
res
)
{
const
mapId
=
req
.
query
.
map
;
if
(
!
mapId
)
{
return
errorHandler
(
res
,
400
,
'
Missing map id
'
);
}
sql
.
query
(
'
SELECT map, date, COUNT(*) AS count, round(avg(vote)) as average FROM votes WHERE map = ?
'
,
mapId
,
(
err
,
sqlRes
)
=>
{
if
(
err
)
{
console
.
log
(
err
);
errorHandler
(
res
,
500
,
'
Internal server error
'
);
}
else
{
if
(
sqlRes
[
0
].
map
==
null
)
{
return
errorHandler
(
res
,
404
,
'
Map not found
'
);
}
// Get the latest date from an array
const
latestDate
=
new
Date
(
Math
.
max
.
apply
(
null
,
sqlRes
.
map
(
function
(
e
)
{
return
new
Date
(
e
.
date
);
})));
res
.
json
({
mapUid
:
sqlRes
[
0
].
map
,
votes
:
sqlRes
[
0
].
count
,
average
:
sqlRes
[
0
].
average
,
lastVoteDate
:
latestDate
});
}
});
});
};
\ No newline at end of file
route/noToken/test.js
0 → 100644
View file @
1cab9dfa
const
path
=
require
(
'
path
'
),
scriptName
=
path
.
basename
(
__filename
).
replace
(
/
\.
js$/i
,
''
);
module
.
exports
=
function
(
router
)
{
router
.
get
(
'
/
'
+
scriptName
,
function
(
req
,
res
)
{
res
.
json
({
status
:
"
Test valid!
"
});
});
};
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment