203 lines
6.6 KiB
C++
203 lines
6.6 KiB
C++
#include "UserModelServiceServer.h"
|
|
|
|
#include "server/sparkd/persistence/SqliteStore.h"
|
|
#include "lib/logging/logging.h"
|
|
#include "lib/api/services/AccountService.pb.h"
|
|
#include "lib/utils/PBUtils.h"
|
|
|
|
#include <boost/tokenizer.hpp>
|
|
#include <fmt/format.h>
|
|
#include <map>
|
|
#include <set>
|
|
#include <utility>
|
|
|
|
namespace bts {
|
|
namespace account{
|
|
|
|
UserModelServiceServer::UserModelServiceServer(bts::api::ServiceContext& context)
|
|
:
|
|
bts::api::account::UserModelServiceBase (context),
|
|
_table_name("users"),
|
|
_unique_key("0"),
|
|
_state(),
|
|
_store(new bts::persistence::SqliteStore("user_model"))
|
|
{
|
|
LOG(Gen, Info, "UserModelServiceServer created");
|
|
std::map<std::string,std::map<std::string,std::string>> all_data = _store->retrieve_all();
|
|
if (all_data.size()==0)
|
|
// write the first row
|
|
_update_and_publish();
|
|
else
|
|
{
|
|
auto table = all_data.find(_table_name);
|
|
bool data_found = false;
|
|
if (table != all_data.end())
|
|
{
|
|
auto row = table->second.find(_unique_key);
|
|
if (row != table->second.end())
|
|
{
|
|
std::string& ser_data = row->second;
|
|
if (_state.ParseFromString(ser_data))
|
|
{
|
|
data_found = true;
|
|
_update_and_publish();
|
|
}
|
|
}
|
|
}
|
|
if (!data_found)
|
|
LOG(Gen, Error, "Could not locate user data");
|
|
}
|
|
}
|
|
|
|
UserModelServiceServer::~UserModelServiceServer() {}
|
|
|
|
void UserModelServiceServer::create_account(const api::account::AccountCreateAPIArgument& acct_create_arg,
|
|
const api::Response<::bts::api::VoidMessage>&)
|
|
{
|
|
if (_find_account(acct_create_arg.account_name())==nullptr){
|
|
bts::api::account::BTSAccount* new_account = _state.add_accounts();
|
|
new_account->set_id(_find_curr_acct_id()+1);
|
|
new_account->set_name(acct_create_arg.account_name());
|
|
new_account->set_description(acct_create_arg.description());
|
|
new_account->set_is_active(false);
|
|
_update_and_publish();
|
|
}
|
|
}
|
|
|
|
void UserModelServiceServer::set_account_clip_limits(const api::account::AccountSecurityClipLimitsAPIArgument& acct_clips_arg,
|
|
const api::Response<::bts::api::VoidMessage>&)
|
|
{
|
|
bts::api::account::BTSAccount* account = _find_account(acct_clips_arg.account_id());
|
|
if (account != nullptr)
|
|
{
|
|
// if(!account->has_account_data())
|
|
// {
|
|
// account->mutable_account_data()->mutable_security_clip_limits()->mutable_limit()->Clear();
|
|
// }
|
|
}
|
|
else{
|
|
// TODO: return error
|
|
}
|
|
}
|
|
|
|
|
|
void UserModelServiceServer::set_account_product_position_limits(const api::account::AccountProductPositionLimitsAPIArgument&,
|
|
const api::Response<::bts::api::VoidMessage>&)
|
|
{
|
|
}
|
|
|
|
void UserModelServiceServer::set_account_security_position_limits(const api::account::AccountSecurityPositionLimitsAPIArgument&,
|
|
const api::Response<::bts::api::VoidMessage>&)
|
|
{
|
|
}
|
|
|
|
void UserModelServiceServer::set_account_margin_limits(const api::account::AccountMarginLimitAPIArgument&,
|
|
const api::Response<::bts::api::VoidMessage>&)
|
|
{
|
|
}
|
|
|
|
void UserModelServiceServer::set_account_connections(const api::account::AccountConnectionsAPIArgument&,
|
|
const api::Response<::bts::api::VoidMessage>&)
|
|
{
|
|
}
|
|
|
|
void UserModelServiceServer::set_account_exchange_data(const api::account::AccountExchangeDataAPIArgument&,
|
|
const api::Response<::bts::api::VoidMessage>&)
|
|
{
|
|
}
|
|
|
|
void UserModelServiceServer::create_user(const api::account::UserCreateAPIArgument&,
|
|
const api::Response<::bts::api::VoidMessage>&)
|
|
{
|
|
}
|
|
|
|
void UserModelServiceServer::set_user_account_association(const api::account::UserAccountAssociationAPIArgument&,
|
|
const api::Response<::bts::api::VoidMessage>&)
|
|
{
|
|
}
|
|
|
|
void UserModelServiceServer::set_user_cme_md_entitlement_data(const api::account::CMEUserMarketDataEntitlementDataAPIArgument&,
|
|
const api::Response<::bts::api::VoidMessage>&)
|
|
{
|
|
}
|
|
|
|
void UserModelServiceServer::set_user_cme_routing_data(const api::account::CMEUserDataAPIArgument&,
|
|
const api::Response<::bts::api::VoidMessage>&)
|
|
{
|
|
}
|
|
|
|
void UserModelServiceServer::get_user_model(const bts::api::VoidMessage&,
|
|
const api::Response<::bts::api::account::AdminData>&)
|
|
{
|
|
}
|
|
|
|
void UserModelServiceServer::_update_and_publish()
|
|
{
|
|
std::string ser_state;
|
|
if (_state.SerializeToString(&ser_state))
|
|
{
|
|
LOG(Gen, Info, "Could not save user data");
|
|
_store->update(_table_name, _unique_key, ser_state);
|
|
// publish it
|
|
}
|
|
else
|
|
LOG(Gen, Error, "Could not save user data");
|
|
}
|
|
|
|
bts::api::account::BTSAccount* UserModelServiceServer::_find_account (const std::string& acct_name){
|
|
for(int i=0; i<_state.accounts_size(); i++){
|
|
bts::api::account::BTSAccount* acct = _state.mutable_accounts(i);
|
|
if(acct_name==acct->name())
|
|
return acct;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
bts::api::account::BTSAccount* UserModelServiceServer::_find_account (int id){
|
|
for(int i=0; i<_state.accounts_size(); i++){
|
|
bts::api::account::BTSAccount* acct = _state.mutable_accounts(i);
|
|
if(id == acct->id())
|
|
return acct;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
bts::api::account::BTSUser* UserModelServiceServer::_find_user (const std::string& user_name){
|
|
for(int i=0; i<_state.users_size(); i++){
|
|
bts::api::account::BTSUser* user = _state.mutable_users(i);
|
|
if(user_name==user->name())
|
|
return user;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
bts::api::account::BTSUser* UserModelServiceServer::_find_user (const int id){
|
|
for(int i=0; i<_state.users_size(); i++){
|
|
bts::api::account::BTSUser* user = _state.mutable_users(i);
|
|
if(id == user->id())
|
|
return user;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
int UserModelServiceServer::_find_curr_acct_id (){
|
|
int rv = 0;
|
|
for(int i=0; i<_state.accounts_size(); i++){
|
|
bts::api::account::BTSAccount* acct = _state.mutable_accounts(i);
|
|
if (acct->id() > rv)
|
|
rv = acct->id();
|
|
}
|
|
return rv;
|
|
}
|
|
|
|
int UserModelServiceServer::_find_curr_user_id (){
|
|
int rv = 0;
|
|
for(int i=0; i<_state.users_size(); i++){
|
|
bts::api::account::BTSUser* user = _state.mutable_users(i);
|
|
if (user->id() > rv)
|
|
rv = user->id();
|
|
}
|
|
return rv;
|
|
}
|
|
|
|
}} |