/bike-tracker

Little Spring Boot project to track bike movements

Primary LanguageJavaMIT LicenseMIT

Welcome to the Bike-tracking and visualizing App (Backend)

The main goal of this Project is that I can train some techniques from Spring Boot to a React Frontend. I don't think this will be a big thing, but I try to build it fullstack.

Table of Contents

Introduction

In this App it is possible to save data about your bicycle movements, like driven kilometers, movement time, average speed and maximum speed. The App will provide you with a table-view of your monthly or yearly movements with the overall kilometers and the current top values for average and maximum speed.
It will also be possible to save your costs on parts and maintenance and calculate a costs per kilometer information.
I will try to visualize some things in charts.

Tech stack

This project is made with Java and Spring Boot to provide a backend with REST API. The backend uses a PostgreSQL database for persistence which is called with Spring Data JPA. I also use Flyway to do my custom database migration to keep the full control by my own. After that I used some little helpers like lombok to reduce the overhead of writing the same code over and over again and javafaker to create the data for my integration tests.

Versions

0.0.1

In the first Version it will be possible to register an owner, adding bikes, record driven kilometers and persist them in a database.

Build

JDK 17.0.1 or higher is recommended. To build the application right now you can simply use mvn clean package

Run

JDK 17.0.1 or higher is recommended. Currently, it is recommended to run the application over IDE.
In all cases you need to configure a PostgreSQL database first.

PostgreSQL Docker Container

I highly recommend using a postgres docker container as database in development mode. When you have installed docker and compose on your system you can run the docker-compose.yml to automatically configure the needed container by simply execute docker compose up. Your volume will be saved locally to \\wsl$\docker-desktop-data\version-pack-data\community\docker\volumes. You can stop the test database setup with docker compose down. If you also want to delete the volume just append --volumes

If it already exists, and it is not up and running, you can simply start it via docker start bt-test

To psql into the postgres container use ./psql-into-database.bat from bin/ directory.

Initialize Database

Docker compose will bring up a test cluster and auto-generate the needed database, user and password. Flyway will perform all commands provided in /src/main/resources/db/migration on application start up.

API

The backend provides you a REST-API that can be reached on the base-URL localhost:8080/api/v1/ if you are running locally.

/owners

  • /all GET (no parameters)
    Returns all Owners that are persisted in the database.

  • /{id} GET (id parameter)
    Returns one single Owner with given id or null.

  • /create POST (body: Owner)
    You can perform a POST request against Owners with a body containing
    { "firstName" : "Foo", "lastName" : "Bar" }
    to add a new Owner to the database.
    By adding
    "bikes" : [ { "name" : "bla", "maker" : "blub", "model" : "NX-01", "bikeType" : "OTHER"}, ... ]
    to your Owner you will automatically create a corresponding Bike in your database for given owner.

  • /delete/{id} DELETE (id parameter)
    Deletes Owner entry with given id. Entry must exist.

  • /update/{id} PATCH (id parameter, body: Owner)
    Updates Owner with given id and input from message body. Body can be used similar to POST.

/tours

  • /all GET (no parameters)
    Returns all Tours that are persisted in the database.

  • /{id} GET (id parameter)
    Returns one single Tour with given id if exists or null.

  • /create POST (body: Tour)
    You can create a new Tour in the database with a body containing these informations:
    { "date" : "yyyy-MM-dd", "distance" : 22.0, "description" : "Went to the beach", "drivingTime" : 30, "maximumSpeed" : 37.6, "additionalInfo" : null }
    This Tour is not assigned to a Bike until you connect it over the Movement. To do so add this to the Tour structure:
    "movements" : [ {"id" : {"bikeId" : ##, "tourId" : ##} ]
    A Bike with this id has to exist and the tourId must match the id returned when inserted to the database.

  • /delete/{id} DELETE (id parameter)
    Deletes Tour entry with given id. Entry must exist.

TODOs

  • Integration Test Movements and Tours
  • For using views make the Entity @Immutable and use a Read-only Repository

@NoRepositoryBean
public interface ReadOnlyRepository<T, ID> extends Repository<T, ID> {
List findAll();
List findAll(Sort sort);
Page findAll(Pageable pageable);
Optional findById(ID id);
long count();
}