Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Progark gruppe 3
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Tobias Ingebrigt Ørstad
Progark gruppe 3
Merge requests
!18
dev into fetch-tournament.
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
dev into fetch-tournament.
dev
into
4-fetch-tournament
Overview
0
Commits
11
Pipelines
0
Changes
3
Merged
Tobias Ingebrigt Ørstad
requested to merge
dev
into
4-fetch-tournament
5 years ago
Overview
0
Commits
11
Pipelines
0
Changes
3
Expand
To allow testing before merging into dev.
0
0
Merge request reports
Compare
4-fetch-tournament
version 1
e4c564ac
5 years ago
4-fetch-tournament (base)
and
latest version
latest version
bef1187a
11 commits,
5 years ago
version 1
e4c564ac
10 commits,
5 years ago
3 files
+
300
−
2
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
3
Search (e.g. *.vue) (Ctrl+P)
backend/api/rounds.js
0 → 100644
+
217
−
0
Options
const
express
=
require
(
"
express
"
);
const
router
=
express
.
Router
();
const
mongo
=
require
(
"
mongodb
"
);
const
MongoClient
=
mongo
.
MongoClient
;
const
connectionUrl
=
process
.
env
.
MONGO_CONNECTION_STRING
;
router
.
get
(
"
/:roundid
"
,
(
req
,
res
)
=>
{
// Connect to database
MongoClient
.
connect
(
connectionUrl
,
{
useNewUrlParser
:
true
,
useUnifiedTopology
:
true
},
(
err
,
client
)
=>
{
// Unable to connect to database
if
(
err
)
{
res
.
sendStatus
(
500
);
// Internal server error
return
;
}
// Using the database gameWare and collection rounds
const
db
=
client
.
db
(
"
gameWare
"
);
const
collection
=
"
rounds
"
;
let
id
=
undefined
;
try
{
id
=
mongo
.
ObjectID
(
req
.
params
.
roundid
);
// get roundId
}
catch
(
error
)
{
res
.
status
(
404
).
send
(
"
No document with specified id was found
"
);
return
;
}
db
.
collection
(
collection
)
.
findOne
({
_id
:
id
})
.
then
(
result
=>
{
if
(
!
result
)
{
res
.
status
(
404
).
send
(
"
No document with specified id found
"
);
return
;
}
res
.
json
(
result
);
})
.
catch
(
err
=>
console
.
log
(
err
));
}
);
});
router
.
get
(
"
/:tournamentId/:playerId/:roundNr
"
,
(
req
,
res
)
=>
{
// Connect to database
MongoClient
.
connect
(
connectionUrl
,
{
useNewUrlParser
:
true
,
useUnifiedTopology
:
true
},
(
err
,
client
)
=>
{
// Unable to connect to database
if
(
err
)
{
res
.
sendStatus
(
500
);
// Internal server error
return
;
}
// Using the database gameWare and collection rounds
const
db
=
client
.
db
(
"
gameWare
"
);
const
collection
=
"
rounds
"
;
let
tournamentId
,
playerId
,
roundNr
=
undefined
;
try
{
tournamentId
=
mongo
.
ObjectID
(
req
.
params
.
tournamentId
);
// get tournamentId
playerId
=
mongo
.
ObjectID
(
req
.
params
.
playerId
);
// get playerId
roundNr
=
parseInt
(
req
.
params
.
roundNr
);
// get roundNr
}
catch
(
error
)
{
res
.
status
(
404
).
send
(
"
No document with specified params found
"
);
return
;
}
db
.
collection
(
collection
)
.
findOne
({
tournamentId
,
playerId
,
roundNr
})
.
then
(
result
=>
{
if
(
!
result
)
{
res
.
status
(
404
).
send
(
"
No document with specified id params found
"
);
return
;
}
res
.
json
(
result
);
})
.
catch
(
err
=>
console
.
log
(
err
));
}
);
});
router
.
post
(
"
/
"
,
(
req
,
res
)
=>
{
// Connect to database
MongoClient
.
connect
(
connectionUrl
,
{
useNewUrlParser
:
true
,
useUnifiedTopology
:
true
},
(
err
,
client
)
=>
{
// Unable to connect to database
if
(
err
)
{
res
.
sendStatus
(
500
);
// Internal server error
return
;
}
// Using the database gameWare and collection rounds
const
db
=
client
.
db
(
"
gameWare
"
);
const
collection
=
"
rounds
"
;
const
values
=
{
tournamentId
:
null
,
playerId
:
null
,
gameId
:
null
,
roundNr
:
null
,
deadlineDate
:
null
};
try
{
values
.
tournamentId
=
mongo
.
ObjectID
(
req
.
body
.
tournamentId
);
values
.
playerId
=
mongo
.
ObjectID
(
req
.
body
.
playerId
);
values
.
gameId
=
mongo
.
ObjectID
(
req
.
body
.
gameId
);
values
.
roundNr
=
!
isNaN
(
parseInt
(
req
.
body
.
roundNr
))
?
parseInt
(
req
.
body
.
roundNr
)
:
null
;
values
.
deadlineDate
=
getDate
(
req
.
body
.
deadlineDate
);
}
catch
(
error
)
{
res
.
status
(
404
).
send
(
"
No document with specified id was found
"
);
console
.
log
(
error
);
return
;
}
// validating body
Object
.
keys
(
values
).
forEach
(
val
=>
{
if
(
!
req
.
body
[
val
])
{
values
[
val
]
=
null
;
}
});
// values contains a null field
if
(
Object
.
values
(
values
).
some
(
x
=>
x
===
null
))
{
res
.
status
(
404
).
send
(
"
Missing fields in request
"
);
return
;
}
values
.
scoreValue
=
0
;
values
.
hasPlayed
=
false
;
db
.
collection
(
collection
).
insertOne
(
values
,
(
err
,
result
)
=>
{
if
(
err
)
{
res
.
sendStatus
(
500
);
// Internal server error
return
;
}
res
.
json
(
result
.
ops
[
0
]);
});
}
);
});
router
.
put
(
"
/:roundid
"
,
(
req
,
res
)
=>
{
// Connect to database
MongoClient
.
connect
(
connectionUrl
,
{
useNewUrlParser
:
true
,
useUnifiedTopology
:
true
},
(
err
,
client
)
=>
{
// Unable to connect to database
if
(
err
)
{
res
.
sendStatus
(
500
);
// Internal server error
return
;
}
// Using the database gameWare and collection rounds
const
db
=
client
.
db
(
"
gameWare
"
);
const
collection
=
"
rounds
"
;
let
id
,
scoreValue
=
null
;
try
{
id
=
mongo
.
ObjectID
(
req
.
params
.
roundid
);
// get roundId
scoreValue
=
!
isNaN
(
parseInt
(
req
.
body
.
scoreValue
))
?
parseInt
(
req
.
body
.
scoreValue
)
:
null
;
}
catch
(
error
)
{
res
.
status
(
400
).
send
(
"
No document with specified id was found
"
);
return
;
}
if
(
scoreValue
===
null
)
{
res
.
status
(
400
).
send
(
"
Request body not valid
"
);
return
;
}
db
.
collection
(
collection
)
.
findOneAndUpdate
(
{
_id
:
id
},
{
$set
:
{
scoreValue
:
scoreValue
}
},
{
returnOriginal
:
false
}
)
.
then
(
updatedDocument
=>
{
if
(
updatedDocument
.
value
)
{
//console.log("Successfully updated document");
res
.
json
(
updatedDocument
.
value
);
// Return updated document
}
else
{
//console.log("Failed to update doucment");
res
.
status
(
404
).
send
(
"
No document with specified id was found
"
);
}
})
.
catch
(
err
=>
console
.
log
(
err
));
}
);
});
// Export API routes
module
.
exports
=
router
;
function
getDate
(
dateString
)
{
date
=
new
Date
(
dateString
);
if
(
isNaN
(
date
.
getTime
()))
{
return
null
;
}
return
date
;
}
Loading