-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.Structs.sol
61 lines (52 loc) · 1.78 KB
/
3.Structs.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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;
contract ShoppingList {
mapping(address => User) users;
struct User {
mapping(string => List) lists;
string[] listNames;
}
struct Item {
string name;
uint256 quantity;
}
struct List {
string name;
Item[] items;
}
function listExists(string memory name) internal view returns (bool) {
// if name of accessed list is empty than list has not been created
return bytes(users[msg.sender].lists[name].name).length != 0;
}
function createList(string memory name) public {
require(!listExists(name), "a list with this name already exists");
require(bytes(name).length > 0, "name cannot be empty");
users[msg.sender].listNames.push(name);
users[msg.sender].lists[name].name = name;
}
function getListNames() public view returns (string[] memory) {
return users[msg.sender].listNames;
}
function getItemNames(string memory listName)
public
view
returns (string[] memory)
{
require(listExists(listName), "no list with this name exists");
string[] memory names = new string[](
users[msg.sender].lists[listName].items.length
);
for (uint256 idx; idx < names.length; idx++) {
names[idx] = users[msg.sender].lists[listName].items[idx].name;
}
return names;
}
function addItem(
string memory listName,
string memory itemName,
uint256 quantity
) public {
require(listExists(listName), "no list with this name exists");
users[msg.sender].lists[listName].items.push(Item(itemName, quantity));
}
}