Skip to content
This repository has been archived by the owner. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/src/main/java/ru/ct/belfort/OperationDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package ru.ct.belfort;

public record OperationDTO(String id, MoneyValueDTO payment, MoneyValueDTO price, String figi, Advice operationType) {
}
9 changes: 9 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,12 @@ services:
POSTGRES_DB: trading_bot
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres

postgres_user:
image: postgres
ports:
- "5431:5432"
environment:
POSTGRES_DB: user_service
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
6 changes: 6 additions & 0 deletions user-service/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.slf4j:slf4j-api'
implementation 'org.slf4j:slf4j-simple'
implementation 'org.postgresql:postgresql'
implementation 'org.liquibase:liquibase-core'
implementation 'org.springframework:spring-jdbc'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'

implementation(project(":api"))
}

Expand Down
41 changes: 41 additions & 0 deletions user-service/src/main/java/ru/ct/belfort/db/LiquibaseConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ru.ct.belfort.db;

import liquibase.integration.spring.SpringLiquibase;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;
import java.util.Objects;

@Configuration
@PropertySource("classpath:database.properties")
public class LiquibaseConfig {
final Environment env;

LiquibaseConfig(Environment env) {
this.env = env;
}

@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();

dataSource.setDriverClassName(Objects.requireNonNull(env.getProperty("spring.datasource.drivername")));
dataSource.setUrl(env.getProperty("spring.datasource.url"));
dataSource.setUsername(env.getProperty("spring.datasource.username"));
dataSource.setPassword(env.getProperty("spring.datasource.password"));

return dataSource;
}

@Bean
public SpringLiquibase liquibase() {
SpringLiquibase liquibase = new SpringLiquibase();
liquibase.setChangeLog("classpath:db/migration/master.xml");
liquibase.setDataSource(dataSource());
return liquibase;
}
}
8 changes: 8 additions & 0 deletions user-service/src/main/java/ru/ct/belfort/db/UserEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ru.ct.belfort.db;


import lombok.Builder;

@Builder
public record UserEntity(int id, String str) {
}
37 changes: 37 additions & 0 deletions user-service/src/main/java/ru/ct/belfort/db/UserRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ru.ct.belfort.db;


import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Service
@Slf4j
public class UserRepository {
private final JdbcTemplate jdbcTemplate;

public UserRepository(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

private static final UserEntityMapper mapper = new UserEntityMapper();

public List<UserEntity> selectAll() {
return jdbcTemplate.query("SELECT * FROM users", mapper);
}

private static class UserEntityMapper implements RowMapper<UserEntity> {
@Override
public UserEntity mapRow(ResultSet rs, int rowNum) throws SQLException {
return UserEntity.builder()
.id(rs.getInt("id"))
.str(rs.getString("str"))
.build();
}
}
}
1 change: 1 addition & 0 deletions user-service/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.liquibase.change-log=classpath:db/migration/master.xml
4 changes: 4 additions & 0 deletions user-service/src/main/resources/database.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
spring.datasource.drivername=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://172.17.0.1:5431/user_service
spring.datasource.username=postgres
spring.datasource.password=postgres
44 changes: 44 additions & 0 deletions user-service/src/main/resources/db/migration/master.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd
http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">

<changeSet author="GrunskiiAlexey" id="1.0.0">
<sqlFile path="db/migration/sql/create_users.sql"/>
</changeSet>

<changeSet author="GrunskiiAlexey" id="1.1.0">
<sqlFile path="db/migration/sql/create_correct_users.sql"/>
</changeSet>

<changeSet author="GrunskiiAlexey" id="1.2.0">
<sqlFile path="db/migration/sql/create_figis.sql"/>
</changeSet>

<changeSet author="GrunskiiAlexey" id="2.0.0">
<sqlFile path="db/migration/sql/create_operation.sql"/>
<dropTable tableName="users"/>
<renameTable oldTableName="users_table" newTableName="users"/>
</changeSet>

<changeSet author="GrunskiiAlexey" id="2.1.0">
<addColumn tableName="operations">
<column name="parent_id" type="integer"/>
</addColumn>
<sql>
SELECT * FROM users
INNER JOIN figis
ON users.id = figis.id
</sql>
<sql>
SELECT * FROM users
INNER JOIN operations
ON users.id = operations.id
</sql>
</changeSet>
</databaseChangeLog>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE users_table
(
id SERIAL PRIMARY KEY,
token varchar(255),
strategy varchar(255)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE figis
(
id INTEGER,
figi varchar(255)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE operations
(
id INT PRIMARY KEY,
payment DOUBLE PRECISION,
price DOUBLE PRECISION,
figi varchar(255),
operationType varchar(255)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE users
(
id SERIAL PRIMARY KEY,
score DOUBLE PRECISION
)