Skip to content

Commit

Permalink
🚧 making shot not an admin colletion
Browse files Browse the repository at this point in the history
  • Loading branch information
acidjazz committed Aug 13, 2024
1 parent 9265233 commit bd9acb6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
1 change: 0 additions & 1 deletion app/components/shot/ShotForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const props = defineProps<{ pen: Pen }>()
const emit = defineEmits(['created'])
const form = ref<Form<any>>()
const state = reactive({
user: props.pen.cartridge?.userId,
cartridge: props.pen.cartridge?.id,
units: shotUnits[1],
date: format(new Date(), 'yyyy-MM-dd'),
Expand Down
11 changes: 6 additions & 5 deletions server/routes/shot.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { z } from 'zod'

const index = defineEventHandler(async (event) => {
const { user } = await requireUserSession(event)
return metapi().render(
await prisma.shot.findMany({
where: {
userId: BigInt(auth.user().id),
userId: user.id,
},
}),
)
})

const create = defineEventHandler(async (event) => {
if (!middleware.requireAdmin()) return metapi().notFound(event)
const { user } = await requireUserSession(event)
const schema = z.object({
user: z.string(),
cartridge: z.string(),
units: z.number(),
date: z.string().date(),
Expand All @@ -23,7 +23,7 @@ const create = defineEventHandler(async (event) => {
const shot = await prisma.shot.create({
data: {
cartridgeId: BigInt(parsed.data.cartridge),
userId: BigInt(parsed.data.user),
userId: user.id,
units: parsed.data.units,
date: new Date(parsed.data.date),
},
Expand All @@ -33,13 +33,14 @@ const create = defineEventHandler(async (event) => {
})

const remove = defineEventHandler(async (event) => {
if (!middleware.requireAdmin()) return metapi().notFound(event)
const { user } = await requireUserSession(event)
const schema = z.object({ id: z.number(), user: z.number() })
const parsed = schema.safeParse({ id: event.context.params?.id })
if (!parsed.success) return metapi().error(event, parsed.error.issues, 403)
await prisma.shot.delete({
where: {
id: parsed.data.id,
userId: user.id,
},
})
return metapi().success('shot deleted')
Expand Down

0 comments on commit bd9acb6

Please sign in to comment.