From e4c29cbe787584a3e20ce628052bac0a7abedd82 Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Fri, 17 Jul 2015 22:58:36 -0400 Subject: [PATCH] #166 Define type-safe API for get_full_account() --- libraries/app/api.cpp | 45 +++++++++---------- libraries/app/include/graphene/app/api.hpp | 3 +- .../app/include/graphene/app/full_account.hpp | 34 ++++++++++++++ 3 files changed, 58 insertions(+), 24 deletions(-) create mode 100644 libraries/app/include/graphene/app/full_account.hpp diff --git a/libraries/app/api.cpp b/libraries/app/api.cpp index fc19f56d..f6131a5c 100644 --- a/libraries/app/api.cpp +++ b/libraries/app/api.cpp @@ -154,10 +154,10 @@ namespace graphene { namespace app { return result; } - std::map database_api::get_full_accounts(std::function callback, + std::map database_api::get_full_accounts(std::function callback, const vector& names_or_ids) { - std::map results; + std::map results; std::set ids_to_subscribe; for (const std::string& account_name_or_id : names_or_ids) @@ -177,59 +177,58 @@ namespace graphene { namespace app { ids_to_subscribe.insert({account->id, account->statistics}); - fc::mutable_variant_object full_account; + // fc::mutable_variant_object full_account; + full_account acnt; + acnt.account = *account; + acnt.statistics = account->statistics(_db); + acnt.registrar_name = account->registrar(_db).name; + acnt.referrer_name = account->referrer(_db).name; + acnt.lifetime_referrer_name = account->lifetime_referrer(_db).name; // Add the account itself, its statistics object, cashback balance, and referral account names + /* full_account("account", *account)("statistics", account->statistics(_db)) ("registrar_name", account->registrar(_db).name)("referrer_name", account->referrer(_db).name) ("lifetime_referrer_name", account->lifetime_referrer(_db).name); + */ if (account->cashback_vb) { ids_to_subscribe.insert(*account->cashback_vb); - full_account("cashback_balance", account->cashback_balance(_db)); + acnt.cashback_balance = account->cashback_balance(_db); } // Add the account's balances auto balance_range = _db.get_index_type().indices().get().equal_range(account->id); - vector balances; + //vector balances; std::for_each(balance_range.first, balance_range.second, - [&balances, &ids_to_subscribe](const account_balance_object& balance) { - balances.emplace_back(balance); + [&acnt, &ids_to_subscribe](const account_balance_object& balance) { + acnt.balances.emplace_back(balance); ids_to_subscribe.insert(balance.id); }); - idump((balances)); - full_account("balances", balances); // Add the account's vesting balances auto vesting_range = _db.get_index_type().indices().get().equal_range(account->id); - vector vesting_balances; std::for_each(vesting_range.first, vesting_range.second, - [&vesting_balances, &ids_to_subscribe](const vesting_balance_object& balance) { - vesting_balances.emplace_back(balance); + [&acnt, &ids_to_subscribe](const vesting_balance_object& balance) { + acnt.vesting_balances.emplace_back(balance); ids_to_subscribe.insert(balance.id); }); - full_account("vesting_balances", vesting_balances); // Add the account's orders auto order_range = _db.get_index_type().indices().get().equal_range(account->id); - vector orders; std::for_each(order_range.first, order_range.second, - [&orders, &ids_to_subscribe] (const limit_order_object& order) { - orders.emplace_back(order); + [&acnt, &ids_to_subscribe] (const limit_order_object& order) { + acnt.limit_orders.emplace_back(order); ids_to_subscribe.insert(order.id); }); auto call_range = _db.get_index_type().indices().get().equal_range(account->id); - vector calls; std::for_each(call_range.first, call_range.second, - [&calls, &ids_to_subscribe] (const call_order_object& call) { - calls.emplace_back(call); + [&acnt, &ids_to_subscribe] (const call_order_object& call) { + acnt.call_orders.emplace_back(call); ids_to_subscribe.insert(call.id); }); - full_account("limit_orders", orders)("call_orders", calls); - - results[account_name_or_id] = full_account; + results[account_name_or_id] = acnt; } - wdump((results)); subscribe_to_objects(callback, vector(ids_to_subscribe.begin(), ids_to_subscribe.end())); return results; diff --git a/libraries/app/include/graphene/app/api.hpp b/libraries/app/include/graphene/app/api.hpp index d61d8d12..b0fb8b75 100644 --- a/libraries/app/include/graphene/app/api.hpp +++ b/libraries/app/include/graphene/app/api.hpp @@ -29,6 +29,7 @@ #include #include +#include #include @@ -150,7 +151,7 @@ namespace graphene { namespace app { * * TODO: Describe the return value and argument to callback in detail */ - std::map get_full_accounts(std::function callback, + std::map get_full_accounts(std::function callback, const vector& names_or_ids); /** diff --git a/libraries/app/include/graphene/app/full_account.hpp b/libraries/app/include/graphene/app/full_account.hpp new file mode 100644 index 00000000..a9e12a1a --- /dev/null +++ b/libraries/app/include/graphene/app/full_account.hpp @@ -0,0 +1,34 @@ +#pragma once + +#include + +namespace graphene { namespace app { + using namespace graphene::chain; + + struct full_account + { + account_object account; + account_statistics_object statistics; + string registrar_name; + string referrer_name; + string lifetime_referrer_name; + optional cashback_balance; + vector balances; + vector vesting_balances; + vector limit_orders; + vector call_orders; + }; + +} } + +FC_REFLECT( graphene::app::full_account, + (account) + (statistics) + (registrar_name) + (referrer_name) + (lifetime_referrer_name) + (cashback_balance) + (balances) + (vesting_balances) + (limit_orders) + (call_orders) )