feat: Database model & configuration
This commit is contained in:
parent
c087081d25
commit
ee6c6eda02
10 changed files with 209 additions and 0 deletions
|
@ -2,8 +2,12 @@ package eu.ztsh.wymiana;
|
|||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableConfigurationProperties
|
||||
@ConfigurationPropertiesScan
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
package eu.ztsh.wymiana.data.config;
|
||||
|
||||
import com.zaxxer.hikari.HikariConfig;
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import org.hsqldb.persist.HsqlProperties;
|
||||
import org.hsqldb.server.Server;
|
||||
import org.hsqldb.server.ServerAcl;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@Configuration
|
||||
public class DatabaseConfiguration {
|
||||
|
||||
/**
|
||||
* Create HSQL Database instance that is accessible from outside of application
|
||||
*
|
||||
* @return server instance
|
||||
* @throws ServerAcl.AclFormatException when instantiating ServerAcl
|
||||
* @throws IOException when instantiating ServerAcl
|
||||
*/
|
||||
@Bean
|
||||
public Server hsqlServer(HsqlDbProperties properties) throws ServerAcl.AclFormatException, IOException {
|
||||
var server = new Server();
|
||||
var props = new HsqlProperties();
|
||||
props.setProperty("server.database.0", String.format("mem:%s", properties.name()));
|
||||
props.setProperty("server.dbname.0", properties.name());
|
||||
props.setProperty("server.no_system_exit", true);
|
||||
props.setProperty("server.port", properties.port());
|
||||
server.setProperties(props);
|
||||
server.start();
|
||||
return server;
|
||||
}
|
||||
|
||||
/**
|
||||
* As we manually create database instance, we need also create datasource instance.
|
||||
* HSQL instance is passed to force spring to create {@link Server} before pool.
|
||||
*
|
||||
* @param dataSourceProperties spring.datasource.* properties
|
||||
* @return datasource instance
|
||||
*/
|
||||
@Bean
|
||||
public HikariDataSource hikariDataSource(DataSourceProperties dataSourceProperties, Server ignore) {
|
||||
var config = new HikariConfig();
|
||||
config.setJdbcUrl(dataSourceProperties.getUrl());
|
||||
config.setUsername(dataSourceProperties.getUsername());
|
||||
config.setPassword(dataSourceProperties.getPassword());
|
||||
config.setDriverClassName(dataSourceProperties.getDriverClassName());
|
||||
return new HikariDataSource(config);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package eu.ztsh.wymiana.data.config;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
@ConfigurationProperties("hsqldb")
|
||||
public record HsqlDbProperties(String name, int port) {
|
||||
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package eu.ztsh.wymiana.data.entity;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Table;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Entity
|
||||
@Table(name = "currency")
|
||||
public class CurrencyEntity {
|
||||
|
||||
@Id
|
||||
String pesel;
|
||||
@Id
|
||||
String symbol;
|
||||
Double amount;
|
||||
|
||||
}
|
29
src/main/java/eu/ztsh/wymiana/data/entity/UserEntity.java
Normal file
29
src/main/java/eu/ztsh/wymiana/data/entity/UserEntity.java
Normal file
|
@ -0,0 +1,29 @@
|
|||
package eu.ztsh.wymiana.data.entity;
|
||||
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.Table;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Entity
|
||||
@Table(name = "user")
|
||||
public class UserEntity {
|
||||
|
||||
@Id
|
||||
String pesel;
|
||||
String name;
|
||||
String surname;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL, mappedBy = "pesel")
|
||||
List<CurrencyEntity> currencies;
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package eu.ztsh.wymiana.data.repository;
|
||||
|
||||
import eu.ztsh.wymiana.data.entity.UserEntity;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface UserRepository extends CrudRepository<UserEntity, String> {
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue