AutoCodeGen - Custom Endpoints
We add custom endpoints to our project so our customers can deposit and withdraw cash, which is basic banking operations.
pgAdmin helps to manage the Postgresql server. After installation, launch pgAdmin and enter your password. Click on the SQL query executor by clicking the button highlighted on the left side of the picture.
Here we write a Query to increment account balance when a user makes a cash deposit through any means. And then log the cash deposit transaction into the transaction table. This can be done using two separate API calls, one API call to increment the account balance and another API call to update the transactions. We already have an autogenerated API endpoint to post a transaction value. To understand how to write slightly complex SQL queries, we combine both into one.
Query updates the accounts table by incrementing account_balance by 555 if the customer_id and account_id conditions are met. Then we create a record in the transactions table by inserting the transaction information. In line 5 account_id is updated to 2.
Now we have to convert these fixed values into variables, which can be done by following what is shown below. Replace the const value with the $ sign followed by open and close curly brackets within the brackets enter the variable name. If the const value is within quotes, keep the quotes, else it is not needed. These variables will be passed in as request parameters for our endpoint.
Now launch the AutoCodeGen, navigate to our project and click on the button to create a custom endpoint as shown below, a modal dialog will open. Enter the endpoint name in the text field, we are going to call it execute_a_transaction. Now we have to decide what variables passed in the query and in the body, of the request. We will pass search parameters like account_id and customer_id in the query, and the rest of the variables in the body.
Now select other variables that need to be in the body of the request.
Then select the request type, we are going to make a post request so select post. Now copy-paste the SQL query from pgAdmin into the Autocodegen text field for query. Click on add it will save our project.
Now update the project name and save it, AutoCodeGen does not allow us to regenerate the code with the same project name.
Click on generate button and type the exact project name in the input text field. and the click on generate.
Once code generation is done download the generated code.
Install compare folder VS Code extension and select the downloaded code folder. Compare Folder extension will show the difference between the existing code and downloaded code with custom endpoints. Since we removed, authentication for our endpoints, Compare Folder will show all the router files under Differences, just pick custom_router and customer_controller files.
Now restart our Node.JS server. In the thunder-client create a new collection called custom_endpoints and create a new post request, input the endpoint URL and define the body variables and query variables as shown in the below pictures, and execute the API call and it is a success.
Rando created his account with an initial amount of 1000 INR and then we add 555 INR from pgAdmin and we added another 1000 INR during the API call. So the total account balance in Rando's account should be 2555.
Now let us make an API call to get all account data, in the response under Rando's account_balance is 2555 INR.
Now we execute the get_all_transaction_data endpoint and we can see we have two recorded transactions under accoun_id 3. we did not create a record for the 1000 INR deposit when our Rando created his account. It can be done in future account creation.In the next article, we will discuss how to create a custom endpoint to debit money from the account and update the transactions. And then we will write another custom endpoint to get all associated transactions with an account