feat(quest): add full CRUD and image handling for hunt quests
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
import { useValidatedParams, z, zh } from 'h3-zod';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import prisma from '~~/lib/prisma';
|
||||
import { minioClient, s3Bucket, s3Host } from '~~/lib/s3';
|
||||
|
||||
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
|
||||
})
|
||||
);
|
||||
|
||||
// Verify membership
|
||||
const quest = await prisma.huntQuest.findUnique({
|
||||
where: {
|
||||
id: questId,
|
||||
huntId,
|
||||
hunt: {
|
||||
members: {
|
||||
some: {
|
||||
memberId: userId
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
select: {
|
||||
id: true
|
||||
}
|
||||
});
|
||||
|
||||
if (!quest) {
|
||||
throw createError({ statusCode: 404, statusMessage: 'Not found!' });
|
||||
}
|
||||
|
||||
const body = await readFormData(event);
|
||||
const formDataObj = Object.fromEntries(body.entries());
|
||||
|
||||
const { image } = z
|
||||
.object({
|
||||
image: z
|
||||
.file()
|
||||
.mime(['image/png', 'image/jpeg', 'image/jpg'])
|
||||
.max(10_000_000)
|
||||
})
|
||||
.parse(formDataObj);
|
||||
|
||||
const uuid = randomUUID();
|
||||
await minioClient.putObject(
|
||||
s3Bucket,
|
||||
uuid,
|
||||
Buffer.from(await image.arrayBuffer()),
|
||||
undefined,
|
||||
{
|
||||
'Cache-Control': 'public, max-age=31536000, immutable',
|
||||
'Content-Type': image.type
|
||||
}
|
||||
);
|
||||
|
||||
await prisma.huntQuest.update({
|
||||
where: {
|
||||
id: questId
|
||||
},
|
||||
data: {
|
||||
picture: {
|
||||
create: {
|
||||
creatorId: userId,
|
||||
url: `https://${s3Bucket}.${s3Host}/${uuid}`,
|
||||
uuid,
|
||||
questId
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return { success: true };
|
||||
});
|
||||
Reference in New Issue
Block a user