-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRobotFactory.cpp
More file actions
109 lines (91 loc) · 3.92 KB
/
RobotFactory.cpp
File metadata and controls
109 lines (91 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "RobotFactory.hpp"
namespace Factory {
void RobotFactory::create(account_name account, robot newRobot) {
/**
* We access the "robot" table as creating an object of type "robotIndex"
* As parameters we pass code & scope - _self from the parent contract
*/
robotIndex robots(_self, _self);
/**
* We must verify that the robot doesn't exist yet
* If the robot exist the iterator variable should be equal to robots.end()
*/
auto iterator = robots.find(newRobot.series_number);
eosio_assert(iterator == robots.end(), "Robot with this series number already exists");
/**
* We add the new robot in the table using emplace
* The first argument is the payer of the storage which will store the data.
* In our case, we have set the current account who trigger the action to be the payer.
* In some cases, we can set ourselves
*/
robots.emplace(account, [&](auto& robot) {
robot.series_number = newRobot.series_number;
robot.model = newRobot.model;
robot.operating_system = newRobot.operating_system;
robot.profession = newRobot.profession;
/**
* name{account} - We're getting the string representation of the account
*/
robot.owner = name{account}.to_string();
/**
* now() - Returns the time in seconds from 1970 of the block including this action
*/
robot.manufactured = now();
});
}
void RobotFactory::update(account_name account, uint64_t series_number, string operating_system) {
/**
* Verifies that name exists in the set of provided auths on a action. Throws if not found.
*/
require_auth(account);
/**
* We access the "robot" table as creating an object of type "robotIndex"
* As parameters we pass code & scope - _self from the parent contract
*/
robotIndex robots(_self, _self);
/**
* We must verify that the robot exist before updating its data
* If the robot doesn't exist the iterator variable won't be equal to robots.end()
*/
auto iterator = robots.find(series_number);
eosio_assert(iterator != robots.end(), "Robot not found");
/**
* We're updating the data using modify
*/
robots.modify(iterator, account, [&](auto& robot) {
robot.operating_system = operating_system;
});
}
void RobotFactory::getbyid(uint64_t robotId) {
/**
* We access the "robot" table as creating an object of type "robotIndex"
* As parameters we pass code & scope - _self from the parent contract
*/
robotIndex robots(_self, _self);
/**
* We must verify that the robot exist before getting its data
*/
auto iterator = robots.find(robotId);
eosio_assert(iterator != robots.end(), "Robot not found");
/**
* Getting the robot by its robotId (series number) and printing the data on the console
*/
auto robot = robots.get(robotId);
print("Series Number: ", robot.series_number);
print(" | Model: ", robot.model.c_str());
print(" | OS: ", robot.operating_system.c_str());
print(" | Profession: ", robot.profession.c_str());
print(" | Owner: ", robot.owner.c_str());
print(" | Manufactured On: ", robot.manufactured);
}
void RobotFactory::remove(account_name account, uint64_t robotId) {
/**
* Verifies that name exists in the set of provided auths on a action. Throws if not found.
*/
require_auth(account);
robotIndex robots(_self, _self);
auto iterator = robots.find(robotId);
eosio_assert(iterator != robots.end(), "Robot not found");
robots.erase(iterator);
}
}