initiation librodrome
This commit is contained in:
24
server/api/messages/index.post.ts
Normal file
24
server/api/messages/index.post.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body = await readBody<{ author: string; email?: string; text: string }>(event)
|
||||
|
||||
if (!body.author?.trim() || !body.text?.trim()) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'Nom et message requis' })
|
||||
}
|
||||
|
||||
const data = await readYaml<{ messages: any[] }>('messages.yml')
|
||||
|
||||
const maxId = data.messages.reduce((max, m) => Math.max(max, m.id || 0), 0)
|
||||
|
||||
data.messages.push({
|
||||
id: maxId + 1,
|
||||
author: body.author.trim(),
|
||||
email: body.email?.trim() || '',
|
||||
text: body.text.trim(),
|
||||
published: false,
|
||||
createdAt: new Date().toISOString(),
|
||||
})
|
||||
|
||||
await writeYaml('messages.yml', data)
|
||||
|
||||
return { ok: true }
|
||||
})
|
||||
Reference in New Issue
Block a user