Adding Records to Database
Creating business process to add record to database
Now, by pressing the Save button, it is necessary to organize the process of transferring the entered data to the database to create a new country. And this is worth looking into in more detail.
Technically, everything is very similar to the actions from the previous modules. There is an endpoint that performs certain actions (in our case, this is the creation of a new record in the database) that needs to transfer data to it and start the execution. The difference is that not just individual fields are transferred (as was the case with X and Y); the entire object is transferred. At the stage of designing the database, a model was created, and now, to launch the endpoint, you need to pass a ready-made object of this model.
But the object itself will be assembled from separate fields. To do this, you need to select the appropriate Make block (in our case, Make country) and pass data from the input fields to it.
You can notice that there are quite a lot of fields in the model, but many of them are system ones (ID, CreatedAt, UpdatedAt, DeletedAt), which means they will be created automatically. It would be possible to transfer information about the related table of cities and indicate which cities are in this country, but at this stage, it does not make sense because there is not a single city in the database yet. Therefore, we leave this field blank.
You need to do the following:
- Collect data from input fields
- Generate an object for writing to the database (Make block)
- Pass the finished object to the endpoint of creating (POST) a record in the database
In finished form, the business process looks like this:
It is worth paying attention once again to the fact that the Make block itself does not create any records in the database. It only forms an object. But how this object will be used in the future (passed for writing to the database or somewhere else) depends solely on the logic of your application.
On this, the creation process could be completed, it is already operational, but for real use, it is still inconvenient. It's worth at least adding a confirmation message. To do this, we will find out the recording result itself (_success) and, depending on the result (If-Else), show the corresponding message (Show Notification block) and also automatically close the modal window (Modal Hide). Сlosing should be assigned to the Cancel button of the modal window.