· 6 years ago · Mar 10, 2019, 12:34 AM
1#include <eosiolib/eosio.hpp>
2// #include "includes/person.hpp"
3
4using namespace eosio;
5using namespace std;
6
7/**
8 * Assymptions:
9 * 1. The only account authorized to modify the address book is the user.
10 * 2. the primary_key of our table is unique, based on username
11 * 3. For usability, the contract should have the ability to both create and modify a table row with a single action.
12 */
13class [[eosio::contract("addressbook")]] addressbook : public contract {
14 public:
15 using contract::contract;
16
17 addressbook(name receiver, name code, datastream<const char*> ds) : contract(receiver, code, ds) {}
18
19 [[eosio::action]]
20 void upsert(name user, string first_name, string last_name, string street, string city, string state) {
21 // The only account authorized to modify the address book is the user.
22 require_auth(user);
23
24 /*
25 Instantiate the table. Previously, a multi_index table was configured, and declared it as address_index.
26 To instantiate a table, consider it's two required arguments:
27 1. The "code", which represents the contract's account. This value is accessible through the scoped _code variable.
28 2. The "scope" which ensures the uniqueness of the contract. In this case, since we only have one table we can use "_code" as well
29 */
30 address_index addresses( _code, _code.value );
31
32 // Next, query the iterator, setting it to a variable since this iterator will be used several times
33 auto iterator = addresses.find(user.value);
34
35 /*
36 Detect whether or not the particular user already exists. To do this, use table's find method by passing the user parameter.
37 The find method will return an iterator. Use that iterator to test it against the end method.
38 The "end" method is an alias for "null".
39 */
40 if (iterator == addresses.end()) {
41 // The user IS NOT in the table
42 addresses.emplace(user, [&](auto& row) {
43 row.key = user;
44 row.first_name = first_name;
45 row.last_name = last_name;
46 row.street = street;
47 row.city = city;
48 row.state = state;
49 });
50 } else {
51 // The user is in the table
52 addresses.modify(iterator, user, [&](auto& row) {
53 row.key = user;
54 row.first_name = first_name;
55 row.last_name = last_name;
56 row.street = street;
57 row.city = city;
58 row.state = state;
59 });
60 }
61 }
62
63 [[eosio::action]]
64 void erase(name user) {
65 require_auth(user);
66
67 address_index addresses(_code, _code.value);
68 auto iterator = addresses.find(user.value);
69 eosio_assert(iterator != addresses.end(), "Record does not exist");
70 addresses.erase(iterator);
71 }
72
73 private:
74 struct [[eosio::table]] person {
75 name key;
76 string first_name;
77 string last_name;
78 string street;
79 string city;
80 string state;
81
82 uint64_t primary_key() const { return key.value; }
83 };
84
85 typedef multi_index<"people"_n, person> address_index;
86};
87
88EOSIO_DISPATCH( addressbook, (upsert) (erase) )
89
90// Command to build: rm -rf ./build && mkdir build && eosio-cpp -o ./build/addressbook.wasm ./src/addressbook.cpp --abigen;