-
Notifications
You must be signed in to change notification settings - Fork 0
/
TruffleCrud.sol
53 lines (43 loc) · 1.26 KB
/
TruffleCrud.sol
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
pragma solidity ^0.5.1;
contract TruffleCrud {
struct Truffle {
uint id;
string name;
}
Truffle[] public truffles;
uint public nextId = 1;
function create(string memory _name) public {
truffles.push(Truffle(nextId, _name));
nextId++;
}
function read(uint id) public view returns (uint, string memory) {
for (uint i = 0; i <= truffles.length; i++) {
if (truffles[i].id == id) {
return (truffles[i].id, truffles[i].name);
}
}
}
function update(uint id, string memory name) public {
uint i = find(id);
truffles[i].name = name;
}
// costs more gas than the above...
function updateTwo(uint id, string memory name) public {
for (uint i = 0; i < truffles.length; i++) {
if (truffles[i].id == id) {
truffles[i].name = name;
}
}
}
function destroy(uint id) public {
uint i = find(id);
delete truffles[i];
}
function find(uint id) view internal returns(uint) {
for (uint i = 0; i < truffles.length; i++) {
if (truffles[i].id == id) {
return i;
}
}
}
}