-
Notifications
You must be signed in to change notification settings - Fork 0
/
CRUD-CRUD.sol
110 lines (98 loc) · 3.17 KB
/
CRUD-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
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
// SPDX-License-Identifier: None
pragma solidity 0.8.21;
contract UserCrudString {
struct UserStruct {
string userEmail;
uint userAge;
uint index;
}
mapping(address => UserStruct) private userStructs;
address[] private userIndex;
event LogNewUser (address indexed userAddress, uint index, string userEmail, uint userAge);
event LogUpdateUser(address indexed userAddress, uint index, string 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,
string memory userEmail,
uint userAge)
external
returns(uint index)
{
require(!isUser(userAddress), "Already exists");
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 getUser(address userAddress) external view
returns(string memory userEmail, uint userAge, uint index)
{
require(isUser(userAddress), "User not found");
return(
userStructs[userAddress].userEmail,
userStructs[userAddress].userAge,
userStructs[userAddress].index);
}
function updateUserEmail(address userAddress, string memory userEmail)
external
returns(bool success)
{
require(isUser(userAddress), "User not found");
userStructs[userAddress].userEmail = userEmail;
emit LogUpdateUser(
userAddress,
userStructs[userAddress].index,
userEmail,
userStructs[userAddress].userAge);
success = true;
return success;
}
function updateUserAge(address userAddress, uint userAge)
external
returns(bool success)
{
require(isUser(userAddress), "User not found");
userStructs[userAddress].userAge = userAge;
emit LogUpdateUser(
userAddress,
userStructs[userAddress].index,
userStructs[userAddress].userEmail,
userAge);
return true;
}
function deleteUser(address userAddress)
external
returns(uint index)
{
require(isUser(userAddress), "User not found");
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 getUserCount() external view returns(uint count)
{
return userIndex.length;
}
function getUserAtIndex(uint index) external view returns(address userAddress)
{
return userIndex[index];
}
}