Skip to content

Commit

Permalink
Update some repository's interface
Browse files Browse the repository at this point in the history
  • Loading branch information
MizukiSonoko committed Mar 10, 2017
1 parent 819266e commit 000af3f
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 274 deletions.
35 changes: 26 additions & 9 deletions core/repository/domain/account_repository.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,31 @@ limitations under the License.
#include <vector>

namespace repository {
namespace account {
std::string add(const std::string &publicKey, const std::string &name,
const std::vector<std::string> &assets);
bool attach(const std::string &uuid, const std::string &asset);
bool update(const std::string &uuid, const std::string &name, const std::vector<std::string> &assets);
bool remove(const std::string &uuid);
Api::Account findByUuid(const std::string &uuid);
bool exists(const std::string &uuid);
}

namespace account {

bool add(
const std::string &publicKey,
const Api::Account &account
);

bool update(
const std::string &publicKey,
const Api::Account &account
);

bool remove(
const std::string &publicKey
);

Api::Account find(
const std::string &publicKey
);
bool exists(
const std::string &uuid
);

}

}
#endif // __CORE_REPOSITORY_DOMAIN_ACCOUNT_REPOSITORY_HPP__
38 changes: 28 additions & 10 deletions core/repository/domain/asset_repository.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,34 @@ limitations under the License.
#include <vector>

namespace repository {
namespace asset {
std::string add(const std::string &domain, const std::string &name,
const txbuilder::Map &value,
const std::string &smartContractName);
bool update(const std::string &uuid, const txbuilder::Map &value);
bool remove(const std::string &uuid);
std::vector<Api::Asset> findAll(const std::string &uuid);
Api::Asset findByUuid(const std::string &uuid);
bool exists(const std::string &uuid);
}
namespace asset {

bool add(
const std::string &publicKey,
const std::string &assetName,
const Api::Asset &asset
);

bool update(
const std::string &publicKey,
const std::string &assetName,
const Api::Asset &asset
);

bool remove(
const std::string &publicKey,
const std::string &assetName
);

Api::Asset find(
const std::string &publicKey,
const std::string &assetName
);
bool exists(
const std::string &publicKey,
const std::string &assetName
);
}
}

#endif // __CORE_REPOSITORY_DOMAIN_ASSET_REPOSITORY_HPP__
2 changes: 2 additions & 0 deletions core/repository/domain/instance/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ add_library(core_repository STATIC
asset_repository.cpp
simple_asset_repository.cpp
peer_repository.cpp

transaction_repository.cpp
)

target_link_libraries(core_repository
Expand Down
158 changes: 46 additions & 112 deletions core/repository/domain/instance/account_repository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,122 +30,56 @@ const auto ValuePrefix = common::Prefix("Account::");
namespace repository {
namespace account {

namespace detail {
/********************************************************************************************
* utils
********************************************************************************************/
std::string createAccountUuid(const std::string &publicKey) {
/*
account-uuid (Reason for account-uuid is url param can not use base64 in
default,
And accout-uuid is sha3-256(publicKey) )
*/
return hash::sha3_256_hex(publicKey);
}
}

/********************************************************************************************
* Add<Account>
********************************************************************************************/
std::string add(const std::string &publicKey, const std::string &name,
const std::vector<std::string> &assets) {

const auto allAssets = convert_string::to_string(assets);

logger::explore(NameSpaceID) << "Add<Account> publicKey: " << publicKey
<< " name: " << name << " assets: " << allAssets;

const auto uuid = detail::createAccountUuid(publicKey);
if (!exists(uuid)) {
const auto account = txbuilder::createAccount(publicKey, name, assets);
const auto strAccount = common::stringify<Api::Account>(account, ValuePrefix);
logger::debug(NameSpaceID) << "Save key: " << uuid << " strAccount: \""
<< strAccount << "\"";
if (world_state_repository::add(uuid, strAccount)) {
return uuid;
/********************************************************************************************
* Add<Account>
********************************************************************************************/
bool add(
const std::string &publicKey,
const Api::Account &account
){
return world_state_repository::add("account_" + publicKey, account.SerializeAsString());
}
}

return "";
}

/********************************************************************************************
* Add<Asset, To<Account>>
********************************************************************************************/
bool attach(const std::string &uuid, const std::string &asset) {

if (!exists(uuid)) {
return false;
}

const auto strAccount = world_state_repository::find(uuid);

Api::Account account = common::parse<Api::Account>(strAccount, ValuePrefix);
account.add_assets(asset);

auto str = common::stringify<Api::Account>(account, ValuePrefix);
if (world_state_repository::update(uuid, str)) {
logger::explore(NameSpaceID) << "Add<Asset, To<Account>> uuid: " << uuid
<< "asset: " << asset;
return true;
}

return false;
}

/********************************************************************************************
* Update<Account>
********************************************************************************************/
bool update(const std::string &uuid, const std::string& name, const std::vector<std::string> &assets) {

const auto allAssets = convert_string::stringifyVector(assets);

logger::explore(NameSpaceID) << "Update<Account> uuid: " << uuid
<< " name: " << name
<< " assets: " << allAssets;
/********************************************************************************************
* Update<Account>
********************************************************************************************/
bool update(
const std::string &publicKey,
const Api::Account &account
){
if(world_state_repository::exists("account_" + publicKey)){
return world_state_repository::update("account_" + publicKey, account.SerializeAsString());
}
return false;
}

if (exists(uuid)) {
const auto rval = world_state_repository::find(uuid);
auto account = common::parse<Api::Account>(rval, ValuePrefix);
*account.mutable_name() = name;
*account.mutable_assets() = txbuilder::Vector<std::string>(assets.begin(), assets.end());
const auto strAccount = common::stringify<Api::Account>(account, ValuePrefix);
if (world_state_repository::update(uuid, strAccount)) {
logger::debug(NameSpaceID) << "Update strAccount: \"" << strAccount
<< "\"";
return true;
/********************************************************************************************
* Remove<Account>
********************************************************************************************/
bool remove(
const std::string &publicKey
){
if(world_state_repository::exists("account_" + publicKey)){
return world_state_repository::remove("account_" + publicKey);
}
return false;
}
}

return false;
}
/********************************************************************************************
* Remove<Account>
********************************************************************************************/
bool remove(const std::string &uuid) {
if (exists(uuid)) {
logger::explore(NameSpaceID) << "Remove<Account> uuid: " << uuid;
return world_state_repository::remove(uuid);
}
return false;
}
Api::Account find(
const std::string &publicKey
){
Api::Account res;
if(world_state_repository::exists("account_" + publicKey)){
res.ParseFromString(world_state_repository::find("account_" + publicKey));
}
return res;
}

/********************************************************************************************
* find
********************************************************************************************/
Api::Account findByUuid(const std::string &uuid) {
if (exists(uuid)) {
const auto strAccount = world_state_repository::find(uuid);
logger::explore(NameSpaceID + "findByUuid") << "";
return common::parse<Api::Account>(strAccount, ValuePrefix);
}
return Api::Account();
}
bool exists(
const std::string &publicKey
){
return world_state_repository::exists("account_" + publicKey);
}

bool exists(const std::string &uuid) {
const auto result = world_state_repository::exists(uuid);
logger::explore(NameSpaceID + "::exists") << (result ? "true" : "false");
return result;
}
}
}
};
};
Loading

0 comments on commit 000af3f

Please sign in to comment.