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
!10
Resolve "Tournament round"
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Resolve "Tournament round"
24-tournament-round
into
dev
Overview
0
Commits
6
Pipelines
0
Changes
2
Merged
Ivar Nordvik Myrstad
requested to merge
24-tournament-round
into
dev
5 years ago
Overview
0
Commits
6
Pipelines
0
Changes
2
Expand
Closes
#24 (closed)
Edited
5 years ago
by
Ivar Nordvik Myrstad
0
0
Merge request reports
Compare
dev
version 5
ce479489
5 years ago
version 4
a3f596f4
5 years ago
version 3
9e4ae218
5 years ago
version 2
6d423ca5
5 years ago
version 1
3818cb65
5 years ago
dev (base)
and
latest version
latest version
ad2321bb
6 commits,
5 years ago
version 5
ce479489
5 commits,
5 years ago
version 4
a3f596f4
4 commits,
5 years ago
version 3
9e4ae218
3 commits,
5 years ago
version 2
6d423ca5
2 commits,
5 years ago
version 1
3818cb65
1 commit,
5 years ago
2 files
+
231
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
2
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