From 9ebe393f216a4968c2ac9b88354c1261bd102761 Mon Sep 17 00:00:00 2001 From: BurnyLlama Date: Sun, 3 Oct 2021 19:00:41 +0200 Subject: [PATCH] Basic player API. --- api/api.js | 13 +++++++++ api/players/players.js | 63 ++++++++++++++++++++++++++++++++++++++++++ dotenv-template | 4 ++- index.js | 15 ++++------ 4 files changed, 84 insertions(+), 11 deletions(-) create mode 100644 api/api.js create mode 100644 api/players/players.js diff --git a/api/api.js b/api/api.js new file mode 100644 index 0000000..68093e0 --- /dev/null +++ b/api/api.js @@ -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 \ No newline at end of file diff --git a/api/players/players.js b/api/players/players.js new file mode 100644 index 0000000..917093c --- /dev/null +++ b/api/players/players.js @@ -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 \ No newline at end of file diff --git a/dotenv-template b/dotenv-template index cab0778..787b1ec 100644 --- a/dotenv-template +++ b/dotenv-template @@ -1 +1,3 @@ -MONGO_URI = "A URI to the mongodb database." \ No newline at end of file +MONGO_URI = "A URI to the mongodb database." +PORT = 12345 +LOAD_DB = true | false \ No newline at end of file diff --git a/index.js b/index.js index c9e42d7..57f9887 100644 --- a/index.js +++ b/index.js @@ -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}.`)) \ No newline at end of file