Repository

The repository for the backend implementation of Tabunganku is available here.

Technology

I develop the backend using Java 17 with Spring Boot, and Postgres 15.3 as the data store. Other Java libraries that I use in this project:

  • spring-dotenv: a library to load environment variables from .env file. I use it to load database information and others.
  • MapStruct: a library to create mapping between two Java Beans. I use it to transform entity objects into DTO objects.
  • jOOQ: a library to generate Java code from database structure. I use it as an interface to interact with the database.

Directory Structure

Tabunganku Project Structure All classes are stored in the package com.willystw.tabunganku. TabungankuApplication is the main class to start the application. The subdirectories here manage different classes:

  • configuration: stores classes with @Configuration annotation. @Configuration indicates that the class has @Bean definition.
  • controller: stores classes with @RestController annotation.
  • dto: stores request and response classes, as well as DTO classes.
  • mapper: stores classes with @Mapper annotation. @Mapper annotation is used to generate a class that can transform one class into another.
  • model: stores model classes, such as: Category, Transaction, and User.
  • repository: stores classes with @Repository annotation. @Repository annotation is used to indicate that a class connects to a database to manipulate a data.
  • resolver: stores classes with @RestControllerAdvice annotation. @RestControllerAdvice overrides the default exception handling in Spring Boot.
  • service: stores classes that perform business logic or data manipulation tasks.

SQL Script

I put the script to initialize the tables in src/resources/initial_table.sql. The script will recreate all tables by first deleting and then creating them.

Application Setup

To properly start the application, we need to do several steps:

Create a New User

Currently, there is no user data available. Accessing the front page of Tabunganku will result in an error 500. To resolve the issue, we need to create a new user data by following several steps:

  1. Start the backend application
  2. Run this command to hit the API in backend to create a new user:
curl --location 'http://localhost:8080/users/add' \
--header 'Content-Type: application/json' \
--data-raw '{
    "username": "Foobar_1",
    "email": "foo@bar.com"
}'

http://localhost:8080 is the backend endpoint, while Foobar_1 serves as the desired username, and foo@bar.com as the desired email. Modify these values accordingly, with the caveat that the username value must be unique.

  1. The system should return a JSON response like this: {"message":null,"user_id":169166405720426381}. Take note of the user_id value.

Put the New User ID Value

Substitute the USER_ID value in .env file in tabunganku-fe project with the user_id value from the previous process. Dotenv File Content in Frontend

Start the Services

There are property values set in pom.xml file that are used by jOOQ plugin: pg.url, pg.username, and pg.password. We can modify the values to start the service by overriding the VM options: mvn spring-boot:run -Dpg.url=jdbc_url -Dpg.username=foo -Dpg.password=bar. After starting the backend, we can start the frontend service.

Improvement Idea

The process of creating a new user is unintuitive. I want to have a “Sign In with Google” button in the future to simplify the user creation process. That way user data can be easily managed.