Create a Person

Now that we have successfully logged into the application and received our first JWT, it is time to create a new ‘Person’ entity. Start by copying the content of the ‘token’ tag from the login response body to the clipboard. This JWT must henceforth be included in the http header of every subsequent request.

Create a new tab in Postman and specify a target URL of http://127.0.0.1:8080/person with the http POST method. Next, add the following key-value pairs to the http header:

  • Content-Type : application\json
  • Authorization : Bearer *paste-your-JWT-here*

person-a

When you have finished maintaining the http header-values, click on ‘Body’ and maintain it using the ‘raw’ setting. This will allow you to paste the following JSON code snippet into the request’s body:

    {
	    "name": "Steve Dallas",
	    "age": 46,
	    "weight": 185,
	    "valid_license": true,
	    "license_class": "A"
    }

When you have finished, the test session should look as follows and it is time to create our first ‘Person’ entity. Click ‘Send’ to post the new entity to the application.

person-b

Congratulations! You have created your first ‘Person’ entity!

person-c

What Just Happened?

The router matched the request URL to a route (service end-point), the middleware layer in the matched route examined the JWT, verified it was okay to proceed and then passed the raw JSON from the request body to the ‘Person’ entity’s controller. The controller unmarshalled the JSON into a ‘Person’ struct and then passed the result to the Create method in the model/validation layer. Validation of the ‘Person’ struct’s content occurred, and then a call was made to the underlying sqac ORM to create the entity on the database.

The sqac ORM-layer returned the new entity to the application’s model-layer, where it was checked and passed back to the controller layer, whereupon it was marshaled (struct content becomes JSON) and written to the the response-writer.

This is a high-level view of what transpired, but the general flow of things is accurate.

Notice that the entity passed back to us seems to have a couple of extra fields? All entities created via a Jiffy model file are injected with a primary-key of ‘id’ as well as a non-persistent ‘href’ field. In this example, our entity’s ‘id’ field was specified in the model file to be auto-incrementing with a starting value of 10000000. ‘href’ is included in each entity’s GET responses, and acts as a self-reference providing the entity’s direct access URI to the consumer.

See the Annotated Simple Single Entity Model in the Model Maintenance section for details regarding key options.