-
Daniel authored7faf5ace
Forked from
IF3110 / TugasBesar3_2017
53 commits ahead of the upstream repository.
driver.js 1.04 KiB
/*
* Tugas Besar 3 WBD - AMEN
*/
'use strict'
const express = require('express')
const hub = require('../service/hub')
const relay = require('../service/relay')
let app = express.Router()
app.get('/find', function (req, res) {
let location = req.query.location
res.json({
'driver_ids': hub.filter(location)
})
})
app.post('/', async function (req, res) {
let id = req.body.driver_id
let locations = req.body.locations
try {
let userId = await hub.waitAsync(id, locations, function (reject) {
req.connection.on('close', () => reject(new Error('Aborted')))
})
res.json({
'user_id': userId
})
} catch (e) {
console.log(e)
}
})
app.post('/pick', function (req, res) {
let driverId = req.body.driver_id
let userId = req.body.user_id
try {
// Pick driver by user.
hub.pick(driverId, userId)
// Associate user with driver.
relay.associate(userId, driverId)
res.json({ 'status': 'ok' })
} catch (e) {
res.status(500).json({ 'status': 'error' })
}
})
module.exports = app