diff --git a/database/interface.h b/database/interface.h index 9c5c0c4..097474d 100644 --- a/database/interface.h +++ b/database/interface.h @@ -12,6 +12,7 @@ #include "schema/session.h" #include "schema/asset.h" #include "schema/currency.h" +#include "schema/transaction.h" namespace DB { class Interface { @@ -44,14 +45,21 @@ public: virtual uint32_t registerAccount(const std::string& login, const std::string& hash) = 0; virtual std::string getAccountHash(const std::string& login) = 0; + virtual Session createSession(const std::string& login, const std::string& access, const std::string& renew) = 0; virtual Session findSession(const std::string& accessToken) = 0; + virtual std::vector listAssets(uint32_t owner) = 0; virtual Asset addAsset(const Asset& asset) = 0; virtual void updateAsset(const Asset& asset) = 0; virtual bool deleteAsset(uint32_t assetId, uint32_t actorId) = 0; + virtual std::vector listUsedCurrencies(uint32_t owner) = 0; + virtual DB::Transaction addTransaction(const DB::Transaction& transaction) = 0; + virtual void updateTransaction(const DB::Transaction& transaction) = 0; + virtual std::vector listTransactions(uint32_t owner) = 0; + protected: Interface(Type type); diff --git a/database/mysql/mysql.cpp b/database/mysql/mysql.cpp index 2ae8770..296d37b 100644 --- a/database/mysql/mysql.cpp +++ b/database/mysql/mysql.cpp @@ -31,6 +31,10 @@ constexpr const char* removeAsset = "DELETE FROM assets where `id` = ? AND `owne constexpr const char* selectUsedCurrencies = "SELECT DISTINCT c.id, c.code, c.title, c.manual, c.icon FROM currencies c" " JOIN assets a ON c.id = a.currency" " WHERE a.owner = ?"; +constexpr const char* addTransactionQuery = "INSERT INTO transactions" + " (`initiator`, `type`, `asset`, `parent`, `value`, `performed`)" + " VALUES (?, 1, ?, ?, ?, ?)"; + static const std::filesystem::path buildSQLPath = "database"; @@ -409,3 +413,30 @@ std::vector DB::MySQL::listUsedCurrencies(uint32_t owner) { return result; } + +DB::Transaction DB::MySQL::addTransaction(const DB::Transaction& transaction) { + MYSQL* con = &connection; + DB::Transaction result = transaction; + + std::string value = std::to_string(result.value); + + Statement add(con, addTransactionQuery); + add.bind(&result.initiator, MYSQL_TYPE_LONG, true); + add.bind(&result.asset, MYSQL_TYPE_LONG, true); + add.bind(&result.parent, MYSQL_TYPE_LONG, true); + add.bind(value.data(), MYSQL_TYPE_STRING); + add.bind(&result.performed, MYSQL_TYPE_LONG, true); + add.execute(); + + result.id = lastInsertedId(); + //todo retreive timestamp and actual value which could have changed after insertion + + return result; +} + +void DB::MySQL::updateTransaction(const DB::Transaction& transaction) { +} + +std::vector DB::MySQL::listTransactions(uint32_t owner) { + return std::vector(); +} diff --git a/database/mysql/mysql.h b/database/mysql/mysql.h index bd703bb..273a9e8 100644 --- a/database/mysql/mysql.h +++ b/database/mysql/mysql.h @@ -32,14 +32,21 @@ public: uint32_t registerAccount (const std::string& login, const std::string& hash) override; std::string getAccountHash (const std::string& login) override; + Session createSession (const std::string& login, const std::string& access, const std::string& renew) override; Session findSession (const std::string& accessToken) override; + std::vector listAssets (uint32_t owner) override; Asset addAsset (const Asset& asset) override; void updateAsset (const Asset& asset) override; bool deleteAsset(uint32_t assetId, uint32_t actorId) override; + std::vector listUsedCurrencies(uint32_t owner) override; + DB::Transaction addTransaction(const DB::Transaction& transaction) override; + void updateTransaction(const DB::Transaction& transaction) override; + std::vector listTransactions(uint32_t owner) override; + private: void executeFile (const std::filesystem::path& relativePath); static std::optional getComment (std::string& string); diff --git a/database/schema/CMakeLists.txt b/database/schema/CMakeLists.txt index 94706fd..776b4fb 100644 --- a/database/schema/CMakeLists.txt +++ b/database/schema/CMakeLists.txt @@ -5,12 +5,14 @@ set(HEADERS session.h asset.h currency.h + transaction.h ) set(SOURCES session.cpp asset.cpp currency.cpp + transaction.cpp ) target_sources(${PROJECT_NAME} PRIVATE ${SOURCES}) diff --git a/database/schema/transaction.cpp b/database/schema/transaction.cpp new file mode 100644 index 0000000..9d90429 --- /dev/null +++ b/database/schema/transaction.cpp @@ -0,0 +1,50 @@ +//SPDX-FileCopyrightText: 2024 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later + +#include "transaction.h" + +DB::Transaction::Transaction(): + id(0), + initiator(0), + asset(0), + parent(0), + value(0), + modified(0), + performed(0), + notes() +{} + +DB::Transaction::Transaction(const std::vector& vec): + id(std::any_cast(vec[0])), + initiator(std::any_cast(vec[1])), + asset(std::any_cast(vec[2])), + parent(std::any_cast(vec[3])), + value(std::any_cast(vec[4])), + modified(std::any_cast(vec[5])), + performed(std::any_cast(vec[6])), + notes() +{} + +void DB::Transaction::parse(const std::vector& vec) { + id = std::any_cast(vec[0]); + initiator = std::any_cast(vec[1]); + asset = std::any_cast(vec[2]); + parent = std::any_cast(vec[3]); + value = std::any_cast(vec[4]); + modified = std::any_cast(vec[5]); + performed = std::any_cast(vec[6]); +} + +nlohmann::json DB::Transaction::toJSON() const { + nlohmann::json result = nlohmann::json::object(); + + result["id"] = id; + result["initiator"] = initiator; + result["asset"] = asset; + result["parent"] = parent; + result["value"] = value; + result["modified"] = modified; + result["performed"] = performed; + + return result; +} diff --git a/database/schema/transaction.h b/database/schema/transaction.h new file mode 100644 index 0000000..754f0eb --- /dev/null +++ b/database/schema/transaction.h @@ -0,0 +1,35 @@ +//SPDX-FileCopyrightText: 2024 Yury Gubich +//SPDX-License-Identifier: GPL-3.0-or-later + +#pragma once + +#include +#include +#include +#include + +#include + +namespace DB { +class Transaction { +public: + Transaction (); + Transaction (const std::vector& vec); + + void parse (const std::vector& vec); + nlohmann::json toJSON () const; + +public: + uint32_t id; + uint32_t initiator; + // `type` INTEGER UNSIGNED NOT NULL, + uint32_t asset; + uint32_t parent; + double value; + // `state` INTEGER UNSIGNED DEFAULT 0, + uint32_t modified; + uint32_t performed; + // `party` INTEGER UNSIGNED, + std::string notes; +}; +} \ No newline at end of file