Small fixes, update asset method

This commit is contained in:
Blue 2024-04-07 20:03:10 -03:00
parent a9f46b2ab0
commit 4914a467e5
Signed by: blue
GPG key ID: 9B203B252A63EE38
13 changed files with 172 additions and 6 deletions

View file

@ -48,6 +48,7 @@ public:
virtual Session findSession(const std::string& accessToken) = 0;
virtual std::vector<Asset> 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<Currency> listUsedCurrencies(uint32_t owner) = 0;

View file

@ -25,8 +25,10 @@ constexpr const char* selectSession = "SELECT id, owner, access, renew FROM sess
constexpr const char* selectAssets = "SELECT id, owner, currency, title, icon, color, archived FROM assets where owner = ?";
constexpr const char* insertAsset = "INSERT INTO assets (`owner`, `currency`, `title`, `icon`, `color`, `archived`, `type`)"
" VALUES (?, ?, ?, ?, ?, ?, 1)";
constexpr const char* updateAssetQuery = "UPDATE assets SET `owner` = ?, `currency` = ?, `title` = ?, `icon` = ?, `color` = ?, `archived` = ?"
" WHERE `id` = ?";
constexpr const char* removeAsset = "DELETE FROM assets where `id` = ? AND `owner` = ?";
constexpr const char* selectUsedCurrencies = "SELECT c.id, c.code, c.title, c.manual, c.icon FROM currencies c"
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 = ?";
@ -366,6 +368,21 @@ DB::Asset DB::MySQL::addAsset(const Asset& asset) {
return result;
}
void DB::MySQL::updateAsset(const Asset& asset) {
MYSQL* con = &connection;
Asset result = asset;
Statement update(con, updateAssetQuery);
update.bind(&result.owner, MYSQL_TYPE_LONG, true);
update.bind(&result.currency, MYSQL_TYPE_LONG, true);
update.bind(result.title.data(), MYSQL_TYPE_STRING);
update.bind(result.icon.data(), MYSQL_TYPE_STRING);
update.bind(&result.color, MYSQL_TYPE_LONG, true);
update.bind(&result.archived, MYSQL_TYPE_TINY);
update.bind(&result.id, MYSQL_TYPE_LONG, true);
update.execute();
}
bool DB::MySQL::deleteAsset(uint32_t assetId, uint32_t actorId) {
Statement del(&connection, removeAsset);
del.bind(&assetId, MYSQL_TYPE_LONG, true);

View file

@ -36,6 +36,7 @@ public:
Session findSession (const std::string& accessToken) override;
std::vector<Asset> 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<Currency> listUsedCurrencies(uint32_t owner) override;