Fetch top ranking
This commit is contained in:
parent
a575d447f9
commit
0c60b4ef26
4 changed files with 48 additions and 2 deletions
|
|
@ -2,7 +2,7 @@
|
||||||
const baseApiPath = "http://localhost:3000"
|
const baseApiPath = "http://localhost:3000"
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
async post(path: string, data: any) {
|
async post(path: string, data?: any) {
|
||||||
const headers = {
|
const headers = {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
}
|
}
|
||||||
|
|
@ -27,6 +27,31 @@ export default {
|
||||||
return response.json()
|
return response.json()
|
||||||
},
|
},
|
||||||
|
|
||||||
|
async get(path: string, data?: any) {
|
||||||
|
const headers = {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
}
|
||||||
|
|
||||||
|
if (window.myStuff.token) {
|
||||||
|
headers['Authorization'] = `Bearer ${window.myStuff.token}`
|
||||||
|
}
|
||||||
|
|
||||||
|
let response;
|
||||||
|
try {
|
||||||
|
response = await fetch(`${baseApiPath}/${path}`, {
|
||||||
|
method: "GET",
|
||||||
|
headers,
|
||||||
|
body: JSON.stringify(data)
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
console.log("oops");
|
||||||
|
|
||||||
|
console.error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.json()
|
||||||
|
},
|
||||||
|
|
||||||
async signup(name: string) {
|
async signup(name: string) {
|
||||||
return this.post("signup", { name })
|
return this.post("signup", { name })
|
||||||
},
|
},
|
||||||
|
|
@ -42,4 +67,8 @@ export default {
|
||||||
time
|
time
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
|
async top() {
|
||||||
|
return this.get("top")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -145,13 +145,15 @@ declare global {
|
||||||
elements.key!.innerText = ''
|
elements.key!.innerText = ''
|
||||||
}
|
}
|
||||||
|
|
||||||
function setup() {
|
async function setup() {
|
||||||
window.myStuff = {}
|
window.myStuff = {}
|
||||||
|
|
||||||
elements.buttons.login!.addEventListener("click", login)
|
elements.buttons.login!.addEventListener("click", login)
|
||||||
elements.buttons.logout!.addEventListener("click", logout)
|
elements.buttons.logout!.addEventListener("click", logout)
|
||||||
elements.buttons.signup!.addEventListener("click", signup)
|
elements.buttons.signup!.addEventListener("click", signup)
|
||||||
|
|
||||||
|
console.log(await API.top())
|
||||||
|
|
||||||
checkIfLogged()
|
checkIfLogged()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,14 @@ class Database {
|
||||||
await this.db.write()
|
await this.db.write()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getTop(amount = 15) {
|
||||||
|
return this.db.data.users
|
||||||
|
.filter(user => user.record)
|
||||||
|
.map(user => { return { name: user.name, record: user.record } })
|
||||||
|
.sort((a, b) => a.record > b.record ? -1 : 1)
|
||||||
|
.slice(0, amount)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default new Database
|
export default new Database
|
||||||
|
|
|
||||||
|
|
@ -84,6 +84,13 @@ app.post("/record", protect, neededArguments(['points', 'shoots', 'time']), jg,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
app.get("/top", (req, res) => {
|
||||||
|
res.json({
|
||||||
|
status: "ok",
|
||||||
|
records: db.getTop()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
app.use(function (err, req, res, next) {
|
app.use(function (err, req, res, next) {
|
||||||
if (err.name === 'UnauthorizedError') {
|
if (err.name === 'UnauthorizedError') {
|
||||||
res.status(401).send('invalid token...');
|
res.status(401).send('invalid token...');
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue