Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
TDT4240 Tank Wars
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
Package registry
Model registry
Operate
Environments
Terraform modules
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
Snorre Skjellestad Kristiansen
TDT4240 Tank Wars
Merge requests
!3
Resolve "Create a users endpoint"
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Resolve "Create a users endpoint"
6-create-a-users-endpoint
into
main
Overview
0
Commits
2
Pipelines
0
Changes
10
Merged
Fredrik Fonn Hansen
requested to merge
6-create-a-users-endpoint
into
main
2 years ago
Overview
0
Commits
2
Pipelines
0
Changes
10
Expand
Closes
#6 (closed)
0
0
Merge request reports
Compare
main
version 1
cdf6e6d3
2 years ago
main (base)
and
latest version
latest version
993a842c
2 commits,
2 years ago
version 1
cdf6e6d3
1 commit,
2 years ago
10 files
+
1477
−
22
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
10
Search (e.g. *.vue) (Ctrl+P)
backend/src/index.ts
+
75
−
0
Options
import
express
from
'
express
'
;
import
path
from
'
path
'
;
const
app
=
express
();
const
port
=
3000
;
// const cors = require('cors');
// app.use(cors());
// in testing, we don't want to cache the results
app
.
set
(
'
etag
'
,
false
);
app
.
use
((
req
,
res
,
next
)
=>
{
res
.
setHeader
(
'
Cache-Control
'
,
'
no-store
'
);
next
();
});
app
.
get
(
'
/
'
,
(
req
,
res
)
=>
{
res
.
send
(
'
Hello, World!
'
);
});
@@ -10,3 +21,67 @@ app.get('/', (req, res) => {
app
.
listen
(
port
,
()
=>
{
console
.
log
(
`Server started on port
${
port
}
`
);
});
// establish connection to firebase
const
admin
=
require
(
'
firebase-admin
'
);
const
serviceAccount
=
path
.
join
(
__dirname
,
'
../..
'
,
'
keys
'
,
'
fb-key.json
'
);
admin
.
initializeApp
({
credential
:
admin
.
credential
.
cert
(
serviceAccount
),
});
// returns all the user-id's
app
.
get
(
'
/user
'
,
async
(
req
,
res
)
=>
{
const
usersRef
=
admin
.
firestore
().
collection
(
'
users
'
);
const
querySnapshot
=
await
usersRef
.
get
();
if
(
querySnapshot
.
empty
)
{
res
.
status
(
204
).
send
(
'
No users found
'
);
}
else
{
const
userids
=
querySnapshot
.
docs
.
map
((
doc
:
{
id
:
any
})
=>
doc
.
id
);
res
.
status
(
200
).
send
(
userids
);
}
});
// returns data of a specific user
app
.
get
(
'
/user/:idOrUsername
'
,
async
(
req
,
res
)
=>
{
const
usersRef
=
admin
.
firestore
().
collection
(
'
users
'
);
const
snapshot
=
await
usersRef
.
get
();
const
searchParam
=
req
.
params
.
idOrUsername
;
const
user
=
snapshot
.
docs
.
find
((
doc
:
{
id
:
string
})
=>
doc
.
id
===
searchParam
)
||
(
await
Promise
.
all
(
snapshot
.
docs
.
map
(
async
(
doc
:
{
id
:
string
})
=>
{
const
userSnapshot
=
await
usersRef
.
doc
(
doc
.
id
).
get
();
return
userSnapshot
.
data
().
username
===
searchParam
?
userSnapshot
:
null
;
})
)
).
find
(
Boolean
);
if
(
user
)
{
res
.
status
(
200
).
send
(
user
.
data
());
}
else
{
res
.
status
(
404
).
send
(
'
User not found
'
);
}
});
// create new user
app
.
post
(
'
/user/create/:username
'
,
async
(
req
,
res
)
=>
{
const
usersRef
=
admin
.
firestore
().
collection
(
'
users
'
);
const
querySnapshot
=
await
usersRef
.
where
(
'
username
'
,
'
==
'
,
req
.
params
.
username
).
get
();
if
(
!
querySnapshot
.
empty
)
{
res
.
status
(
409
).
send
(
'
User already exists
'
);
}
else
{
const
newUserRef
=
await
usersRef
.
add
({
username
:
req
.
params
.
username
,
highscore
:
0
,
games
:
0
,
wins
:
0
,
losses
:
0
,
});
res
.
status
(
201
).
send
(
newUserRef
.
id
);
}
});
Loading