nfp_sites/api/page/model.mjs

48 lines
1.1 KiB
JavaScript
Raw Normal View History

2022-07-20 00:33:06 +00:00
import { parseFile } from '../file/util.mjs'
export async function getTree(ctx) {
2022-07-26 02:56:07 +00:00
let res = await ctx.db.safeCallProc('common.pages_get_tree', [])
2022-06-16 22:44:43 +00:00
let out = []
let children = []
let map = new Map()
for (let page of res.first) {
if (!page.parent_id) {
out.push(page)
} else {
children.push(page)
}
map.set(page.id, page)
}
for (let page of children) {
let parent = map.get(page.parent_id)
if (!parent.children) {
parent.children = []
}
parent.children.push(page)
}
return {
tree: out
}
}
2022-06-28 08:15:51 +00:00
export async function getPage(ctx, path, page = 0, per_page = 10) {
2022-07-26 02:56:07 +00:00
let res = await ctx.db.safeCallProc('common.pages_get_single', [path, page, per_page])
2022-07-20 00:33:06 +00:00
let articleMap = new Map()
let out = {
page: res.results[0][0] || null,
articles: res.results[1],
total_articles: res.results[2][0].total_articles,
featured: res.results[4][0] || null
}
out.articles.forEach(article => {
article.files = []
articleMap.set(article.id, article)
})
res.results[3].forEach(file => {
articleMap.get(file.id).files.push(parseFile(file))
})
return out
2022-06-28 08:15:51 +00:00
}