Basic player API.
This commit is contained in:
parent
c3dfa5c6bf
commit
9ebe393f21
13
api/api.js
Normal file
13
api/api.js
Normal 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
63
api/players/players.js
Normal 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
|
|
@ -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
|
15
index.js
15
index.js
|
@ -3,6 +3,7 @@ import dotenv from 'dotenv'
|
||||||
import sqlite2mongo from './libs/database/sqlite2mongo.js'
|
import sqlite2mongo from './libs/database/sqlite2mongo.js'
|
||||||
import databaseInit from './libs/database/init.js'
|
import databaseInit from './libs/database/init.js'
|
||||||
import initLog from './libs/utills/log.js'
|
import initLog from './libs/utills/log.js'
|
||||||
|
import api from './api/api.js'
|
||||||
|
|
||||||
const log = initLog("[ MAIN ]")
|
const log = initLog("[ MAIN ]")
|
||||||
|
|
||||||
|
@ -11,17 +12,11 @@ dotenv.config()
|
||||||
|
|
||||||
const Server = express()
|
const Server = express()
|
||||||
|
|
||||||
Server.get('/', (req, res) =>
|
Server.use('/api', api)
|
||||||
res.status(200)
|
|
||||||
.json(
|
|
||||||
{
|
|
||||||
status: "OK!",
|
|
||||||
message: "The API is up and running! The current version is: 0.0.1"
|
|
||||||
}
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
await databaseInit()
|
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}.`))
|
Server.listen(process.env.PORT, () => log(`Server started and listening on port ${process.env.PORT}.`))
|
Loading…
Reference in New Issue
Block a user