Started reworking API...

This commit is contained in:
BurnyLlama 2021-11-02 11:32:59 +01:00
parent f2ae45b2b8
commit 5319966d7f
4 changed files with 48 additions and 26 deletions

View File

@ -1,5 +1,6 @@
import { Router } from 'express' import { Router } from 'express'
import { sqlite } from '../libs/database/init.js' import { sqlite } from '../libs/database/init.js'
import searcher from '../libs/database/searcher.js'
const playerApi = Router() const playerApi = Router()
@ -7,40 +8,57 @@ const playerApi = Router()
playerApi.get( playerApi.get(
'/get/:player', '/get/:player',
async (req, res) => { async (req, res) => {
let player = req.params.player searcher(
'points',
/* Misc */ 'player',
const firstFinish = sqlite.prepare(`SELECT * FROM race WHERE player = ? ORDER BY date ASC LIMIT 1`).get(player) req.params.player,
undefined,
/* Points */ false,
let points = {} "get",
const pointsData = sqlite.prepare(`SELECT type, rank, points FROM points WHERE player = ?`) 0
).then(
for (const pointsType of pointsData.iterate(player)) { player => res.json({
points[pointsType.type] = pointsType
}
return res.json({
success: true, success: true,
response: { response: player
firstFinish,
points,
}
}) })
).catch(
error => res.json({
success: false,
response: error
})
)
} }
) )
playerApi.get( playerApi.get(
'/search', '/search',
async (req, res) => { async (req, res) => {
/* Check if a query was provided */
if (!req.query.q) { if (!req.query.q) {
return res.json({ return res.json({
success: false, 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
})
)
} }
) )

View File

@ -8,7 +8,8 @@ import { downloadEssentialData } from './libs/download/dowload.js'
loadEnv() loadEnv()
await downloadEssentialData() if (process.env.DOWNLOAD_FILES === "true")
await downloadEssentialData()
dbInit() dbInit()
generateDB() generateDB()

View File

@ -30,7 +30,7 @@ export default function searcher(table, matchField=undefined, matchQuery=undefin
sqlite sqlite
.prepare(` .prepare(`
SELECT count(*) FROM ${simpleSanitize(table)} SELECT count(*) FROM ${simpleSanitize(table)}
${matchField ? `WHERE ${simpleSanitize(matchField)} = $matchQuery` : ""} ${matchField ? `WHERE ${simpleSanitize(matchField)} LIKE $matchQuery` : ""}
`) `)
.get({ .get({
matchQuery matchQuery
@ -44,7 +44,7 @@ export default function searcher(table, matchField=undefined, matchQuery=undefin
const result = sqlite const result = sqlite
.prepare(` .prepare(`
SELECT * FROM ${simpleSanitize(table)} SELECT * FROM ${simpleSanitize(table)}
${matchField ? `WHERE ${simpleSanitize(matchField)} = $matchQuery` : ""} ${matchField ? `WHERE ${simpleSanitize(matchField)} LIKE $matchQuery` : ""}
${orderBy ? `ORDER BY ${simpleSanitize(orderBy)} ${descending === true ? "DESC" : "ASC"}` : ""} ${orderBy ? `ORDER BY ${simpleSanitize(orderBy)} ${descending === true ? "DESC" : "ASC"}` : ""}
${method === "all" ? `LIMIT ${entriesPerPage * (page - 1)}, ${entriesPerPage}` : ""} ${method === "all" ? `LIMIT ${entriesPerPage * (page - 1)}, ${entriesPerPage}` : ""}
`) `)

View File

@ -12,6 +12,9 @@ MSGPACK_PATH = "data/players.msgpack"
# Should the server try to generate the database? # Should the server try to generate the database?
GENERATE_DB = "true" GENERATE_DB = "true"
# Should download files from DDNet servers?
DOWNLOAD_FILES = "true"
# The port on which the server listens... # The port on which the server listens...
PORT = 12345 PORT = 12345