Skip to content

Commit

Permalink
✨ proper equipment handling
Browse files Browse the repository at this point in the history
  • Loading branch information
acidjazz committed Aug 13, 2024
1 parent c26bef2 commit 9265233
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
12 changes: 6 additions & 6 deletions app/pages/users/[user]/equipment.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { User } from '~/types/models'
const route = useRoute()
const penModal = ref(false)
const cartridgeModal = ref(false)
const { data: user } = await useApi().api<MetapiResponse<User>>(`/api/user/${route.params.user}`)
const { data: user } = await useApi().fetch<MetapiResponse<User>>(`/api/user/${route.params.user}`)
useCrumb()
.custom({
Expand All @@ -16,7 +16,7 @@ useCrumb()
},
)
.custom({
label: user.value.data.name as string,
label: user.value?.data.name as string,
icon: 'i-mdi-account',
})
.custom({
Expand All @@ -26,8 +26,8 @@ useCrumb()
.action({ label: 'Add a Pen', click: () => penModal.value = true })
.action({ label: 'Add a Cartridge', click: () => cartridgeModal.value = true })
const { data: pens, refresh: penRefresh } = await useApi().api<MetapiResponse<Pen[]>>(`/api/user/${route.params.user}/pen`)
const { data: cartridges, refresh: cartRefresh } = await useApi().api<MetapiResponse<Cartridge[]>>(`/api/user/${route.params.user}/cartridge`)
const { data: pens, refresh: penRefresh } = await useApi().fetch<MetapiResponse<Pen[]>>(`/api/user/${route.params.user}/pen`)
const { data: cartridges, refresh: cartRefresh } = await useApi().fetch<MetapiResponse<Cartridge[]>>(`/api/user/${route.params.user}/cartridge`)
const refresh = () => {
penRefresh()
Expand All @@ -44,9 +44,9 @@ const reload = () => {
<template>
<div class="flex flex-col space-y-8">
<div class="text-lg font-semibold">Pens </div>
<pen-list :pens="pens.data" :cartridges="cartridges.data" @reload="reload" />
<pen-list :pens="pens?.data" :cartridges="cartridges?.data" @reload="reload" />
<div class="text-lg font-semibold">Cartridges </div>
<cartridge-list :cartridges="cartridges.data" @reload="reload" />
<cartridge-list :cartridges="cartridges?.data" @reload="reload" />
<u-dashboard-modal
v-model="penModal"
title="Add a pen"
Expand Down
12 changes: 8 additions & 4 deletions server/routes/cartridges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { z } from 'zod'
import { cartridgeContents, cartridgeMgs, cartridgeMls } from '~/utils/shared'

const index = defineEventHandler(async (event) => {
if (!middleware.requireAdmin()) return metapi().notFound(event)
const { user } = await requireUserSession(event)
if (!user.isAdmin) return metapi().notFound(event)
const schema = z.object({ id: z.number() })
const parsed = schema.safeParse({ id: Number.parseInt(event.context.params?.user as string) })
if (!parsed.success) return metapi().error(event, parsed.error.issues, 400)
Expand All @@ -20,7 +21,8 @@ const index = defineEventHandler(async (event) => {
})

const create = defineEventHandler(async (event) => {
if (!middleware.requireAdmin()) return metapi().notFound(event)
const { user } = await requireUserSession(event)
if (!user.isAdmin) return metapi().notFound(event)
const schema = z.object({
user: z.string(),
content: z.enum(cartridgeContents as [string, ...string[]]),
Expand All @@ -42,7 +44,8 @@ const create = defineEventHandler(async (event) => {
})

const get = defineEventHandler(async (event) => {
if (!middleware.requireAdmin()) return metapi().notFound(event)
const { user } = await requireUserSession(event)
if (!user.isAdmin) return metapi().notFound(event)
const schema = z.object({ id: z.number(), user: z.number() })
const parsed = schema.safeParse({
id: event.context.params?.id,
Expand All @@ -59,7 +62,8 @@ const get = defineEventHandler(async (event) => {
})

const remove = defineEventHandler(async (event) => {
if (!middleware.requireAdmin()) return metapi().notFound(event)
const { user } = await requireUserSession(event)
if (!user.isAdmin) return metapi().notFound(event)
const schema = z.object({ id: z.number(), user: z.number() })
const parsed = schema.safeParse({ id: event.context.params?.id, user: Number.parseInt(event.context.params?.user as string) })
if (!parsed.success) return metapi().error(event, parsed.error.issues, 403)
Expand Down
3 changes: 1 addition & 2 deletions server/routes/pen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ const create = defineEventHandler(async (event) => {
const pen = await prisma.pen.create({
data: {
color: parsed.data.color,
// user: { connect: { id: BigInt(auth.user().id) } },
userId: BigInt(user.id),
userId: user.id,
cartridgeId: null,
},
})
Expand Down
14 changes: 10 additions & 4 deletions server/routes/pens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { z } from 'zod'
import { penColors } from '~/utils/shared'

const index = defineEventHandler(async (event) => {
if (!middleware.requireAdmin()) return metapi().notFound(event)
const { user } = await requireUserSession(event)
if (!user.isAdmin) return metapi().notFound(event)
const schema = z.object({ id: z.number() })
const parsed = schema.safeParse({ id: Number.parseInt(event.context.params?.user as string) })
if (!parsed.success) return metapi().error(event, parsed.error.issues, 400)
Expand All @@ -23,7 +24,8 @@ const index = defineEventHandler(async (event) => {
})

const create = defineEventHandler(async (event) => {
if (!middleware.requireAdmin()) return metapi().notFound(event)
const { user } = await requireUserSession(event)
if (!user.isAdmin) return metapi().notFound(event)
const schema = z.object({
user: z.string(),
color: z.enum(penColors as [string, ...string[]]),
Expand All @@ -42,7 +44,8 @@ const create = defineEventHandler(async (event) => {
})

const update = defineEventHandler(async (event) => {
if (!middleware.requireAdmin()) return metapi().notFound(event)
const { user } = await requireUserSession(event)
if (!user.isAdmin) return metapi().notFound(event)
const schema = z.object({
id: z.number(),
user: z.number(),
Expand All @@ -68,6 +71,8 @@ const update = defineEventHandler(async (event) => {
})

const get = defineEventHandler(async (event) => {
const { user } = await requireUserSession(event)
if (!user.isAdmin) return metapi().notFound(event)
const schema = z.object({ id: z.number(), user: z.number() })
const parsed = schema.safeParse({
id: event.context.params?.id,
Expand All @@ -84,7 +89,8 @@ const get = defineEventHandler(async (event) => {
})

const remove = defineEventHandler(async (event) => {
if (!middleware.requireAdmin()) return metapi().notFound(event)
const { user } = await requireUserSession(event)
if (!user.isAdmin) return metapi().notFound(event)
const schema = z.object({ id: z.number(), user: z.number() })
const parsed = schema.safeParse({ id: event.context.params?.id, user: Number.parseInt(event.context.params?.user as string) })
if (!parsed.success) return metapi().error(event, parsed.error.issues, 403)
Expand Down

0 comments on commit 9265233

Please sign in to comment.