nfp_sites/api/page/model.mjs

49 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-07-20 00:33:06 +00:00
import { parseFile } from '../file/util.mjs'
2022-07-26 02:55:37 +00:00
import { parseArticle, parseArticles } from '../article/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,
2022-07-26 02:55:37 +00:00
articles: parseArticles(res.results[1]),
2022-07-20 00:33:06 +00:00
total_articles: res.results[2][0].total_articles,
2022-07-26 02:55:37 +00:00
featured: parseArticle(res.results[4][0]),
2022-07-20 00:33:06 +00:00
}
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
}