|
|
|
import fs from 'fs'
|
|
|
|
import siteomatQuery from '@helpers/siteomat-query'
|
|
|
|
|
|
|
|
const { Filter, orderBy, reduce } = siteomatQuery
|
|
|
|
|
|
|
|
/**
|
|
|
|
* handle pages
|
|
|
|
*
|
|
|
|
* @author Björn Hase
|
|
|
|
* @license hhttps://www.gnu.org/licenses/gpl-3.0.en.html GPL-3
|
|
|
|
* @link https://gitea.node001.net/HerrHase/tellme-bot.git
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
export default async function(fastify, opts)
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param {object} request
|
|
|
|
* @param {object} response
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
fastify.get('/v1/:name', async function (request, response) {
|
|
|
|
|
|
|
|
// create file path
|
|
|
|
const filePath = process.env.APP_PUBLIC_PATH + '/' + request.params.name + '.json'
|
|
|
|
|
|
|
|
// if file not exists
|
|
|
|
if (!fs.existsSync(filePath)) {
|
|
|
|
return response
|
|
|
|
.code(404)
|
|
|
|
.send()
|
|
|
|
}
|
|
|
|
|
|
|
|
// loading data
|
|
|
|
const data = JSON.parse(fs.readFileSync(filePath, 'utf8'))
|
|
|
|
|
|
|
|
// results
|
|
|
|
let results = []
|
|
|
|
|
|
|
|
// filter
|
|
|
|
if (request.query.filter) {
|
|
|
|
const filter = new Filter(request.query.filter)
|
|
|
|
|
|
|
|
data.forEach((single) => {
|
|
|
|
if (filter.validate(single)) {
|
|
|
|
results.push(single)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
} else {
|
|
|
|
results = data
|
|
|
|
}
|
|
|
|
|
|
|
|
// getting offset
|
|
|
|
let offset = !request.query.offset ? 0 : request.query.offset
|
|
|
|
|
|
|
|
// getting limit
|
|
|
|
let limit = !request.query.limit ? results.length : request.query.limit
|
|
|
|
|
|
|
|
// offset and limit
|
|
|
|
results = results.slice(offset, limit)
|
|
|
|
|
|
|
|
response
|
|
|
|
.header('Content-Type', 'application/json; charset=utf-8')
|
|
|
|
.code(200)
|
|
|
|
.send(results)
|
|
|
|
})
|
|
|
|
}
|