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
7c213ebe
Commit
7c213ebe
authored
Aug 16, 2021
by
Dennis Willers
🏀
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement - REST-Services
parent
86ddf602
Pipeline
#409
passed with stages
in 2 minutes and 42 seconds
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
206 additions
and
6 deletions
+206
-6
events.js
events.js
+200
-0
index.js
index.js
+6
-6
No files found.
events.js
0 → 100644
View file @
7c213ebe
const
express
=
require
(
'
express
'
);
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
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
status
(
500
).
json
({
status
:
'
error in hasMap
'
});
}
else
{
numberResults
=
results
[
0
].
Maps
;
console
.
log
(
numberResults
);
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
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
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
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
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
'
});
}
});
};
const
hasVote
=
function
hasVote
(
req
,
res
,
next
)
{
if
(
isVoteRequestValid
(
req
))
{
const
userId
=
req
.
body
.
userId
;
const
sql
=
'
SELECT userId FROM Vote WHERE userId =
\
"
'
+
userId
+
'
\
";
'
;
console
.
log
(
'
Map Insert:
'
,
sql
);
db
.
query
(
sql
,
(
error
,
results
)
=>
{
if
(
error
)
{
console
.
log
(
error
);
res
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
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
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
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
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
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
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
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
+
'
\
";
'
;
console
.
log
(
'
Update Vote:
'
,
sql
);
db
.
query
(
sql
,
(
error
,
results
)
=>
{
if
(
error
)
{
console
.
log
(
error
);
res
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
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
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
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
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
status
(
500
).
json
({
status
:
'
error in getMapInfo
'
});
}
else
{
if
(
results
.
length
===
1
)
{
const
average
=
results
[
0
].
average
;
const
votes
=
results
[
0
].
votes
;
if
(
average
!==
null
&&
votes
!==
null
)
{
res
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
status
(
500
).
json
({
votes
:
votes
,
average
:
average
,
vote
:
vote
});
return
;
}
}
res
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
status
(
500
).
json
({
status
:
'
error in getMapInfo
'
});
}
});
}
else
{
res
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
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 v2 WHERE mapId LIKE
\
"
'
+
mapId
+
'
\
" AND userId LIKE
\
"
'
+
userId
+
'
\
";
'
;
console
.
log
(
'
Update Vote:
'
,
sql
);
db
.
query
(
sql
,
(
error
,
results
)
=>
{
if
(
error
)
{
console
.
log
(
error
);
res
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
status
(
500
).
json
({
status
:
'
error in getVote
'
});
}
else
{
if
(
results
.
length
===
1
)
{
const
vote
=
results
[
0
].
vote
;
if
(
vote
!==
null
)
{
req
.
body
.
vote
=
vote
;
getMapInfo
(
req
,
res
);
return
;
}
}
res
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
status
(
500
).
json
({
status
:
'
error in getVote
'
});
}
});
}
else
{
res
.
header
(
'
Access-Control-Allow-Origin
'
,
"
*
"
).
status
(
500
).
json
({
status
:
'
Vote Request is not valid
'
});
}
};
// the routes are defined here
//Return all Sessions (Not neccesary)
router
.
post
(
'
/addMap
'
,
[
hasMap
,
hasVote
,
getMapInfo
]);
router
.
post
(
'
/getMapInfo
'
,
[
getVote
]);
return
router
;
}
module
.
exports
=
createRouter
;
index.js
View file @
7c213ebe
...
...
@@ -3,14 +3,14 @@ const express = require('express');
const
cors
=
require
(
'
cors
'
);
const
bodyParser
=
require
(
'
body-parser
'
);
const
mysql
=
require
(
'
mysql
'
);
//
const events = require('./events');
const
events
=
require
(
'
./events
'
);
/*
const connection = mysql.createConnection({
const
connection
=
mysql
.
createConnection
({
host
:
'
192.168.0.50
'
,
port
:
'
3307
'
,
user : '
--
',
password : '
--
',
database : '
--
',
user
:
'
votingApp
'
,
password
:
'
R0b0TecDev!
'
,
database
:
'
Voting
'
,
multipleStatements
:
true
,
});
...
...
@@ -25,4 +25,4 @@ const app = express()
app
.
listen
(
port
,
()
=>
{
console
.
log
(
`Express server listening on port
${
port
}
`
);
});
*/
});
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