feat: add notes app
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
await deleteRow(event, notesTable(event), notesDocId(event))
|
||||
return { deleted: true }
|
||||
})
|
||||
@@ -0,0 +1,3 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
return toNotesDoc(await getRow<NotesDocRow>(event, notesTable(event), notesDocId(event)))
|
||||
})
|
||||
@@ -0,0 +1,5 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
const changes = await readValidBody(event, notesDocUpdateSchema)
|
||||
const row = await updateRow<NotesDocRow>(event, notesTable(event), notesDocId(event), { ...changes, updated: notesNow() })
|
||||
return toNotesDocSummary(row)
|
||||
})
|
||||
@@ -0,0 +1,11 @@
|
||||
// ?q= searches title and text with Baserow's search. Baserow only searches (and filters) the fields a request
|
||||
// includes, so a search has to include `content` even though the list only needs the summary.
|
||||
export default defineEventHandler(async (event) => {
|
||||
const q = String(getQuery(event).q ?? '').trim()
|
||||
const query = q
|
||||
? { include: `${NOTES_SUMMARY_FIELDS},content`, search: q }
|
||||
: { include: NOTES_SUMMARY_FIELDS }
|
||||
|
||||
const rows = await listRows<NotesDocRow>(event, notesTable(event), query)
|
||||
return rows.map(toNotesDocSummary)
|
||||
})
|
||||
@@ -0,0 +1,4 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
const row = await createRow<NotesDocRow>(event, notesTable(event), { title: '', content: '', created: notesNow(), updated: notesNow() })
|
||||
return toNotesDoc(row)
|
||||
})
|
||||
@@ -0,0 +1,40 @@
|
||||
import type { H3Event } from 'h3'
|
||||
|
||||
export interface NotesDocRow {
|
||||
id: number
|
||||
title: string | null
|
||||
content?: string | null
|
||||
created: string | null
|
||||
updated: string | null
|
||||
}
|
||||
|
||||
export const NOTES_SUMMARY_FIELDS = 'title,created,updated'
|
||||
|
||||
export function notesTable(event: H3Event): string {
|
||||
const { baserowNotesAppTableId } = useRuntimeConfig(event)
|
||||
if (!baserowNotesAppTableId) {
|
||||
throw createError({ statusCode: 500, statusMessage: 'NUXT_BASEROW_NOTES_APP_TABLE_ID is not set' })
|
||||
}
|
||||
return baserowNotesAppTableId
|
||||
}
|
||||
|
||||
export function notesDocId(event: H3Event): number {
|
||||
const id = Number(getRouterParam(event, 'id'))
|
||||
if (!Number.isInteger(id) || id < 1) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'Invalid note' })
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
/** `created` and `updated` are plain date fields, so the server keeps them up to date */
|
||||
export function notesNow(): string {
|
||||
return new Date().toISOString()
|
||||
}
|
||||
|
||||
export function toNotesDocSummary(row: NotesDocRow): NotesDocSummary {
|
||||
return { id: row.id, title: (row.title ?? '').trim(), created: row.created, updated: row.updated }
|
||||
}
|
||||
|
||||
export function toNotesDoc(row: NotesDocRow): NotesDoc {
|
||||
return { ...toNotesDocSummary(row), content: row.content ?? '' }
|
||||
}
|
||||
Reference in New Issue
Block a user