mans
06/22/2022, 4:45 PMconst cart = await prisma.cart.update({
where: {
id: dbUser.cartId,
},
data: {
Items: {
create: {
product: {
connect: {
id: parseInt(body.productId),
},
},
quantity: parseInt(body.quantity) || 1,
},
},
totalPrice: 12,
},
});
res.json(cart);
});
Here is a part of my schema
model User {
id Int @id @default(autoincrement())
username String @unique
password_hash String
role Role @default(USER)
orders Order[]
cart Cart @relation(fields: [cartId], references: [id])
cartId Int
}
model Cart {
id Int @id @default(autoincrement())
Items CartItem[]
totalPrice Float @default(0)
User User[]
}
model CartItem {
id Int @id @default(autoincrement())
Order Order? @relation(fields: [orderId], references: [id])
orderId Int?
product Product @relation(fields: [productId], references: [id])
productId Int @unique
Cart Cart? @relation(fields: [cartId], references: [id])
cartId Int?
quantity Int @default(1)
}Sabin Adams
07/06/2022, 8:19 PMconst alreadyExists = await prisma.cartItem.count({
where: { productId: body.productId },
});
if (alreadyExists) {
// Run an update
} else {
// Run a create
}
I am:
1. Checking if the product is already in the cart
2. Running an update to increment the quantity & total costs if it does)
3. Adding the data if it doesn’tSabin Adams
07/19/2022, 10:26 PM