Next.js Discord

Discord Forum

How to setup routes?

Answered
Rex posted this in #help-forum
Open in Discord
RexOP
Here is a basic problem I am facing that I need help me
I have a button which adds to the quantity count in the db and I have a button which substrats from the quantity count in db
How do I writre the put function in the API route so that when the increment button is clicked for the particular route eg /api/product/[productId] the values is updated
but when the decrement button is clicked for the same route /api/product/[productId]

My api route logic for now
export async function PUT(req: Request) {
  // update the quantity count
  const request = await req.json();
  const { quantity, id } = request;

  console.log("request: ", request);

  const { userId } = auth();

  if (!userId) return new NextResponse("Unauthorized", { status: 401 });
  // need the category id
  try {
    const product = await prisma.product.findUnique({
      where: {
        id,
      },
    });

    if (!product) {
      return new NextResponse("Product not found", { status: 404 });
    }

    // Add the new quantity to the existing quantity
    const updatedQuantity = product.quantity + quantity;

    // Update the product with the new quantity
    await prisma.product.update({
      where: {
        id,
      },
      data: {
        quantity: updatedQuantity,
      },
    });
  } catch (error) {
    console.log("ERROR: ", error);
  }
  return NextResponse.json("Working");
}

I can;t have two put functions for teh same route

product schema
model Product {
  id         Int         @id @default(autoincrement())
  itemCode   String      @unique
  company    String
  quantity   Int
  categoryId Int
  Category   Category    @relation(fields: [categoryId], references: [id])
  IssuedItem IssueItem[]

  // list of all the time the item was issued
  @@index([categoryId])
}
Answered by Plott Hound
Something like this
  // Determine the updated quantity based on the operation
    let updatedQuantity;
    if (operation === 'increment') {
      updatedQuantity = product.quantity + quantity;
    } else if (operation === 'decrement') {
      // Make sure not to decrement below 0
      updatedQuantity = Math.max(0, product.quantity - quantity);
    } else {
      return new NextResponse("Invalid operation", { status: 400 });
    }
View full answer

6 Replies

Plott Hound
You can modify your PUT function to accept additional parameters
Eg

const { operation, quantity, id } = request; // Include an operation parameter
Plott Hound
Something like this
  // Determine the updated quantity based on the operation
    let updatedQuantity;
    if (operation === 'increment') {
      updatedQuantity = product.quantity + quantity;
    } else if (operation === 'decrement') {
      // Make sure not to decrement below 0
      updatedQuantity = Math.max(0, product.quantity - quantity);
    } else {
      return new NextResponse("Invalid operation", { status: 400 });
    }
Answer
Plott Hound
Would that suit the needs of your application?
Sorry for formatting I’m on mobile
RexOP
Yes this solve the issue thanks