-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add admin page so I can create events
- Loading branch information
1 parent
dcec658
commit 68ec2a1
Showing
5 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default defineNuxtRouteMiddleware((to, from) => { | ||
const user = useUser(); | ||
if (user.value?.admin) { | ||
return navigateTo('/', { replace: true }) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<script setup lang="ts"> | ||
definePageMeta({ | ||
middlewate: ["admin"] | ||
}) | ||
const name = ref('') | ||
const startAt = ref('') | ||
const endAt = ref('') | ||
const photoLimit = ref(25); | ||
const createEvent = async () => { | ||
const res = await $fetch("/api/events", { | ||
method: "POST", | ||
body: JSON.stringify({ | ||
name: name.value, | ||
startAt: new Date(startAt.value).toISOString(), | ||
endAt: new Date(endAt.value).toISOString(), | ||
photoLimit: photoLimit.value | ||
}), | ||
headers: { | ||
"Content-Type": "application/json" | ||
} | ||
}) | ||
} | ||
</script> | ||
<template> | ||
<div class="container p-4 mx-auto"> | ||
<div class="flex flex-col gap-4 items-center"> | ||
<UInput v-model="name" color="primary" variant="outline" placeholder="Name" /> | ||
<input v-model="startAt" type="datetime-local" placeholder="Start at" /> | ||
<input v-model="endAt" type="datetime-local" placeholder="End at" /> | ||
<UInput v-model="photoLimit" type="number" min="5" max="50" color="primary" variant="outline" | ||
placeholder="Photolimiet" /> | ||
<UButton label="create" @click="createEvent" /> | ||
</div> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { z } from "zod"; | ||
import { prisma } from "~/server/util/db"; | ||
|
||
const newEventSchema = z.object({ | ||
name: z.string(), | ||
startAt: z.string().datetime(), | ||
endAt: z.string().datetime(), | ||
photoLimit: z.number(), | ||
}) | ||
|
||
export default defineEventHandler(async (event) => { | ||
if (!event.context.user?.id) { | ||
throw createError({ | ||
statusCode: 403, | ||
}); | ||
} | ||
if (!event.context.user.admin) { | ||
throw createError({ | ||
statusCode: 401, | ||
}); | ||
} | ||
|
||
const result = await readValidatedBody(event, b => newEventSchema.safeParse(b)); | ||
if (!result.success) { | ||
throw createError({ | ||
statusCode: 400, | ||
statusMessage: "Invalid body", | ||
}); | ||
} | ||
const body = result.data; | ||
|
||
await prisma.event.create({ | ||
data: { | ||
name: body.name, | ||
startAt: new Date(body.startAt), | ||
endAt: new Date(body.endAt), | ||
photoLimit: body.photoLimit, | ||
} | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters