Skip to content

Commit

Permalink
Convert query params to REST endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
akelsch committed Jul 2, 2020
1 parent 58bbc56 commit fe9cad0
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 26 deletions.
16 changes: 11 additions & 5 deletions lib/routes/cases.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import { findAllCases } from '../services/cases'
import { assertNoQueryParamMissing } from '../utils/errors'
import { findAllCases, findCasesByStateId } from '../services/cases'

import express from 'express'
const router = express.Router()

router.get('/', async (req, res, next) => {
try {
const { stateId } = req.query
assertNoQueryParamMissing({ stateId })
res.json(await findAllCases(stateId))
res.json(await findAllCases())
} catch (err) {
next(err)
}
})

router.get('/:stateId', async (req, res, next) => {
try {
const { stateId } = req.params
res.json(await findCasesByStateId(stateId))
} catch (err) {
next(err)
}
Expand Down
19 changes: 15 additions & 4 deletions lib/routes/mapdata.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import { getMapdata } from '../services/mapdata'
import { findAllMapdata, findMapdataByStateId } from '../services/mapdata'
import { assertNoQueryParamMissing } from '../utils/errors'

import express from 'express'
const router = express.Router()

router.get('/', async (req, res, next) => {
try {
const { stateId, resolution, zoom } = req.query
assertNoQueryParamMissing({ stateId, resolution, zoom })
res.json(await getMapdata(stateId, resolution, zoom))
const { resolution, zoom } = req.query
assertNoQueryParamMissing({ resolution, zoom })
res.json(await findAllMapdata(resolution, zoom))
} catch (err) {
next(err)
}
})

router.get('/:stateId', async (req, res, next) => {
try {
const { stateId } = req.params
const { resolution, zoom } = req.query
assertNoQueryParamMissing({ resolution, zoom })
res.json(await findMapdataByStateId(stateId, resolution, zoom))
} catch (err) {
next(err)
}
Expand Down
6 changes: 2 additions & 4 deletions lib/routes/options.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { findOption, saveOption } from '../services/options'
import { assertNoQueryParamMissing } from '../utils/errors'

import express from 'express'
const router = express.Router()

router.get('/', async (req, res, next) => {
router.get('/:guid', async (req, res, next) => {
try {
const { guid } = req.query
assertNoQueryParamMissing({ guid })
const { guid } = req.params
res.json(await findOption(guid))
} catch (err) {
next(err)
Expand Down
12 changes: 8 additions & 4 deletions lib/services/cases.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import sequelize from 'sequelize'

import Case from '../models/Case'

export function findAllCases (stateId) {
export function findAllCases () {
return Case.findAll()
}

export function findCasesByStateId (stateId) {
return Case.findAll({
where: sequelize.literal(`"stateId" = ${stateId} OR ${stateId} = 0`)
where: {
stateId: stateId
}
})
}
12 changes: 10 additions & 2 deletions lib/services/mapdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ import sequelize from 'sequelize'

import Mapdata from '../models/Mapdata'

export async function getMapdata (stateId, resolution, zoom) {
export function findAllMapdata (resolution, zoom) {
return getMapdata(resolution, zoom)
}

export function findMapdataByStateId (stateId, resolution, zoom) {
return getMapdata(resolution, zoom, { where: { stateId: stateId } })
}

async function getMapdata (resolution, zoom, options) {
// Wichtig: Erst zoom, dann resolution!
const mapdata = await Mapdata.findAll({
attributes: [
Expand All @@ -11,7 +19,7 @@ export async function getMapdata (stateId, resolution, zoom) {
sequelize.fn('ST_Scale', sequelize.col('geometry'), ...getScaleFactor(zoom)), getEpsilon(resolution)),
'geometry']
],
where: sequelize.literal(`"stateId" = ${stateId} OR ${stateId} = 0`)
...options
})

return mapdata.filter(data => data.geometry !== null)
Expand Down
8 changes: 4 additions & 4 deletions lib/utils/rki.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ async function get (url) {
return response.data
}

export async function fetchCases () {
return await get(CASES_URL)
export function fetchCases () {
return get(CASES_URL)
}

export async function fetchMapdata () {
return await get(MAPDATA_URL)
export function fetchMapdata () {
return get(MAPDATA_URL)
}
8 changes: 5 additions & 3 deletions public/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ form.onsubmit = event => {
form.dispatchEvent(new Event('change'))

function getFormURL () {
const url = new URL(form.action)
const queryParams = new URLSearchParams(new FormData(form))
url.search = queryParams
const params = new URLSearchParams(new FormData(form))
const stateId = params.get('stateId')
const url = stateId === '0' ? new URL(form.action) : new URL(`${form.action}/${stateId}`)
params.delete('stateId')
url.search = params
return url
}

Expand Down

0 comments on commit fe9cad0

Please sign in to comment.