-
Notifications
You must be signed in to change notification settings - Fork 0
/
CRUD.sol
44 lines (38 loc) · 1.07 KB
/
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
pragma solidity ^0.5.0;
contract CRUD{
struct User{
uint id;
string name;
}
User[] users;
uint nextId=1;
function addUser(string memory _name) public{
users.push(User(nextId,_name));
nextId++;
}
function readUser(uint _position) public view returns (string memory,uint){
if(_position <= users.length){
return (users[_position].name,users[_position].id);
}
}
function updateUser(uint _id,string memory _name) public returns(bool){
uint _i = findUser(_id);
users[_i].name=_name;
return true;
}
function removeUser(uint _id) public {
uint _i = findUser(_id);
delete users[_i];
}
function findUser(uint _id) internal returns(uint){
if(users.length > 0){
for(uint8 i=1; i < users.length ;i++){
if(users[i].id == _id){
return i;
}
}
revert("Users doesn't exists!");
}
revert("First Add users to user array!");
}
}