-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #402 from spacious-team/develop
Релиз 2022.1
- Loading branch information
Showing
44 changed files
with
1,092 additions
and
483 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* InvestBook | ||
* Copyright (C) 2022 Vitalii Ananev <[email protected]> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package db.migration.all; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.flywaydb.core.api.migration.BaseJavaMigration; | ||
import org.flywaydb.core.api.migration.Context; | ||
|
||
import java.math.BigDecimal; | ||
import java.sql.ResultSet; | ||
import java.sql.Statement; | ||
import java.sql.Timestamp; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
@Slf4j | ||
public class V2022_1_0_1 extends BaseJavaMigration { | ||
|
||
public void migrate(Context context) throws Exception { | ||
try (Statement select = context.getConnection().createStatement()) { | ||
try (ResultSet rows = select.executeQuery("SELECT * FROM `portfolio_property` WHERE `property` = 'CASH'")) { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
while (rows.next()) { | ||
String portfolio = rows.getString("portfolio"); | ||
Timestamp timestamp = rows.getTimestamp("timestamp"); | ||
String value = rows.getString("value"); | ||
Collection<PortfolioCash> cashCollection = PortfolioCash.deserialize(objectMapper, value); | ||
try (Statement statement = context.getConnection().createStatement()) { | ||
cashCollection.forEach(cash -> insertCash(statement, portfolio, timestamp, cash)); | ||
} | ||
} | ||
} | ||
} | ||
try (Statement delete = context.getConnection().createStatement()) { | ||
delete.execute("DELETE FROM `portfolio_property` WHERE `property` = 'CASH'"); | ||
} | ||
} | ||
|
||
private void insertCash(Statement statement, String portfolio, Timestamp timestamp, PortfolioCash cash) { | ||
try { | ||
statement.execute("INSERT INTO `portfolio_cash` SET " + | ||
"portfolio ='" + portfolio + "', " + | ||
"timestamp = '" + timestamp + "', " + | ||
"market = '" + cash.getSection() + "', " + | ||
"value = " + cash.getValue() + ", " + | ||
"currency = '" + cash.getCurrency() + "'"); | ||
} catch (Exception e) { | ||
log.error("can't insert to table 'cash' value: portfolio = {}, timestamp = {}, cash = {}", | ||
portfolio, timestamp, cash, e); | ||
} | ||
} | ||
|
||
|
||
@Getter | ||
@Setter | ||
private static class PortfolioCash { | ||
private String section; | ||
private BigDecimal value; | ||
private String currency; | ||
|
||
public static Collection<PortfolioCash> deserialize(ObjectMapper objectMapper, String value) { | ||
try { | ||
return objectMapper.readValue(value, new TypeReference<>() { | ||
}); | ||
} catch (Exception e) { | ||
log.error("can't deserialize portfolio cash", e); | ||
return Collections.emptySet(); | ||
} | ||
} | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
src/main/java/ru/investbook/api/PortfolioCashRestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* InvestBook | ||
* Copyright (C) 2021 Vitalii Ananev <[email protected]> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package ru.investbook.api; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.Parameter; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import org.spacious_team.broker.pojo.PortfolioCash; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.PutMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import ru.investbook.converter.EntityConverter; | ||
import ru.investbook.entity.PortfolioCashEntity; | ||
|
||
import javax.validation.Valid; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@RestController | ||
@Tag(name = "Информация по остатку денежных средств на счете") | ||
@RequestMapping("/api/v1/portfolio-cash") | ||
public class PortfolioCashRestController extends AbstractRestController<Integer, PortfolioCash, PortfolioCashEntity> { | ||
|
||
public PortfolioCashRestController(JpaRepository<PortfolioCashEntity, Integer> repository, | ||
EntityConverter<PortfolioCashEntity, PortfolioCash> converter) { | ||
super(repository, converter); | ||
} | ||
|
||
@Override | ||
@GetMapping | ||
@Operation(summary = "Отобразить все", description = "Отображает всю имеющуюся информацию обо всех счетах") | ||
public List<PortfolioCash> get() { | ||
return super.get(); | ||
} | ||
|
||
@Override | ||
@GetMapping("{id}") | ||
@Operation(summary = "Отобразить один", description = "Отображает информацию по идентификатору") | ||
public ResponseEntity<PortfolioCash> get(@PathVariable("id") | ||
@Parameter(description = "Внутренний идентификатор записи") | ||
Integer id) { | ||
return super.get(id); | ||
} | ||
|
||
@Override | ||
@PostMapping | ||
@Operation(summary = "Добавить", description = "Добавить информацию для конкретного счета") | ||
public ResponseEntity<Void> post(@Valid @RequestBody PortfolioCash property) { | ||
return super.post(property); | ||
} | ||
|
||
@Override | ||
@PutMapping("{id}") | ||
@Operation(summary = "Обновить", description = "Обновить информацию для счета") | ||
public ResponseEntity<Void> put(@PathVariable("id") | ||
@Parameter(description = "Внутренний идентификатор записи") | ||
Integer id, | ||
@Valid @RequestBody PortfolioCash property) { | ||
return super.put(id, property); | ||
} | ||
|
||
@Override | ||
@DeleteMapping("{id}") | ||
@Operation(summary = "Удалить") | ||
public void delete(@PathVariable("id") | ||
@Parameter(description = "Внутренний идентификатор записи") | ||
Integer id) { | ||
super.delete(id); | ||
} | ||
|
||
@Override | ||
protected Optional<PortfolioCashEntity> getById(Integer id) { | ||
return repository.findById(id); | ||
} | ||
|
||
@Override | ||
protected Integer getId(PortfolioCash object) { | ||
return object.getId(); | ||
} | ||
|
||
@Override | ||
protected PortfolioCash updateId(Integer id, PortfolioCash object) { | ||
return object.toBuilder().id(id).build(); | ||
} | ||
|
||
@Override | ||
protected String getLocation() { | ||
return "/portfolio-cash"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/main/java/ru/investbook/converter/PortfolioCashConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* InvestBook | ||
* Copyright (C) 2020 Vitalii Ananev <[email protected]> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package ru.investbook.converter; | ||
|
||
import org.spacious_team.broker.pojo.PortfolioCash; | ||
import org.springframework.stereotype.Component; | ||
import ru.investbook.entity.PortfolioCashEntity; | ||
|
||
@Component | ||
public class PortfolioCashConverter implements EntityConverter<PortfolioCashEntity, PortfolioCash> { | ||
|
||
@Override | ||
public PortfolioCashEntity toEntity(PortfolioCash cash) { | ||
PortfolioCashEntity entity = new PortfolioCashEntity(); | ||
entity.setId(cash.getId()); | ||
entity.setPortfolio(cash.getPortfolio()); | ||
entity.setMarket(cash.getMarket()); | ||
entity.setTimestamp(cash.getTimestamp()); | ||
entity.setValue(cash.getValue()); | ||
entity.setCurrency(cash.getCurrency()); | ||
return entity; | ||
} | ||
|
||
@Override | ||
public PortfolioCash fromEntity(PortfolioCashEntity entity) { | ||
return PortfolioCash.builder() | ||
.id(entity.getId()) | ||
.portfolio(entity.getPortfolio()) | ||
.market(entity.getMarket()) | ||
.timestamp(entity.getTimestamp()) | ||
.value(entity.getValue()) | ||
.currency(entity.getCurrency()) | ||
.build(); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/ru/investbook/entity/PortfolioCashEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* InvestBook | ||
* Copyright (C) 2020 Vitalii Ananev <[email protected]> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package ru.investbook.entity; | ||
|
||
import lombok.Data; | ||
import org.hibernate.annotations.GenericGenerator; | ||
|
||
import javax.persistence.Basic; | ||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
import javax.persistence.Table; | ||
import java.math.BigDecimal; | ||
import java.time.Instant; | ||
|
||
@Entity | ||
@Table(name = "portfolio_cash") | ||
@Data | ||
public class PortfolioCashEntity { | ||
|
||
@Id | ||
@GenericGenerator(name = "UseExistingOrGenerateIdGenerator", strategy = "ru.investbook.entity.UseExistingOrGenerateIdGenerator") | ||
@GeneratedValue(generator = "UseExistingOrGenerateIdGenerator") | ||
@Column(name = "id") | ||
private Integer id; | ||
|
||
@Basic | ||
@Column(name = "portfolio", nullable = false) | ||
private String portfolio; | ||
|
||
@Basic | ||
@Column(name = "timestamp", nullable = false) | ||
private Instant timestamp; | ||
|
||
@Basic | ||
@Column(name = "market") | ||
private String market; | ||
|
||
@Basic | ||
@Column(name = "value", nullable = false) | ||
private BigDecimal value; | ||
|
||
@Basic | ||
@Column(name = "currency", nullable = false) | ||
private String currency; | ||
} |
Oops, something went wrong.