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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
| pragma solidity >=0.6.0 <0.7.0;
contract UserCrud {
struct UserStruct { bytes32 userEmail; uint userAge; uint index; } mapping(address => UserStruct) private userStructs; address[] private userIndex; event LogNewUser (address indexed userAddress, uint index, bytes32 userEmail, uint userAge); event LogUpdateUser(address indexed userAddress, uint index, bytes32 userEmail, uint userAge); event LogDeleteUser(address indexed userAddress, uint index); function isUser(address userAddress) public view returns(bool isIndeed) { if(userIndex.length == 0) return false; return (userIndex[userStructs[userAddress].index] == userAddress); } function insertUser( address userAddress, bytes32 userEmail, uint userAge) public returns(uint index) { if(isUser(userAddress)) {revert("this user already exist.");} userStructs[userAddress].userEmail = userEmail; userStructs[userAddress].userAge = userAge; userIndex.push(userAddress); userStructs[userAddress].index = userIndex.length-1; emit LogNewUser( userAddress, userStructs[userAddress].index, userEmail, userAge); return userIndex.length-1; } function deleteUser(address userAddress) public returns(uint index) { if(!isUser(userAddress)) {revert("only other user can deleteUser.");} uint rowToDelete = userStructs[userAddress].index; address keyToMove = userIndex[userIndex.length-1]; userIndex[rowToDelete] = keyToMove; userStructs[keyToMove].index = rowToDelete; userIndex.pop(); emit LogDeleteUser( userAddress, rowToDelete); emit LogUpdateUser( keyToMove, rowToDelete, userStructs[keyToMove].userEmail, userStructs[keyToMove].userAge); return rowToDelete; } function getUser(address userAddress) public view returns(bytes32 userEmail, uint userAge, uint index) { if(!isUser(userAddress)) {revert("only exist user can be run by getUser.");} return( userStructs[userAddress].userEmail, userStructs[userAddress].userAge, userStructs[userAddress].index); } function updateUserEmail(address userAddress, bytes32 userEmail) public returns(bool success) { if(!isUser(userAddress)) {revert("only exist user can updateUserEmail.");} userStructs[userAddress].userEmail = userEmail; emit LogUpdateUser( userAddress, userStructs[userAddress].index, userEmail, userStructs[userAddress].userAge); return true; } function updateUserAge(address userAddress, uint userAge) public returns(bool success) { if(!isUser(userAddress)) {revert("only exist user can updateUserAge.");} userStructs[userAddress].userAge = userAge; emit LogUpdateUser( userAddress, userStructs[userAddress].index, userStructs[userAddress].userEmail, userAge); return true; }
function getUserCount() public view returns(uint count) { return userIndex.length; }
function getUserAtIndex(uint index) public view returns(address userAddress) { return userIndex[index]; }
}
|