Skip to content

Commit

Permalink
Merge pull request #188 from geodatagouv/tusbar/update-services-routes
Browse files Browse the repository at this point in the history
Update services routes
  • Loading branch information
tusbar committed Feb 8, 2019
2 parents ed6c68e + 6dc3554 commit f35fb56
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 55 deletions.
38 changes: 20 additions & 18 deletions lib/api/controllers/feature-types.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict'

const mongoose = require('mongoose')

const {Http404} = require('../errors')
Expand All @@ -8,21 +6,22 @@ const am = require('../middlewares/async')
const FeatureType = mongoose.model('FeatureType')

/* Middlewares */
exports.featureType = (req, res, next, id) => {
FeatureType
.findOne({service: req.params.serviceId, name: id})
.exec((err, featureType) => {
if (err) {
return next(err)
}

if (!featureType) {
return next(new Http404())
}

req.featureType = featureType
next()
})
exports.featureType = async (req, res, next, id) => {
try {
const featureType = await FeatureType.findOne({
service: req.params.serviceId,
name: id
}).exec()

if (!featureType) {
return next(new Http404())
}

req.featureType = featureType
next()
} catch (error) {
next(error)
}
}

/* Actions */
Expand All @@ -36,5 +35,8 @@ exports.list = am(async (req, res) => {
})

exports.show = (req, res) => {
res.send(req.featureType)
res.send({
...req.featureType.toJSON(),
serviceLocation: req.service.location
})
}
3 changes: 1 addition & 2 deletions lib/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const cors = require('cors')
const sentry = require('../utils/sentry')

const registerRecordsRoutes = require('./routes/records')
const registerServicesRoutes = require('./routes/services')

const router = new Router()

Expand All @@ -15,11 +14,11 @@ router.use(json())
router.use(require('./routes/publications'))

registerRecordsRoutes(router)
registerServicesRoutes(router)

router.use('/maintenance', require('./routes/maintenance'))
router.use('/catalogs', require('./routes/catalogs'))
router.use('/links', require('./routes/links'))
router.use('/services', require('./routes/services'))

router.use(sentry.Handlers.errorHandler())
router.use((error, req, res, next) => { // eslint-disable-line no-unused-vars
Expand Down
57 changes: 22 additions & 35 deletions lib/api/routes/services.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,35 @@
const {Router} = require('express')

const services = require('../controllers/services')
const featureTypes = require('../controllers/feature-types')

const {TRANSCODER_URL} = process.env

module.exports = function (app) {
// Params
app.param('serviceId', services.service)
app.param('syncId', services.sync)
app.param('typeName', featureTypes.featureType)

// Routes
app.route('/services')
.get(services.list)
.post(services.create)

app.route('/services/by-protocol/:protocol')
.get(services.list)

app.route('/services/:serviceId')
.get(services.show)
const router = new Router()

app.route('/services/:serviceId/sync')
.post(services.handleSync)
router.param('serviceId', services.service)
router.param('syncId', services.sync)
router.param('typeName', featureTypes.featureType)

app.route('/services/:serviceId/synchronizations')
.get(services.listSyncs)
router.get('/', services.list)
router.post('/', services.create)
router.get('/:serviceId', services.show)

app.route('/services/:serviceId/synchronizations/:syncId')
.get(services.showSync)
router.post('/:serviceId/sync', services.handleSync)

app.route('/services/by-protocol/:protocol/sync-all')
.post(services.syncAllByProtocol)
router.get('/:serviceId/synchronizations', services.listSyncs)
router.get('/:serviceId/synchronizations/:syncId', services.showSync)

app.route('/services/:serviceId/feature-types/:typeName')
.get(featureTypes.show)
router.get('/by-protocol/:protocol', services.list)
router.post('/by-protocol/:protocol/sync-all', services.syncAllByProtocol)

app.route('/services/:serviceId/feature-types/:typeName/download')
.get((req, res) => {
const {serviceId, typeName} = req.params
const {search} = req._parsedUrl
router.get('/:serviceId/feature-types', featureTypes.list)
router.get('/:serviceId/feature-types/:typeName', featureTypes.show)
router.get('/:serviceId/feature-types/:typeName/download', (req, res) => {
const {serviceId, typeName} = req.params
const {search} = req._parsedUrl

res.redirect(`${TRANSCODER_URL}/services/${serviceId}/feature-types/${typeName}${search}`)
})
res.redirect(`${TRANSCODER_URL}/services/${serviceId}/feature-types/${typeName}${search}`)
})

app.route('/services/:serviceId/feature-types')
.get(featureTypes.list)
}
module.exports = router

0 comments on commit f35fb56

Please sign in to comment.