This is part 3 of the Pet Project series about building a small web application from scratch.

The road so far

Check the other posts to see where we are coming from:

The present

After some time off and dealing with other responsibilities, which required my attention, here is a new post on building a small web application from scratch. With this update we went pretty far on the implementation road, and we are now one step closer to having a functional backend. Just as a reminder, the application we are building is a shopping list application with the help of which the user can manage their shopping experience and keep track of items (to buy) by grouping them into lists.

This is what I’ve added with this iteration:

  • endpoints for handling items and shopping lists:
    • create item
    • get items, either all of them, or only those which contain a given text in their name
    • create shopping list
    • get shopping lists, either all of them, or only those which contain a given text in their name
    • get items belonging to a shopping lists
    • add item to a shopping list

You may have noticed, there is no delete operation, this feature will come in the near future, so stay tuned.

  • embedded H2 database - it saves the database files on the file system (see application.properties)
  • H2 compliant script, schema.sql, which is executed when the app starts and creates the database objects required by the application (a complete wipe takes place beforehand)
  • ability to populate the database with real-life data by means of a CommandLineRunner bean, which reads a .csv file and inserts the data into the database
  • test script, requests.sh, which depicts a real life scenario, for this I actually used the IntelliJ feature Generate HTTP Request to generate the requests and then I packed them all in an executable file
  • project uses now Java 11 (keeping up with the Java LTS versions)

Technical Implementation aspects

I tried to keep things as simple as possible and to follow good software development practices. Please note that this is a work in progress and that things will change. That being said, here is a summary of the development experience and status quo.

I tried to go with a package by feature approach for organizing the code, with this approach we have two packages, which bring the attention to the domain of the application, domain which comprises items and shopping lists. All the technical pieces reside within these two packages, provided it makes sense for them to be there. By technical pieces I mean Controllers (web layer), Services (business), Repositories (database access), Models (domain itself) and other objects that together contribute to the business logic of the application.

I use Enums to group different related values under the same name, Data Transfer Objects (DTOs) to move data between boundaries, Mapstruct to convert one object into another, Lombok to get rid of that annoying boilerplate code, validation - javax.validation.* to insure data validity, Jackson to easily process JSON data. As I am using Spring Framework and Spring Boot, a huge part of the heavy lifting is done by the framework, dependency injection, database access and so on.

The diagram below depicts a part of the model:

Domain ObjectsSholia ProjectItemid: Longname: Stringquantity: Doublemeasurement: MeasurementEnumlanguage: LanguageEnumpricePerQuantity: Doubleshop: Stringnotes: Stringcurrency: CurrencyEnumShoppingListid: Longname: Stringitems: List<Item>MeasurementEnumGKGLPCLanguageEnumENDEROCurrencyEnumEURODOLLARRON

You will be seeing more of this kind of diagrams as the project advances (I created them using PlantUML and a plugin for IntelliJ and exporting them as .svg). Think of them as diagram as code, which means, one writes some special code, which is then transformed into a .svg or image of even .pdf, code which can be versioned.

Another interesting thing was how to take a peek inside the embedded H2 database from the terminal. Please note that the database has to be shut down in order to connect to it, we are basically having a look into the database file, which is locked as long as H2 is running.

java -cp <path-to-h2-jar> org.h2.tools.Shell \
     -url "<h2-connection-string>;ifexists=true" \
     -user "<user>" \
     -password "<password>"

where:

  • <path-to-h2-jar> is something like /home/vlad/.m2/repository/com/h2database/h2/1.4.200/h2-1.4.200.jar, in my case I am using maven
  • <h2-connection-string>, <user>, <password> is the information to connect to the H2 database, usually found in the application.properties file

After connecting to the database we can interogate it by running commands like:

show tables;
select * from item;

Final words

This post is more of a status report, and a bird’s-eye view of the current project iteration. We are now at the point where we could start building a fancier kind of client, other than some .sh scripts, and send requests to the backend to create and get items and shopping lists. As necessary, the business logic will change to make room to other features as I discover them from my own shopping experience. There is still a lot of work ahead - new client, a more stable backend, CI/CD process, Infrastructure etc., so stay tuned for the next update.

The current state of the project is available on GitHub.

Take care and ‘til next time!