Refactored the store system
This commit is contained in:
@@ -3,7 +3,7 @@ import { z } from 'zod'
|
||||
import type { CalendarEvent } from "~/models/CalendarEvent";
|
||||
|
||||
const querySchema = z.object({
|
||||
world_id: z.number({ coerce: true }).positive().int()
|
||||
calendarId: z.number({ coerce: true }).positive().int()
|
||||
})
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
@@ -17,11 +17,15 @@ export default defineEventHandler(async (event) => {
|
||||
title,
|
||||
description,
|
||||
location,
|
||||
world_calendars (id, world_id)
|
||||
startDate:start_date,
|
||||
endDate:end_date,
|
||||
wiki,
|
||||
category:calendar_event_categories!calendar_events_category_fkey (*),
|
||||
secondaryCategories:calendar_event_categories!calendar_event_categories_links (*)
|
||||
`)
|
||||
|
||||
if (query.world_id) {
|
||||
output.eq('world_calendars.world_id', query.world_id)
|
||||
if (query.calendarId) {
|
||||
output.eq('calendar_id', query.calendarId)
|
||||
}
|
||||
|
||||
return output.returns<CalendarEvent[]>()
|
||||
|
||||
@@ -3,36 +3,51 @@ import { z } from 'zod'
|
||||
import type { Calendar } from "~/models/CalendarConfig";
|
||||
|
||||
const querySchema = z.object({
|
||||
world_id: z.number({ coerce: true }).positive().int()
|
||||
id: z.number({ coerce: true }).positive().int().optional(),
|
||||
full: z.boolean({ coerce: true }).optional()
|
||||
})
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const client = await serverSupabaseClient(event)
|
||||
const query = await getValidatedQuery(event, querySchema.parse)
|
||||
|
||||
const output = client
|
||||
.from('calendars')
|
||||
.select(`
|
||||
id,
|
||||
name,
|
||||
today,
|
||||
months:calendar_months (*),
|
||||
events:calendar_events (
|
||||
id,
|
||||
title,
|
||||
description,
|
||||
location,
|
||||
hidden,
|
||||
startDate:start_date,
|
||||
endDate:end_date,
|
||||
wiki,
|
||||
category:calendar_event_categories!calendar_events_category_fkey (*),
|
||||
secondaryCategories:calendar_event_categories!calendar_event_categories_links (*)
|
||||
)
|
||||
`)
|
||||
.eq('world_id', query.world_id)
|
||||
.limit(1)
|
||||
.single<Calendar>()
|
||||
const partialFields = `
|
||||
id,
|
||||
name,
|
||||
today,
|
||||
months:calendar_months (*)
|
||||
`
|
||||
|
||||
return output
|
||||
const fullFields = `
|
||||
id,
|
||||
name,
|
||||
today,
|
||||
months:calendar_months (*),
|
||||
events:calendar_events (
|
||||
id,
|
||||
title,
|
||||
description,
|
||||
location,
|
||||
hidden,
|
||||
startDate:start_date,
|
||||
endDate:end_date,
|
||||
wiki,
|
||||
category:calendar_event_categories!calendar_events_category_fkey (*),
|
||||
secondaryCategories:calendar_event_categories!calendar_event_categories_links (*)
|
||||
)
|
||||
`
|
||||
|
||||
let output
|
||||
|
||||
if (query.full) {
|
||||
output = client.from('calendars').select(fullFields)
|
||||
} else {
|
||||
output = client.from('calendars').select(partialFields)
|
||||
}
|
||||
|
||||
if (query.id) {
|
||||
return output.eq('id', query.id).single<Calendar>()
|
||||
}
|
||||
|
||||
return output.returns<Calendar[]>()
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user