76 lines
2.3 KiB
JavaScript
76 lines
2.3 KiB
JavaScript
import express from 'express';
|
|
import multipart from 'connect-multiparty';
|
|
|
|
const multipartMiddleware = multipart();
|
|
|
|
/**
|
|
* GameObjectsController. API for the game objects manager
|
|
*/
|
|
class GameObjectsController{
|
|
|
|
name = 'gameObjectsApi'
|
|
route = '/api/game-object'
|
|
|
|
/**
|
|
* Initializes the GameObjectsController plugin
|
|
* @param {App} app The application instance
|
|
*/
|
|
init(app){
|
|
const { gameObject } = app;
|
|
const router = express.Router();
|
|
|
|
/**
|
|
* API: PUT /api/game-object/ Create or update game object
|
|
* @function createOrUpdate
|
|
* @memberof GameObjectsController
|
|
*/
|
|
router.put('/', multipartMiddleware, async (req, res)=>{
|
|
try{
|
|
let data = req.body;
|
|
let object = await gameObject[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-object/ List game objects by given criteria
|
|
* @function list
|
|
* @returns {GameObject[]}
|
|
* @memberof GameObjectsController
|
|
*/
|
|
router.post('/', async (req, res)=>{
|
|
let result = await gameObject.list(req.body);
|
|
res.json(result);
|
|
})
|
|
|
|
/**
|
|
* API: GET /api/game-object/:id Retrieve game object by ID
|
|
* @function read
|
|
* @param {string} id The id of the game object
|
|
* @returns {GameObject}
|
|
* @memberof GameObjectsController
|
|
*/
|
|
router.get('/:id', async (req, res)=>{
|
|
let object = await gameObject.read(parseInt(req.params.id));
|
|
res.json(object);
|
|
})
|
|
|
|
/**
|
|
* API: DELETE /api/game-object/:id Delete game object by ID
|
|
* @function remove
|
|
* @param {string} id The id of the game object
|
|
* @memberof GameObjectsController
|
|
*/
|
|
router.delete('/:id', async (req, res)=>{
|
|
await gameObject.remove(req.params.id);
|
|
res.json({status: 'OK'});
|
|
})
|
|
|
|
app.webServer.xapp.use(this.route, router);
|
|
}
|
|
}
|
|
|
|
export {GameObjectsController} |