-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathCrud.sol
41 lines (34 loc) · 941 Bytes
/
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
contract Crud {
struct User {
uint id;
string name;
}
User[] public users;
uint public nextId = 1;
function create(string memory name) external {
users.push(User(nextId, name));
nextId++;
}
function read(uint id) view external returns(uint, string memory) {
uint i = find(id);
return(users[i].id, users[i].name);
}
function update(uint id, string memory name) external {
uint i = find(id);
users[i].name = name;
}
function destroy(uint id) external {
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');
}
}