feat(quest): add full CRUD and image handling for hunt quests
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
import { useValidatedBody, useValidatedParams, z, zh } from 'h3-zod';
|
||||
import prisma from '~~/lib/prisma';
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const user = await requireUserSession(event);
|
||||
|
||||
const userId = user.user.id;
|
||||
const { huntId, questId } = await useValidatedParams(
|
||||
event,
|
||||
z.object({
|
||||
huntId: zh.numAsString,
|
||||
questId: zh.numAsString
|
||||
})
|
||||
);
|
||||
|
||||
const { direction } = await useValidatedBody(
|
||||
event,
|
||||
z.object({
|
||||
direction: z.enum(['up', 'down'])
|
||||
})
|
||||
);
|
||||
|
||||
// Verify membership and get quest
|
||||
const quest = await prisma.huntQuest.findUnique({
|
||||
where: {
|
||||
id: questId,
|
||||
huntId,
|
||||
hunt: {
|
||||
members: {
|
||||
some: {
|
||||
memberId: userId
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
order: true
|
||||
}
|
||||
});
|
||||
|
||||
if (!quest) {
|
||||
throw createError({ statusCode: 404, statusMessage: 'Not found!' });
|
||||
}
|
||||
|
||||
const targetOrder = direction === 'up' ? quest.order - 1 : quest.order + 1;
|
||||
|
||||
if (targetOrder < 0) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'Already at top!' });
|
||||
}
|
||||
|
||||
// Find the quest to swap with
|
||||
const swapQuest = await prisma.huntQuest.findFirst({
|
||||
where: {
|
||||
huntId,
|
||||
order: targetOrder
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
order: true
|
||||
}
|
||||
});
|
||||
|
||||
if (!swapQuest) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'Already at bottom!' });
|
||||
}
|
||||
|
||||
// Swap orders in a transaction
|
||||
await prisma.$transaction([
|
||||
prisma.huntQuest.update({
|
||||
where: { id: quest.id },
|
||||
data: { order: swapQuest.order }
|
||||
}),
|
||||
prisma.huntQuest.update({
|
||||
where: { id: swapQuest.id },
|
||||
data: { order: quest.order }
|
||||
})
|
||||
]);
|
||||
|
||||
return { success: true };
|
||||
});
|
||||
Reference in New Issue
Block a user