Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/LucaXGit/proyecto-final-jaz/llms.txt

Use this file to discover all available pages before exploring further.

Sending a POST request with accion=vender decrements the stock of a product by a given quantity. The operation is atomic: the SQL statement only executes the decrement if the current stock is sufficient, preventing overselling even under concurrent requests. If the update affects zero rows — because the product does not exist or has insufficient stock — the servlet returns {"success": false}.

Request

Method: POST
URL: http://localhost:8080/ServidorTiendaPlayeras/ProductoServlet
Content-Type: application/x-www-form-urlencoded

Parameters

accion
string
required
Dispatch key for the POST handler. Must be exactly "vender" to reach the sell branch.
id
integer
required
The primary-key ID of the product to sell.
cantidad
integer
The number of units to sell. Defaults to 1 if this parameter is omitted or not provided.

curl Example

curl -X POST http://localhost:8080/ServidorTiendaPlayeras/ProductoServlet \
  -d "accion=vender&id=1&cantidad=3"

PHP (Laravel) Example

The PlayerasController::vender() method casts both id and cantidad to integers before sending to avoid type coercion issues:
Http::asForm()->post($this->apiUrl, [
    'accion'   => 'vender',
    'id'       => (int) $id,
    'cantidad' => (int) $request->cantidad,
]);

SQL Executed

UPDATE productos SET stock = stock - ? WHERE id = ? AND stock >= ?

Response

success
boolean
true if the stock was decremented successfully (the sale was recorded); false if the product was not found or did not have enough stock to fulfill the requested quantity.
Success:
{ "success": true }
Failure (insufficient stock or product not found):
{ "success": false }

Atomic Stock Guard

The sell operation uses a single UPDATE statement with a conditional WHERE clause to prevent overselling without requiring a separate read or a transaction lock:
UPDATE productos SET stock = stock - ? WHERE id = ? AND stock >= ?
The AND stock >= ? condition means the database will only modify the row if the current stock value is greater than or equal to the requested cantidad. If the condition is not met, the statement updates zero rows, and the DAO layer interprets that as a failed sale, returning false to the servlet which then responds with {"success": false}. This approach is race-condition safe: because the check and the decrement happen inside a single atomic SQL statement, two concurrent requests cannot both read the same stock value and both succeed when only one unit remains.
If cantidad is omitted from the request, the servlet defaults it to 1. You can therefore sell a single unit by sending only accion=vender&id={id} without specifying cantidad.

Build docs developers (and LLMs) love