-
Notifications
You must be signed in to change notification settings - Fork 1
/
GreetingMultiple.sol
94 lines (73 loc) · 2.64 KB
/
GreetingMultiple.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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.21;
contract GreetingMultiple {
uint256 public constant MAX_GREETINGS = 3;
event GreetingAdded(string message);
event GreetingDeleted(string message);
error ContractClosedOrPaused(string message);
error CallerIsNotTheOwner(string message);
struct Data {
string[] greetings;
uint256 timestamp;
}
mapping(address => Data) public allGreetings;
enum Status {
Open, // 0
Closed, // 1
Paused // 2
}
address public owner;
Status status;
constructor() {
owner = msg.sender;
status = Status.Open;
}
modifier checkIfContractIsOpen() {
// require(status == Status.Open, "Contract is closed or paused");
if(status != Status.Open) {
revert ContractClosedOrPaused({ message: "Contract is not open" });
}
_;
}
modifier checkIfCallerIsOwner() {
if(owner != msg.sender) {
revert CallerIsNotTheOwner({ message: "You are not the owner of the contract" });
}
_;
}
function storeGreeting(string memory _value) external checkIfContractIsOpen {
Data storage _data = allGreetings[msg.sender];
require(_data.greetings.length < MAX_GREETINGS, "Max greetings reached!");
_data.greetings.push(_value);
_data.timestamp = block.timestamp;
emit GreetingAdded("Greeting added successfully");
}
function retrieveGreetingsByUser(address _addr) external view returns(string[] memory) {
return allGreetings[_addr].greetings;
}
function retrieveGreetings() external view returns(string[] memory) {
return allGreetings[msg.sender].greetings;
}
function deleteGreeting(uint256 index) external checkIfContractIsOpen returns (bool) {
delete allGreetings[msg.sender].greetings[index];
return true;
}
function deleteAndOrganizeGreetings(uint256 index) external returns (bool) {
string[] storage _greetings = allGreetings[msg.sender].greetings;
_greetings[index] = _greetings[_greetings.length - 1];
_greetings.pop();
emit GreetingDeleted("Greeting deleted successfully");
return true;
}
function deleteUser() external returns(string memory) {
delete allGreetings[msg.sender];
return "User deleted successfully";
}
function deleteUserByAddress(address _addr) external checkIfCallerIsOwner returns(string memory) {
delete allGreetings[_addr];
return "User deleted successfully";
}
function updateStatus(Status _status) external {
status = _status;
}
}