Added correct RLS and month api routes
This commit is contained in:
50
server/api/calendars/months/create.post.ts
Normal file
50
server/api/calendars/months/create.post.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { serverSupabaseClient } from "#supabase/server";
|
||||
import { calendarMonthSchema, type CalendarMonth } from "~/models/CalendarMonth"
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const client = await serverSupabaseClient(event)
|
||||
const { data: bodyData, error: schemaError } = await readValidatedBody(event, body => calendarMonthSchema.safeParse(body))
|
||||
|
||||
if (schemaError) {
|
||||
console.log(schemaError)
|
||||
throw createError({
|
||||
cause: 'Utilisateur',
|
||||
fatal: false,
|
||||
message: "Le schéma de la requête n'est pas complet ou mal renseigné.",
|
||||
status: 401,
|
||||
})
|
||||
}
|
||||
|
||||
try {
|
||||
const { data, error } = await client
|
||||
.from('calendar_months')
|
||||
.insert(
|
||||
{
|
||||
name: bodyData?.name,
|
||||
days: bodyData.days,
|
||||
position: bodyData?.position,
|
||||
calendar_id: bodyData.calendar_id,
|
||||
} as never
|
||||
)
|
||||
.select(`
|
||||
id,
|
||||
name,
|
||||
days,
|
||||
position,
|
||||
calendar_id
|
||||
`)
|
||||
.single<CalendarMonth>()
|
||||
|
||||
if (error) throw error
|
||||
|
||||
return data
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
throw createError({
|
||||
cause: 'Serveur',
|
||||
status: 500,
|
||||
fatal: false,
|
||||
message: 'Une erreur inconnue est survenue.'
|
||||
})
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user