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