Files
pronature-platform/backend/controllers/api/GamesController.js
T
2025-03-20 17:17:50 +02:00

73 lines
2.5 KiB
JavaScript

import express from 'express';
/**
* GamesController. API for the games manager, граничен клас за комуникация с модула за игрови дефиниции
*/
class GamesController{
name = 'gamesApi'
route = '/api/game'
/**
* Initializes the GamesController plugin, инициализация
* @param {App} app The application instance, апликация
*/
init(app){
const router = express.Router();
const { game } = app;
/**
* API: PUT /api/game/ Create or update game, създаване/обновяване на игрова дефиниция
* @function createOrUpdate
* @memberof GamesController
*/
router.put('/', async (req, res)=>{
try{
let data = req.body;
let object = await game[data.id? 'update' : 'create'](req, data)
res.json({status: 'OK', object});
}catch(err){
console.error(err);
res.status(500).json({status: 'ERR', err});
}
});
/**
* API: POST /api/game/ List games by given criteria, търсене в игрови дефиниции
* @function list
* @returns {Game[]}
* @memberof GamesController
*/
router.post('/', async (req, res)=>{
let result = await game.list(req.body);
res.json(result);
})
/**
* API: GET /api/game/:id Retrieve game by ID, извличане на игрова дефиниция
* @function read
* @param {string} id The id of the game, идентификатор на дефиницията
* @returns {Game}
* @memberof GamesController
*/
router.get('/:id', async (req, res)=>{
let object = await game.read(parseInt(req.params.id));
res.json(object);
})
/**
* API: DELETE /api/game/:id Delete game by ID, изтриване на игрова дефиниция
* @function remove
* @param {string} id The id of the game, идентификатор на дефиницията
* @memberof GamesController
*/
router.delete('/:id', async (req, res)=>{
await game.remove(req.params.id);
res.json({status: 'OK'});
})
app.webServer.xapp.use(this.route, router);
}
}
export { GamesController }