-
Notifications
You must be signed in to change notification settings - Fork 0
/
Basic_CRUD.sol
45 lines (33 loc) · 877 Bytes
/
Basic_CRUD.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
pragma solidity ^0.5.0;
contract CRUD{
struct User{
uint id;
string name;
}
User[] public users;
uint public nextId=1;
function create(string memory name) public {
users.push(User(nextId,name));
nextId++;
}
function read(uint id) view public returns(uint,string memory){
uint i = find(id);
return (users[i].id, users[i].name);
}
function update(uint id, string memory name) public {
uint i = find(id);
users[i].name=name;
}
function destroy(uint id) public {
uint i = find(id);
delete users[i];
}
function find(uint id) view internal returns(uint) {
for(uint i=0; i<users.length;i++){
if(users[i].id==id){
return i;
}
}
revert('User does not exist!');
}
}