chore: add all the code from original repo

This commit is contained in:
Riccardo
2024-05-23 16:55:29 +02:00
parent d8f9a215eb
commit 85d66215a7
66 changed files with 16668 additions and 122 deletions

View File

@@ -0,0 +1,76 @@
'use server';
import { nanoid } from 'nanoid';
import { initializeUser } from '../../../../../data/initializeUser';
import { CreateItemFormSchema } from '../../../../../data/types';
import prisma from '../../../../../prisma/prisma';
interface CreateItemActionProps {
open: boolean;
profile?: string;
error?: string;
}
export async function CreateItemAction(
{ profile }: CreateItemActionProps,
formData: FormData
) {
await initializeUser();
const profileId = profile ?? (await prisma.profile.findFirst())?.id;
const formDataObj = Object.fromEntries(formData.entries());
const validatedBody = CreateItemFormSchema.safeParse(formDataObj);
if (!validatedBody.success || !profileId) {
throw new Error('Bad request');
}
const { name, description, price, currency, tag, body, score, regret } =
validatedBody.data;
const newId = nanoid();
try {
await prisma.$transaction([
prisma.item.create({
data: {
id: newId,
name,
description,
price,
currency,
Profile: {
connect: {
id: profileId
}
},
Tag: tag
? {
connect: {
id: tag
}
}
: undefined
}
}),
prisma.itemComment.create({
data: {
body,
score,
regret,
Item: {
connect: {
id: newId
}
}
}
})
]);
} catch (error) {
throw new Error(`Failed to create item`);
}
return { open: false, profile: profileId };
}