diff --git a/db.sqlite b/db.sqlite index 7f7119b..d893593 100644 Binary files a/db.sqlite and b/db.sqlite differ diff --git a/server/api/project/[projectId]/tags/[tag].get.ts b/server/api/project/[projectId]/tags/[tag].get.ts index b1bcb54..d920d59 100644 --- a/server/api/project/[projectId]/tags/[tag].get.ts +++ b/server/api/project/[projectId]/tags/[tag].get.ts @@ -20,16 +20,13 @@ export default defineCachedEventHandler(async (e) => { const where = ["project = $project", "tag = $tag"]; const criteria: Record = { $project: project, $tag: tag }; - if(where.length > 1) + const db = useDatabase(); + + const content = db.query(`SELECT * FROM explorer_tags WHERE ${where.join(" and ")}`).get(criteria) as Tag; + + if(content !== undefined) { - const db = useDatabase(); - - const content = db.query(`SELECT * FROM explorer_tags WHERE ${where.join(" and ")}`).get(criteria) as Tag; - - if(content !== undefined) - { - return content; - } + return content; } setResponseStatus(e, 404); diff --git a/server/tasks/sync.ts b/server/tasks/sync.ts index 5a998e5..67ba67e 100644 --- a/server/tasks/sync.ts +++ b/server/tasks/sync.ts @@ -1,13 +1,13 @@ import useDatabase from "~/composables/useDatabase"; import { extname, basename } from 'node:path'; -import type { File, FileType } from '~/types/api'; +import type { File, FileType, Tag } from '~/types/api'; import { CanvasColor, CanvasContent } from "~/types/canvas"; import useMarkdown from "~/composables/useMarkdown"; const typeMapping: Record = { ".md": "Markdown", ".canvas": "Canvas" -} +}; export default defineTask({ meta: { @@ -57,25 +57,36 @@ export default defineTask({ } })); - /*let tags = []; + let tags: Tag[] = []; const tagFile = files.find(e => e.path === "tags"); if(tagFile) { - useMarkdown()(tagFile.content); - tags.push() - }*/ + const parsed = useMarkdown()(tagFile.content); + const titles = parsed.children.filter(e => e.type === 'element' && e.tagName.match(/h\d/)); + for(let i = 0; i < titles.length; i++) + { + const start = titles[i].position?.start.offset ?? 0; + const end = titles.length === i + 1 ? tagFile.content.length : titles[i + 1].position.start.offset - 1; + tags.push({ tag: titles[i].properties.id, description: tagFile.content.substring(titles[i].position?.end.offset + 1, end) }); + } + } const db = useDatabase(); const oldFiles = db.prepare(`SELECT * FROM explorer_files WHERE project = ?1`).all('1') as File[]; - const remove = db.prepare(`DELETE FROM explorer_files WHERE project = ?1 AND path = ?2`); - db.transaction(data => data.forEach(e => remove.run('1', e.path)))(oldFiles.filter(e => !files.find(f => f.path = e.path))); - remove.finalize(); + const removeFiles = db.prepare(`DELETE FROM explorer_files WHERE project = ?1 AND path = ?2`); + db.transaction((data: File[]) => data.forEach(e => removeFiles.run('1', e.path)))(oldFiles.filter(e => !files.find(f => f.path = e.path))); + removeFiles.finalize(); - const insert = db.prepare(`INSERT INTO explorer_files("project", "path", "owner", "title", "order", "type", "content") VALUES (1, $path, 1, $title, $order, $type, $content)`); - const update = db.prepare(`UPDATE explorer_files SET content = $content WHERE project = 1 AND path = $path`); - const transaction = db.transaction((content) => { + const oldTags = db.prepare(`SELECT * FROM explorer_tags WHERE project = ?1`).all('1') as Tag[]; + const removeTags = db.prepare(`DELETE FROM explorer_tags WHERE project = ?1 AND tag = ?2`); + db.transaction((data: Tag[]) => data.forEach(e => removeTags.run('1', e.tag)))(oldTags.filter(e => !tags.find(f => f.tag = e.tag))); + removeTags.finalize(); + + const insertFiles = db.prepare(`INSERT INTO explorer_files("project", "path", "owner", "title", "order", "type", "content") VALUES (1, $path, 1, $title, $order, $type, $content)`); + const updateFiles = db.prepare(`UPDATE explorer_files SET content = $content WHERE project = 1 AND path = $path`); + db.transaction((content) => { for (const item of content) { let order = item.order; @@ -86,23 +97,32 @@ export default defineTask({ order = 999; if(oldFiles.find(e => item.path === e.path)) - { - console.log(`Updating ${item.title} (${item.path})`); - update.run({ $path: item.path, $content: item.content }); - } + updateFiles.run({ $path: item.path, $content: item.content }); else - { - /* console.log(`Inserting ${item.title}`) */ - insert.run({ $path: item.path, $title: item.title, $type: item.type, $content: item.content, $order: order }); - } + insertFiles.run({ $path: item.path, $title: item.title, $type: item.type, $content: item.content, $order: order }); } - }); - transaction(files); + })(files); - insert.finalize(); - update.finalize(); + insertFiles.finalize(); + updateFiles.finalize(); + + const insertTags = db.prepare(`INSERT INTO explorer_tags("project", "tag", "description") VALUES (1, $tag, $description)`); + const updateTags = db.prepare(`UPDATE explorer_tags SET description = $description WHERE project = 1 AND tag = $tag`); + db.transaction((content) => { + for (const item of content) { + if (oldTags.find(e => item.tag === e.tag)) + updateTags.run({ $tag: item.tag, $description: item.description }); + else + insertTags.run({ $tag: item.tag, $description: item.description }); + } + })(tags); + + insertTags.finalize(); + updateTags.finalize(); useStorage('cache').clear(); + + console.log("Sync finished"); return { result: true }; }