diff --git a/api/keyframes.js b/api/keyframes.js index 4301859..71bc22e 100644 --- a/api/keyframes.js +++ b/api/keyframes.js @@ -3,6 +3,7 @@ import { uuidv7obj, UUID } from "uuidv7" export const mappings = [ + { method: 'get', url:'/keyframes/:kfid', handler: 'getKeyframe', middlewares: [authGuard]}, { method: 'post', url:'/keyframes', handler: 'listKeyframes', middlewares: [authGuard]}, { method: 'put', url:'/keyframes', handler: 'createKeyframe', middlewares: [authGuard]}, { method: 'put', url:'/keyframes/:kfid', handler: 'renameKeyframe', middlewares: [authGuard]}, @@ -11,6 +12,34 @@ export const mappings = [ export const methods = { + async getKeyframe(req, res){ + const kfid = req.params.kfid + if(!this.utils.isValidUUIDV7(kfid)) { + this.err(req, res,`Cannot create Keyframe ! `, `Invalid Keyframe ID !`, 400) + return + } + let results = await this.db.execute(` + SELECT BIN_TO_UUID(ekf_uuid) AS ekf_uuid, ekf_name, BIN_TO_UUID(ekf_prev_uuid) AS ekf_prev_uuid + FROM p42SIM.edited_keyframes + WHERE ekf_uuid = UUID_TO_BIN(?)`, [kfid]) + + if(results.length!=1){ + this.err(req,res,`Cannot find this keyframe !`, `Keyframe ${kfid} not found !`, 404) + return + } + const data = { info: results[0] } + + results = await this.db.execute(` + SELECT BIN_TO_UUID(ekfs_agent_id) AS aid, BIN_TO_UUID(ekfs_ekf_uuid) AS kfId, + ekfs_atp_id , ekfs_gps_values AS gps_values, ekfs_store_values AS store_values, + atp_props + FROM p42SIM.edited_kf_store + LEFT JOIN p42SIM.agent_types ON ekfs_atp_id = atp_id + WHERE ekfs_ekf_uuid = UUID_TO_BIN(?)`, [kfid]) + data.agents = results + this.ok(req, res, data) + }, + async listKeyframes(req, res) { const[isOk, payload, errors] = this.utils.validateMapObject(req.body, { @@ -29,9 +58,9 @@ export const methods = { const conditions = [] const params = [] - if (payload.kfid !== undefined) { + if (payload.kfId !== undefined) { conditions.push('ekf_uuid = UUID_TO_BIN(?)') - params.push(kfid) + params.push(kfId) } if(payload.prevKfId !== undefined) { @@ -120,10 +149,12 @@ export const methods = { const[isOk, payload, errors] = this.utils.validateMapArray(req.body, { 'aid': ((val, obj) => (this.utils.isValidUUIDV7(val)) ), + 'type': ((val, obj) => (/^[0-9]\d*$/.test(val)) ), 'storeValues': ((val, obj) => (typeof(val)==='object') ), 'gpsValues': ((val, obj) => (typeof(val)==='object') ), }, { 'aid': 'aid', + 'type': 'type', 'storeValues': 'storeValues', 'gpsValues': 'gpsValues', } @@ -137,10 +168,11 @@ export const methods = { for(const entry of payload){ await this.db.execute(` INSERT INTO p42SIM.edited_kf_store - (ekfs_ekf_uuid, ekfs_agent_id, ekfs_store_value, ekfs_gps_values) - VALUES (?, ?, ?, ?)`, + (ekfs_ekf_uuid, ekfs_agent_id, ekfs_atp_id, ekfs_store_values, ekfs_gps_values) + VALUES (?, ?, ?, ?, ?)`, [ UUID.parse(kfid).bytes, - UUID.parse(entry.aid).bytes, + UUID.parse(entry.aid).bytes, + entry.type, entry.storeValues, entry.gpsValues ])