Skip to content

Commit

Permalink
Add database support
Browse files Browse the repository at this point in the history
  • Loading branch information
meonwax committed Apr 18, 2024
1 parent 634d1a6 commit 038ee7c
Show file tree
Hide file tree
Showing 14 changed files with 246 additions and 13 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
trim_trailing_whitespace = true
insert_final_newline = true
charset = utf-8
indent_style = space
end_of_line = lf
indent_size = 2

[*.java]
indent_size = 4
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Predictr

A football prediction game.
A football prediction game.
Data for UEFA Euro 2024 included.

## Run locally

```
./mvnw mn:run
```
./start-db.sh # Database
./mvnw mn:run # Application
```
16 changes: 16 additions & 0 deletions docker-compose-database.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
services:
db:
image: postgres:16.2-alpine
restart: always
shm_size: 128mb
environment:
POSTGRES_USER: predictr
POSTGRES_PASSWORD: s3cret
POSTGRES_DB: predictr
ports:
- 5432:5432
adminer:
image: adminer:4.8.1-standalone
restart: always
ports:
- 9000:8080
64 changes: 56 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.meonwax.predictr</groupId>
<groupId>de.meonwax</groupId>
<artifactId>predictr</artifactId>
<version>0.1</version>
<version>0.1.0</version>
<packaging>${packaging}</packaging>

<parent>
<groupId>io.micronaut.platform</groupId>
<artifactId>micronaut-parent</artifactId>
<version>4.3.8</version>
<version>4.4.0</version>
</parent>
<properties>
<packaging>jar</packaging>
<jdk.version>21</jdk.version>
<release.version>21</release.version>
<micronaut.version>4.3.8</micronaut.version>
<micronaut.aot.enabled>false</micronaut.aot.enabled>
<micronaut.aot.packageName>de.meonwax.predictr.aot.generated</micronaut.aot.packageName>
<micronaut.version>4.4.0</micronaut.version>
<micronaut.test.resources.enabled>true</micronaut.test.resources.enabled>
<micronaut.runtime>netty</micronaut.runtime>
<micronaut.aot.enabled>false</micronaut.aot.enabled>
<micronaut.aot.packageName>de.meonwax.aot.generated</micronaut.aot.packageName>
<exec.mainClass>de.meonwax.predictr.Application</exec.mainClass>
</properties>

Expand All @@ -36,16 +37,41 @@
<artifactId>micronaut-http-server-netty</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut.data</groupId>
<artifactId>micronaut-data-jdbc</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut.liquibase</groupId>
<artifactId>micronaut-liquibase</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut.serde</groupId>
<artifactId>micronaut-serde-jackson</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut.sql</groupId>
<artifactId>micronaut-jdbc-hikari</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.micronaut.testresources</groupId>
<artifactId>micronaut-test-resources-client</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-client</artifactId>
Expand All @@ -67,6 +93,7 @@
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
Expand All @@ -87,7 +114,28 @@
<!-- Uncomment to enable incremental compilation -->
<!-- <useIncrementalCompilation>false</useIncrementalCompilation> -->

<annotationProcessorPaths combine.children="append">
<annotationProcessorPaths combine.self="override">
<path>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-inject-java</artifactId>
<version>${micronaut.core.version}</version>
</path>
<path>
<groupId>io.micronaut.data</groupId>
<artifactId>micronaut-data-processor</artifactId>
<version>${micronaut.data.version}</version>
<exclusions>
<exclusion>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-inject</artifactId>
</exclusion>
</exclusions>
</path>
<path>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-graal</artifactId>
<version>${micronaut.core.version}</version>
</path>
<path>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-validation</artifactId>
Expand All @@ -106,7 +154,7 @@
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-Amicronaut.processing.group=de.meonwax.predictr</arg>
<arg>-Amicronaut.processing.group=de.meonwax</arg>
<arg>-Amicronaut.processing.module=predictr</arg>
</compilerArgs>
</configuration>
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/de/meonwax/predictr/controller/ConfigController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package de.meonwax.predictr.controller;

import de.meonwax.predictr.entity.Config;
import de.meonwax.predictr.repository.ConfigRepository;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;

import java.util.Optional;

@Controller("/config")
public class ConfigController {

private final ConfigRepository configRepository;

ConfigController(ConfigRepository configRepository) {
this.configRepository = configRepository;
}

@Get
Optional<Config> index() {
return configRepository.findAll()
.stream()
.findFirst();
}
}
22 changes: 22 additions & 0 deletions src/main/java/de/meonwax/predictr/entity/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package de.meonwax.predictr.entity;

import io.micronaut.core.annotation.Nullable;
import io.micronaut.data.annotation.Id;
import io.micronaut.data.annotation.MappedEntity;
import io.micronaut.serde.annotation.Serdeable;

@MappedEntity
@Serdeable
public record Config(
@Id Long id,
String title,
String owner,
String adminEmail,
Boolean showImportantMessage,
Integer pointsResult,
Integer pointsTendency,
Integer pointsTendencySpread,
@Nullable String rulesEn,
@Nullable String rulesDe
) {
}
10 changes: 10 additions & 0 deletions src/main/java/de/meonwax/predictr/repository/ConfigRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package de.meonwax.predictr.repository;

import de.meonwax.predictr.entity.Config;
import io.micronaut.data.jdbc.annotation.JdbcRepository;
import io.micronaut.data.model.query.builder.sql.Dialect;
import io.micronaut.data.repository.CrudRepository;

@JdbcRepository(dialect = Dialect.POSTGRES)
public interface ConfigRepository extends CrudRepository<Config, Long> {
}
2 changes: 0 additions & 2 deletions src/main/resources/application.properties

This file was deleted.

20 changes: 20 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
micronaut:
application:
name: predictr
server:
port: 8081

datasources:
default:
url: jdbc:postgresql://localhost:5432/predictr
username: predictr
password: s3cret
driverClassName: org.postgresql.Driver
dialect: POSTGRES
schema-generate: NONE

liquibase:
enabled: true
datasources:
default:
change-log: 'classpath:db/liquibase-changelog.xml'
39 changes: 39 additions & 0 deletions src/main/resources/db/changelog/01-create-config-schema.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">

<changeSet id="01" author="meonwax">
<createTable tableName="config">
<column name="id" type="bigserial">
<constraints nullable="false" unique="true" primaryKey="true"/>
</column>
<column name="title" type="varchar(255)">
<constraints nullable="false"/>
</column>
<column name="owner" type="varchar(255)">
<constraints nullable="false"/>
</column>
<column name="admin_email" type="varchar(255)">
<constraints nullable="false"/>
</column>
<column name="show_important_message" type="boolean">
<constraints nullable="false"/>
</column>
<column name="points_result" type="int">
<constraints nullable="false"/>
</column>
<column name="points_tendency" type="int">
<constraints nullable="false"/>
</column>
<column name="points_tendency_spread" type="int">
<constraints nullable="false"/>
</column>
<column name="rules_en" type="text"/>
<column name="rules_de" type="text"/>
</createTable>
</changeSet>

</databaseChangeLog>
20 changes: 20 additions & 0 deletions src/main/resources/db/changelog/02-insert-config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">

<changeSet id="02" author="meonwax">
<insert tableName="config">
<column name="title">Predictr</column>
<column name="owner">John Doe</column>
<column name="admin_email">[email protected]</column>
<column name="show_important_message">true</column>
<column name="points_result">5</column>
<column name="points_tendency">2</column>
<column name="points_tendency_spread">3</column>
</insert>
</changeSet>

</databaseChangeLog>
9 changes: 9 additions & 0 deletions src/main/resources/db/liquibase-changelog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<include file="changelog/01-create-config-schema.xml" relativeToChangelogFile="true"/>
<include file="changelog/02-insert-config.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>
6 changes: 6 additions & 0 deletions src/main/resources/micronaut-banner.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

▄▄▄·▄▄▄ ▄▄▄ .·▄▄▄▄ ▪ ▄▄· ▄▄▄▄▄▄▄▄
▐█ ▄█▀▄ █·▀▄.▀·██▪ ██ ██ ▐█ ▌▪•██ ▀▄ █·
██▀·▐▀▀▄ ▐▀▀▪▄▐█· ▐█▌▐█·██ ▄▄ ▐█.▪▐▀▀▄
▐█▪·•▐█•█▌▐█▄▄▌██. ██ ▐█▌▐███▌ ▐█▌·▐█•█▌
.▀ .▀ ▀ ▀▀▀ ▀▀▀▀▀• ▀▀▀·▀▀▀ ▀▀▀ .▀ ▀
7 changes: 7 additions & 0 deletions start-db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
set -eu
cleanup() {
docker compose -f docker-compose-database.yml rm -fsv
}
trap cleanup EXIT
docker compose -f docker-compose-database.yml up

0 comments on commit 038ee7c

Please sign in to comment.