From 1db5e0753eb78e30381829d6fa218f2ae0cdc79f Mon Sep 17 00:00:00 2001 From: Andrzej Popowski Date: Fri, 13 Feb 2015 08:34:36 +0100 Subject: [PATCH] [Account] - Adding, updating and removing account Change-Id: I8b7855711e8d1d3c9b957c01d4e341de7f3d5254 Signed-off-by: Andrzej Popowski --- src/account/account_api.js | 22 ++++++++---- src/account/account_instance.cc | 30 +++------------- src/account/account_manager.cc | 76 +++++++++++++++++++++++++++++++++++++++++ src/account/account_manager.h | 5 +++ 4 files changed, 100 insertions(+), 33 deletions(-) diff --git a/src/account/account_api.js b/src/account/account_api.js index 712a952..3dab5de 100644 --- a/src/account/account_api.js +++ b/src/account/account_api.js @@ -151,10 +151,18 @@ AccountManager.prototype.add = function() { { name: 'account', type: types_.PLATFORM_OBJECT, values: Account } ]); - var result = native_.callSync( 'AccountManager_add', { account: args.account }); + var result = native_.callSync('AccountManager_add', + { + userName: args.account.userName, + iconUri: args.account.iconUri, + applicationId: args.account.provider.applicationId + } + ); if (native_.isFailure(result)) { throw native_.getErrorObject(result); + } else { + args.account.id = new InternalValues_({ id: native_.getResultObject(result) }); } } @@ -177,12 +185,12 @@ AccountManager.prototype.update = function() { { name: 'account', type: types_.PLATFORM_OBJECT, values: Account } ]); - var result = native_.callSync( 'AccountManager_update', - { - accountId: args.account.id, - userName: args.account.userName, - iconUri: args.account.iconUri - } + var result = native_.callSync('AccountManager_update', + { + accountId: args.account.id, + userName: args.account.userName, + iconUri: args.account.iconUri + } ); if (native_.isFailure(result)) { diff --git a/src/account/account_instance.cc b/src/account/account_instance.cc index 1ccae97..ede47e4 100644 --- a/src/account/account_instance.cc +++ b/src/account/account_instance.cc @@ -149,46 +149,24 @@ void AccountInstance::AccountGetExtendedDataSync(const picojson::value& args, this->manager_->GetExtendedData(account_id, key, out); } -void AccountInstance::AccountManagerAdd(const picojson::value& args, - picojson::object& out) { +void AccountInstance::AccountManagerAdd(const picojson::value& args, picojson::object& out) { LoggerD("Enter"); - CheckAccess(kPrivilegeAccountWrite, &out); - - // implement it - - // if success - // ReportSuccess(out); - // if error - // ReportError(out); + manager_->AddAccount(args, out); } void AccountInstance::AccountManagerRemove(const picojson::value& args, picojson::object& out) { LoggerD("Enter"); - CheckAccess(kPrivilegeAccountWrite, &out); - - // implement it - - // if success - // ReportSuccess(out); - // if error - // ReportError(out); + manager_->RemoveAccount(args, out); } void AccountInstance::AccountManagerUpdate(const picojson::value& args, picojson::object& out) { LoggerD("Enter"); - CheckAccess(kPrivilegeAccountWrite, &out); - - // implement it - - // if success - // ReportSuccess(out); - // if error - // ReportError(out); + manager_->UpdateAccount(args, out); } void AccountInstance::AccountManagerGetAccount(const picojson::value& args, diff --git a/src/account/account_manager.cc b/src/account/account_manager.cc index 4e60f52..57454d3 100644 --- a/src/account/account_manager.cc +++ b/src/account/account_manager.cc @@ -498,6 +498,82 @@ void AccountManager::SetExtendedData(int account_id, const std::string& key, con } } +void AccountManager::AddAccount(const picojson::value& data, picojson::object& out) { + LoggerD("Enter"); + account_h account_handle = NULL; + int account_id; + const std::string& user_name = data.get("userName").get(); + const std::string& icon_uri = data.get("iconUri").get(); + const std::string& application_id = data.get("applicationId").get(); + + int ret = account_create(&account_handle); + if (!ret) { + ret = account_set_user_name(account_handle, user_name.c_str()); + if (!ret) { + ret = account_set_icon_path(account_handle, icon_uri.c_str()); + } + if (!ret) { + ret = account_set_package_name(account_handle, application_id.c_str()); + } + if (!ret) { + ret = account_insert_to_db(account_handle, &account_id); + } + account_destroy(account_handle); + } + if (!ret) { + out["status"] = picojson::value("success"); + out["result"] = picojson::value(static_cast(account_id)); + } else { + LoggerE("Failed to create account"); + REPORT_ERROR(out, UnknownException(GetErrorMsg(ret))); + } +} + +void AccountManager::RemoveAccount(const picojson::value& data, picojson::object& out) { + LoggerD("Enter"); + int account_id = static_cast(data.get("accountId").get()); + + int ret = account_delete_from_db_by_id(account_id); + + if (!ret) { + out["status"] = picojson::value("success"); + } else { + LoggerE("Failed to create account"); + REPORT_ERROR(out, UnknownException(GetErrorMsg(ret))); + } +} + +void AccountManager::UpdateAccount(const picojson::value& data, picojson::object& out) { + LoggerD("Enter"); + int account_id = static_cast(data.get("accountId").get()); + const std::string& user_name = data.get("userName").get(); + const std::string& icon_uri = data.get("iconUri").get(); + account_h account_handle = NULL; + + int ret = account_create(&account_handle); + + if (!ret) { + ret = account_query_account_by_account_id(account_id, &account_handle); + if (!ret) { + ret = account_set_user_name(account_handle, user_name.c_str()); + } + if (!ret) { + ret = account_set_icon_path(account_handle, icon_uri.c_str()); + } + if (!ret) { + ret = account_update_to_db_by_id(account_handle, account_id); + } + + account_destroy(account_handle); + } + if (!ret) { + out["status"] = picojson::value("success"); + } else { + LoggerE("Failed to create account"); + REPORT_ERROR(out, UnknownException(GetErrorMsg(ret))); + } +} + #undef REPORT_ERROR } // namespace account diff --git a/src/account/account_manager.h b/src/account/account_manager.h index 4006c17..5148b32 100644 --- a/src/account/account_manager.h +++ b/src/account/account_manager.h @@ -66,6 +66,11 @@ class AccountManager { static bool ConvertProviderToObject(account_type_h provider, picojson::object& out); static std::string GetErrorMsg(int error); + + void AddAccount(const picojson::value& data, picojson::object& obj); + void RemoveAccount(const picojson::value& data, picojson::object& obj); + void UpdateAccount(const picojson::value& data, picojson::object& obj); + //bool IsValidPackage(const std::string& packageName); }; } // namespace account -- 2.7.4