Skip to content

Commit

Permalink
feat: add admin page so I can create events
Browse files Browse the repository at this point in the history
  • Loading branch information
NuttyShrimp committed Apr 21, 2024
1 parent dcec658 commit 68ec2a1
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 1 deletion.
6 changes: 6 additions & 0 deletions middleware/admin.ts
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 })
}
})
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"oslo": "^1.2.0",
"uuid": "^9.0.1",
"vue": "^3.4.21",
"vue-router": "^4.3.0"
"vue-router": "^4.3.0",
"zod": "^3.22.5"
},
"devDependencies": {
"prisma": "^5.12.1",
Expand Down
37 changes: 37 additions & 0 deletions pages/admin.vue
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>
40 changes: 40 additions & 0 deletions server/api/events/index.post.ts
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,
}
})
});
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9556,3 +9556,8 @@ zip-stream@^6.0.1:
archiver-utils "^5.0.0"
compress-commons "^6.0.2"
readable-stream "^4.0.0"

zod@^3.22.5:
version "3.22.5"
resolved "https://registry.yarnpkg.com/zod/-/zod-3.22.5.tgz#b9b09db03f6700b0d0b75bf0dbf0c5fc46155220"
integrity sha512-HqnGsCdVZ2xc0qWPLdO25WnseXThh0kEYKIdV5F/hTHO75hNZFp8thxSeHhiPrHZKrFTo1SOgkAj9po5bexZlw==

0 comments on commit 68ec2a1

Please sign in to comment.