INSERT API (POST)
In order to create a new product, the user needs to enter the name (ProductName), type (ProductType) and price (Price) of the product.
URL:
https://base-project-testing-dot-eshopbox-test-project.el.r.appspot.com/_ah/api/esb/v1/arpita/create
Sample Request Format: JSON (POST Request)
{
"ProductName": "Shirt",
"ProductType": "Formal",
"Price": "900"
}
Product Id is automatically incremented, therefore it should not be specified in the input request by the user.
Variable Description (Endpoint)
Variable | Type | Description |
---|---|---|
req | Map <String, Object> | Stores the JSON input request from the user |
item2 | Object | Used to call the methods in |
mp | Map <String, Object> | Stores the API response which is returned to the user. |
flag | int | Stores the called function’s return value. Its main purpose is error handling. (For the developer) |
e | Exception | Used for exception handling |
Program Work-Flow
The user makes a POST Request. This request is received at the endpoint.
Multiple checks are there to detect any error such as Invalid format of Id/Price or incorrect or missing field names. A proper error message and error code (400: Bad request) are returned back to the user at the endpoint.
If the request is valid then the control moves to the service layer through the service interface.
In the service layer, a new product of type item (Item is the model class) is created.
The product name, type and price are set and this item is then sent to the Dao class.
Dao interacts with the database and opens the hibernate session and begins the transaction.
The item is then saved as a new product in the database using the session.save method.
The transaction is then committed and the session is closed.
In case any backend error occurs then a proper message is displayed in the console for the developer. The user also gets an error code in response depending on the type of backend error.
All other errors other than InvalidInputException and ResourceNotFound are treated as ApplicationException in this model.
If the product is created successfully, then the message is displayed along with (200 OK) status code.
Sample Response
Status
200: OK
404: Not found
400: Bad Request
412: Precondition failed
500: Internal Server Error
Feel free to leave any suggestions or comments below!