Created
August 9, 2018 05:39
-
-
Save epicdistraction/e17cb350004703d6d3787fb84d1122da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Users.hpp | |
#include <eosiolib/eosio.hpp> | |
#include <eosiolib/print.hpp> | |
#include <string> | |
#include <vector> | |
namespace hodlong { | |
using namespace eosio; | |
using std::string; | |
class Users : public contract { | |
using contract::contract; | |
public: | |
Users(account_name self) : contract(self) {} | |
// @abi action | |
void add(const account_name account, string &username); | |
// @abi action | |
void getuser(const account_name account); | |
// @abi action | |
void additem(account_name account, uint64_t itemId); | |
private: | |
// @abi table user i64 | |
struct user { | |
uint64_t account_name; | |
string username; | |
vector <uint64_t> itemIds; | |
uint64_t primary_key() const { return account_name; } | |
EOSLIB_SERIALIZE(user, (account_name)(username)(itemIds) | |
); | |
}; | |
typedef multi_index<N(user), user> userIndex; | |
}; | |
EOSIO_ABI(Users, (add)(getuser)(additem)); | |
} | |
Users.cpp | |
#include "Users.hpp" | |
namespace hodlong { | |
void Users::add(const account_name account, string &username) { | |
require_auth(account); | |
userIndex users(_self, _self); | |
auto iterator = users.find(account); | |
eosio_assert(iterator == users.end(), "Address for account already exists"); | |
users.emplace(account, [&](auto &user) { | |
user.account_name = account; | |
user.username = username; | |
}); | |
} | |
void Users::getuser(const account_name account) { | |
userIndex users(_self, _self); | |
auto iterator = users.find(account); | |
eosio_assert(iterator != users.end(), "Address for account not found"); | |
auto currentUser = users.get(account); | |
print("Username: ", currentUser.username.c_str()); | |
} | |
void Users::additem(account_name account, uint64_t itemId) { | |
require_auth(account); | |
userIndex users(_self, _self); | |
auto iterator = users.find(account); | |
eosio_assert(iterator != users.end(), "Address for account not found"); | |
users.modify(iterator, account, [&](auto& user) { | |
user.itemIds.push_back(itemId); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment