Basic player API.

This commit is contained in:
BurnyLlama 2021-10-03 19:00:41 +02:00
parent c3dfa5c6bf
commit 9ebe393f21
4 changed files with 84 additions and 11 deletions

13
api/api.js Normal file
View File

@ -0,0 +1,13 @@
import { Router } from 'express'
import playerApi from './players/players.js'
const api = Router()
api.get('/', (req, res) => res.json({
success: true,
response: "You connected to DDStats API! :D"
}))
api.use('/players', playerApi)
export default api

63
api/players/players.js Normal file
View File

@ -0,0 +1,63 @@
import { response, Router } from 'express'
import initLog from '../../libs/utills/log.js'
import Player from '../../schemas/Player.js'
const log = initLog("Player API")
const playerApi = Router()
playerApi.get('/countAll', async (req, res) => {
const playerAmount = await Player.count({})
res.json({
success: true,
response: playerAmount
})
})
playerApi.get('/getAll', async (req, res) => {
const players = await Player.find({}).select('name')
res.json({
success: true,
response: players
})
})
playerApi.get('/get/:player', async (req, res) => {
const player = await Player.findOne({ name: req.params.player })
if (!player)
return res.json({
success: false,
response: "No player found!"
})
res.json({
success: true,
response: player
})
})
playerApi.get('/search', async (req, res) => {
if (!req.query.name)
return res.json({
success: false,
response: "No parameter 'name' provided!"
})
const name = req.query.name
const order = req.query.sortBy === "asc" || req.query.sortBy === "asc" ? req.query.sortBy : "asc"
const players = await Player.find({ name: { $regex: name, $options: 'i' }})
if (!players)
return res.json({
success: false,
response: "No players found!"
})
res.json(players)
})
export default playerApi

View File

@ -1 +1,3 @@
MONGO_URI = "A URI to the mongodb database."
MONGO_URI = "A URI to the mongodb database."
PORT = 12345
LOAD_DB = true | false

View File

@ -3,6 +3,7 @@ import dotenv from 'dotenv'
import sqlite2mongo from './libs/database/sqlite2mongo.js'
import databaseInit from './libs/database/init.js'
import initLog from './libs/utills/log.js'
import api from './api/api.js'
const log = initLog("[ MAIN ]")
@ -11,17 +12,11 @@ dotenv.config()
const Server = express()
Server.get('/', (req, res) =>
res.status(200)
.json(
{
status: "OK!",
message: "The API is up and running! The current version is: 0.0.1"
}
)
)
Server.use('/api', api)
await databaseInit()
await sqlite2mongo()
if (process.env.LOAD_DB === "true")
await sqlite2mongo()
Server.listen(process.env.PORT, () => log(`Server started and listening on port ${process.env.PORT}.`))