-
+
-
-
-
-
- -
-
-
-
+
+
-
-
-
-
-
- jour(s)
-
-
-
-
-
-
-
-
-
-
- Supprimer {{ m.name }} du calendrier
-
-
-
-
-
-
-
+
+
+
+
+
+
+ -
+
+
+
+
+
+
+ jour(s)
+
+
+
+
+
+
+
+
+
+
+ Supprimer {{ m.name }} du calendrier
+
+
+
+
+
+
+
+
+
+ Aucun mois pour l'instant
+
diff --git a/components/calendar/input/TodaySelect.vue b/components/calendar/input/TodaySelect.vue
new file mode 100644
index 0000000..a0b2baa
--- /dev/null
+++ b/components/calendar/input/TodaySelect.vue
@@ -0,0 +1,69 @@
+
+
+
+
+
+
+
+
+
+
+
+
+ Aucun mois disponible
+
+
+ {{ props.availableMonths[model.month - 1].name }}
+
+
+ Choisir le mois
+
+
+
+
+ Mois disponibles
+
+
+ {{ m.name }}
+
+
+
+
+
+
+
diff --git a/models/CalendarConfig.ts b/models/CalendarConfig.ts
index c9c5360..3c3af20 100644
--- a/models/CalendarConfig.ts
+++ b/models/CalendarConfig.ts
@@ -1,6 +1,6 @@
import { z } from 'zod'
import type { CalendarEvent } from "./CalendarEvent"
-import { type CalendarMonth } from "./CalendarMonth"
+import { calendarMonthSchema, type CalendarMonth } from "./CalendarMonth"
import { dateSchema, type RPGDate } from "./Date"
export interface CalendarConfig {
@@ -19,5 +19,6 @@ export const postCalendarSchema = z.object({
name: z.string(),
today: dateSchema.optional().nullable(),
color: z.string().optional().nullable(),
+ months: z.array(calendarMonthSchema).min(1),
worldId: z.number().int(),
})
diff --git a/models/CalendarMonth.ts b/models/CalendarMonth.ts
index 7104ce3..4a14df9 100644
--- a/models/CalendarMonth.ts
+++ b/models/CalendarMonth.ts
@@ -10,5 +10,6 @@ export interface CalendarMonth {
export const calendarMonthSchema = z.object({
name: z.string().max(64),
days: z.number().int().min(12),
- calendarId: z.number().int()
+ position: z.number().int().optional().nullable(),
+ calendar_id: z.number().int().optional().nullable()
})
diff --git a/pages/i/world/[id]/index.vue b/pages/i/world/[id]/index.vue
index 2505e49..e1df308 100644
--- a/pages/i/world/[id]/index.vue
+++ b/pages/i/world/[id]/index.vue
@@ -90,10 +90,10 @@ const modalOpened = ref
(false)
-
+
- Créer un calendrier
+ {{ world.name }} — Nouveau calendrier
diff --git a/server/api/calendars/create.post.ts b/server/api/calendars/create.post.ts
index bedafda..b9a3986 100644
--- a/server/api/calendars/create.post.ts
+++ b/server/api/calendars/create.post.ts
@@ -1,5 +1,6 @@
import { serverSupabaseClient } from "#supabase/server";
// import type { Calendar} from "~/models/CalendarConfig";
+import type { Calendar} from "~/models/CalendarConfig";
import { postCalendarSchema } from "~/models/CalendarConfig";
export default defineEventHandler(async (event) => {
@@ -7,7 +8,6 @@ export default defineEventHandler(async (event) => {
const { data: bodyData, error: schemaError } = await readValidatedBody(event, body => postCalendarSchema.safeParse(body))
if (schemaError) {
- console.log(bodyData)
console.log(schemaError)
throw createError({
cause: 'Utilisateur',
@@ -17,7 +17,8 @@ export default defineEventHandler(async (event) => {
})
}
- let calendarObject: any
+ // Store calendarId for rollback / links
+ let calendarId: number | null = null
// First insert the calendar
try {
@@ -33,10 +34,68 @@ export default defineEventHandler(async (event) => {
)
.select(`id`)
.single()
- console.log(data)
+
+ if (data?.id) {
+ calendarId = data.id
+ }
if (error) throw error
} catch (err) {
console.log(err)
+ throw createError({
+ cause: 'Serveur',
+ fatal: false,
+ message: "Une erreur inconnue s'est produite pendant la création du calendrier.",
+ status: 500,
+ })
+ }
+
+ // Setup months data
+ for (let i = 0; i < bodyData.months.length; i++) {
+ bodyData.months[i].calendar_id = calendarId
+ bodyData.months[i].position = i
+ }
+
+ // Bulk insert the months
+ try {
+ const { error } = await client
+ .from('calendar_months')
+ .insert(bodyData.months as never)
+
+ if (error) throw error
+ } catch (err) {
+ // If we found an error, rollback the calendar
+ if (calendarId) {
+ await client
+ .from('calendars')
+ .delete()
+ .eq('id', calendarId)
+ .single()
+ }
+
+ throw createError({
+ cause: 'Serveur',
+ fatal: false,
+ message: "Une erreur inconnue s'est produite pendant la création des mois du calendrier.",
+ status: 500,
+ })
+ }
+
+ if (calendarId) {
+ try {
+ return client
+ .from('calendars')
+ .select("*")
+ .eq('id', calendarId)
+ .limit(1)
+ .single()
+ } catch (err) {
+ throw createError({
+ cause: 'Serveur',
+ fatal: false,
+ message: "Impossible de récupérer les données du calendrier créé.",
+ status: 500,
+ })
+ }
}
})