Started reworking API...
This commit is contained in:
parent
f2ae45b2b8
commit
5319966d7f
|
@ -1,5 +1,6 @@
|
|||
import { Router } from 'express'
|
||||
import { sqlite } from '../libs/database/init.js'
|
||||
import searcher from '../libs/database/searcher.js'
|
||||
|
||||
const playerApi = Router()
|
||||
|
||||
|
@ -7,40 +8,57 @@ const playerApi = Router()
|
|||
playerApi.get(
|
||||
'/get/:player',
|
||||
async (req, res) => {
|
||||
let player = req.params.player
|
||||
|
||||
/* Misc */
|
||||
const firstFinish = sqlite.prepare(`SELECT * FROM race WHERE player = ? ORDER BY date ASC LIMIT 1`).get(player)
|
||||
|
||||
/* Points */
|
||||
let points = {}
|
||||
const pointsData = sqlite.prepare(`SELECT type, rank, points FROM points WHERE player = ?`)
|
||||
|
||||
for (const pointsType of pointsData.iterate(player)) {
|
||||
points[pointsType.type] = pointsType
|
||||
}
|
||||
|
||||
return res.json({
|
||||
success: true,
|
||||
response: {
|
||||
firstFinish,
|
||||
points,
|
||||
}
|
||||
})
|
||||
searcher(
|
||||
'points',
|
||||
'player',
|
||||
req.params.player,
|
||||
undefined,
|
||||
false,
|
||||
"get",
|
||||
0
|
||||
).then(
|
||||
player => res.json({
|
||||
success: true,
|
||||
response: player
|
||||
})
|
||||
).catch(
|
||||
error => res.json({
|
||||
success: false,
|
||||
response: error
|
||||
})
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
playerApi.get(
|
||||
'/search',
|
||||
async (req, res) => {
|
||||
/* Check if a query was provided */
|
||||
if (!req.query.q) {
|
||||
return res.json({
|
||||
success: false,
|
||||
response: "No query ('host/path?q=query') provided!"
|
||||
response: "No query ('?q=query') provided!"
|
||||
})
|
||||
}
|
||||
/* TODO: Use the searcher function */
|
||||
|
||||
searcher(
|
||||
'points',
|
||||
'player',
|
||||
`%${req.query.q}%`,
|
||||
req.query.sort ?? undefined,
|
||||
req.query.order === "asc",
|
||||
"all",
|
||||
req.query.page
|
||||
).then(
|
||||
player => res.json({
|
||||
success: true,
|
||||
response: player
|
||||
})
|
||||
).catch(
|
||||
error => res.json({
|
||||
success: false,
|
||||
response: error
|
||||
})
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
|
|
3
index.js
3
index.js
|
@ -8,7 +8,8 @@ import { downloadEssentialData } from './libs/download/dowload.js'
|
|||
|
||||
loadEnv()
|
||||
|
||||
await downloadEssentialData()
|
||||
if (process.env.DOWNLOAD_FILES === "true")
|
||||
await downloadEssentialData()
|
||||
|
||||
dbInit()
|
||||
generateDB()
|
||||
|
|
|
@ -30,7 +30,7 @@ export default function searcher(table, matchField=undefined, matchQuery=undefin
|
|||
sqlite
|
||||
.prepare(`
|
||||
SELECT count(*) FROM ${simpleSanitize(table)}
|
||||
${matchField ? `WHERE ${simpleSanitize(matchField)} = $matchQuery` : ""}
|
||||
${matchField ? `WHERE ${simpleSanitize(matchField)} LIKE $matchQuery` : ""}
|
||||
`)
|
||||
.get({
|
||||
matchQuery
|
||||
|
@ -44,7 +44,7 @@ export default function searcher(table, matchField=undefined, matchQuery=undefin
|
|||
const result = sqlite
|
||||
.prepare(`
|
||||
SELECT * FROM ${simpleSanitize(table)}
|
||||
${matchField ? `WHERE ${simpleSanitize(matchField)} = $matchQuery` : ""}
|
||||
${matchField ? `WHERE ${simpleSanitize(matchField)} LIKE $matchQuery` : ""}
|
||||
${orderBy ? `ORDER BY ${simpleSanitize(orderBy)} ${descending === true ? "DESC" : "ASC"}` : ""}
|
||||
${method === "all" ? `LIMIT ${entriesPerPage * (page - 1)}, ${entriesPerPage}` : ""}
|
||||
`)
|
||||
|
|
|
@ -12,6 +12,9 @@ MSGPACK_PATH = "data/players.msgpack"
|
|||
# Should the server try to generate the database?
|
||||
GENERATE_DB = "true"
|
||||
|
||||
# Should download files from DDNet servers?
|
||||
DOWNLOAD_FILES = "true"
|
||||
|
||||
# The port on which the server listens...
|
||||
PORT = 12345
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user