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 { 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({
searcher(
'points',
'player',
req.params.player,
undefined,
false,
"get",
0
).then(
player => res.json({
success: true,
response: {
firstFinish,
points,
}
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
})
)
}
)

View File

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

View File

@ -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}` : ""}
`)

View File

@ -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