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
f08d3d64
Commit
f08d3d64
authored
Aug 23, 2021
by
Matthieu - Greep
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Delete events.js, I don't need you anymore cause we have route
parent
04d5a096
Pipeline
#448
passed with stage
in 50 seconds
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
0 additions
and
217 deletions
+0
-217
events.js
events.js
+0
-216
index.js
index.js
+0
-1
No files found.
events.js
deleted
100644 → 0
View file @
04d5a096
const
express
=
require
(
'
express
'
);
const
fs
=
require
(
'
fs
'
);
function
createRouter
(
db
)
{
const
router
=
express
.
Router
();
function
isMapIdAndNameNotNull
(
req
)
{
const
mapId
=
req
.
body
.
mapId
;
const
name
=
req
.
body
.
name
;
return
mapId
!=
null
&&
name
!=
null
;
}
function
isVoteRequestValid
(
req
)
{
const
userId
=
req
.
body
.
userId
;
const
vote
=
req
.
body
.
vote
;
return
userId
!=
null
&&
vote
!=
null
&&
isMapIdAndNameNotNull
(
req
);
}
function
isgetMapInfoValid
(
req
)
{
const
userId
=
req
.
body
.
userId
;
return
userId
!=
null
&&
isMapIdAndNameNotNull
(
req
);
}
const
hasMap
=
function
hasMap
(
req
,
res
,
next
)
{
const
mapId
=
req
.
body
.
mapId
;
if
(
isMapIdAndNameNotNull
(
req
))
{
const
sql
=
'
SELECT count(*) as Maps From Map WHERE mapId =
\
"
'
+
mapId
+
'
\
";
'
;
console
.
log
(
'
Map Select:
'
,
sql
);
db
.
query
(
sql
,
(
error
,
results
)
=>
{
if
(
error
)
{
console
.
log
(
error
);
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
.
status
(
500
).
json
({
status
:
'
There are multiple maps with the same id
'
});
}
});
}
else
{
res
.
status
(
500
).
json
({
status
:
'
JSON-Parameter are not valid
'
});
}
};
const
addMap
=
function
addMap
(
req
,
res
,
next
)
{
const
mapId
=
req
.
body
.
mapId
;
const
name
=
req
.
body
.
name
;
const
sql
=
'
INSERT INTO Map (mapId, name) VALUES (
\
"
'
+
mapId
+
'
\
",
\
"
'
+
name
+
'
\
");
'
;
console
.
log
(
'
Map Insert:
'
,
sql
);
db
.
query
(
sql
,
(
error
,
results
)
=>
{
if
(
error
)
{
console
.
log
(
error
);
res
.
status
(
500
).
json
({
status
:
'
error in addMap
'
});
}
else
{
if
(
results
.
affectedRows
===
1
)
next
();
else
res
.
status
(
500
).
json
({
status
:
'
error in insertMap
'
});
}
});
};
const
hasVote
=
function
hasVote
(
req
,
res
,
next
)
{
if
(
isVoteRequestValid
(
req
))
{
const
userId
=
req
.
body
.
userId
;
const
mapId
=
req
.
body
.
mapId
;
const
sql
=
'
SELECT userId FROM Vote WHERE userId =
\
"
'
+
userId
+
'
\
" AND mapId =
\
"
'
+
mapId
+
'
\
";
'
;
console
.
log
(
'
Map Insert:
'
,
sql
);
db
.
query
(
sql
,
(
error
,
results
)
=>
{
if
(
error
)
{
console
.
log
(
error
);
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
.
status
(
500
).
json
({
status
:
'
too many votes by user
'
});
}
});
}
else
{
res
.
status
(
500
).
json
({
status
:
'
Vote Request is not valid
'
});
}
};
const
insertVote
=
function
insertVote
(
req
,
res
,
next
)
{
if
(
isVoteRequestValid
(
req
))
{
const
userId
=
req
.
body
.
userId
;
const
mapId
=
req
.
body
.
mapId
;
const
vote
=
req
.
body
.
vote
;
const
sql
=
'
INSERT INTO Vote (userId, mapId, vote) VALUES (
\
"
'
+
userId
+
'
\
",
\
"
'
+
mapId
+
'
\
",
\
"
'
+
vote
+
'
\
");
'
;
console
.
log
(
'
Insert Vote:
'
,
sql
);
db
.
query
(
sql
,
(
error
,
results
)
=>
{
if
(
error
)
{
console
.
log
(
error
);
res
.
status
(
500
).
json
({
status
:
'
error in insertVote
'
});
}
else
{
if
(
results
.
affectedRows
===
1
)
next
();
else
res
.
status
(
500
).
json
({
status
:
'
error in insertVote
'
});
}
});
}
else
{
res
.
status
(
500
).
json
({
status
:
'
Vote Request is not valid
'
});
}
};
const
updateVote
=
function
updateVote
(
req
,
res
,
next
)
{
if
(
isVoteRequestValid
(
req
))
{
const
userId
=
req
.
body
.
userId
;
const
mapId
=
req
.
body
.
mapId
;
const
vote
=
req
.
body
.
vote
;
const
sql
=
'
UPDATE Vote SET userId =
\
"
'
+
userId
+
'
\
", mapId =
\
"
'
+
mapId
+
'
\
", vote =
\
"
'
+
vote
+
'
\
" WHERE userID =
\
"
'
+
userId
+
'
\
" AND mapId =
\
"
'
+
mapId
+
'
\
";
'
;
console
.
log
(
'
Update Vote:
'
,
sql
);
db
.
query
(
sql
,
(
error
,
results
)
=>
{
if
(
error
)
{
console
.
log
(
error
);
res
.
status
(
500
).
json
({
status
:
'
error in updateVote
'
});
}
else
{
if
(
results
.
affectedRows
===
1
)
next
();
else
res
.
status
(
500
).
json
({
status
:
'
error in updateVote
'
});
}
});
}
else
{
res
.
status
(
500
).
json
({
status
:
'
Vote Request is not valid
'
});
}
};
const
getMapInfo
=
function
getMapInfo
(
req
,
res
)
{
if
(
isVoteRequestValid
(
req
))
{
const
mapId
=
req
.
body
.
mapId
;
const
vote
=
req
.
body
.
vote
;
const
sql
=
'
SELECT count(userId) as votes, round(avg(vote)) as average FROM Vote WHERE mapId LIKE
\
"
'
+
mapId
+
'
\
";
'
;
console
.
log
(
'
getMapInfo:
'
,
sql
);
db
.
query
(
sql
,
(
error
,
results
)
=>
{
if
(
error
)
{
console
.
log
(
error
);
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
.
json
({
votes
:
votes
,
average
:
average
,
vote
:
vote
});
return
;
}
res
.
status
(
500
).
json
({
status
:
'
unexpected result in getMapInfo
'
});
}
});
}
else
{
res
.
status
(
500
).
json
({
status
:
'
Vote Request is not valid
'
});
}
};
const
getVote
=
function
getVote
(
req
,
res
)
{
if
(
isgetMapInfoValid
(
req
))
{
const
mapId
=
req
.
body
.
mapId
;
const
userId
=
req
.
body
.
userId
;
const
sql
=
'
SELECT vote FROM Vote WHERE mapId LIKE
\
"
'
+
mapId
+
'
\
" AND userId LIKE
\
"
'
+
userId
+
'
\
";
'
;
console
.
log
(
'
Update Vote:
'
,
sql
);
db
.
query
(
sql
,
(
error
,
results
)
=>
{
if
(
error
)
{
console
.
log
(
error
);
res
.
status
(
500
).
json
({
status
:
'
error in getVote
'
});
}
else
{
if
(
results
.
length
<
2
)
{
let
vote
=
-
1
;
if
(
results
.
length
===
1
)
{
vote
=
results
[
0
].
vote
;
}
if
(
!
isNaN
(
vote
))
{
req
.
body
.
vote
=
vote
;
getMapInfo
(
req
,
res
);
return
;
}
res
.
status
(
500
).
json
({
status
:
'
unexpected result in getVote
'
});
}
else
{
res
.
status
(
500
).
json
({
status
:
'
too many results in getVote
'
});
}
}
});
}
else
{
res
.
status
(
500
).
json
({
status
:
'
Vote Request is not 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
.
post
(
'
/setVote
'
,
[
hasMap
,
hasVote
,
getMapInfo
]);
router
.
post
(
'
/getMapInfo
'
,
[
hasMap
,
getVote
]);
return
router
;
}
module
.
exports
=
createRouter
;
index.js
View file @
f08d3d64
...
...
@@ -5,7 +5,6 @@ const express = require('express'),
logger
=
require
(
'
morgan
'
),
cookieParser
=
require
(
'
cookie-parser
'
),
mysql
=
require
(
'
mysql
'
),
//events = require('./events'),
path
=
require
(
'
path
'
),
fs
=
require
(
'
fs
'
);
...
...
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