fiqih131 commited on
Commit
1ad3bbd
·
verified ·
1 Parent(s): 82e7ae8

Upload Mapping

Browse files
Files changed (1) hide show
  1. Mapping +91 -0
Mapping ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity ^0.8.17;
3
+
4
+ /**
5
+ * @title FavoriteRecords
6
+ * @dev Contract to manage a list of approved music records and allow users to add them to their favorites
7
+ */
8
+ contract FavoriteRecords {
9
+ // Mapping to store whether a record is approved
10
+ mapping(string => bool) private approvedRecords;
11
+ // Array to store the index of approved records
12
+ string[] private approvedRecordsIndex;
13
+
14
+ // Mapping to store user's favorite records
15
+ mapping(address => mapping(string => bool)) public userFavorites;
16
+ // Mapping to store the index of user's favorite records
17
+ mapping(address => string[]) private userFavoritesIndex;
18
+
19
+ // Custom error to handle unapproved records
20
+ error NotApproved(string albumName);
21
+
22
+ /**
23
+ * @dev Constructor that initializes the approved records list
24
+ */
25
+ constructor() {
26
+ // Predefined list of approved records
27
+ approvedRecordsIndex = [
28
+ "Thriller",
29
+ "Back in Black",
30
+ "The Bodyguard",
31
+ "The Dark Side of the Moon",
32
+ "Their Greatest Hits (1971-1975)",
33
+ "Hotel California",
34
+ "Come On Over",
35
+ "Rumours",
36
+ "Saturday Night Fever"
37
+ ];
38
+ // Initialize the approved records mapping
39
+ for (uint i = 0; i < approvedRecordsIndex.length; i++) {
40
+ approvedRecords[approvedRecordsIndex[i]] = true;
41
+ }
42
+ }
43
+
44
+ /**
45
+ * @dev Returns the list of approved records
46
+ * @return An array of approved record names
47
+ */
48
+ function getApprovedRecords() public view returns (string[] memory) {
49
+ return approvedRecordsIndex;
50
+ }
51
+
52
+ /**
53
+ * @dev Adds an approved record to the user's favorites
54
+ * @param _albumName The name of the album to be added
55
+ */
56
+ function addRecord(string memory _albumName) public {
57
+ // Check if the record is approved
58
+ if (!approvedRecords[_albumName]) {
59
+ revert NotApproved({albumName: _albumName});
60
+ }
61
+ // Check if the record is not already in the user's favorites
62
+ if (!userFavorites[msg.sender][_albumName]) {
63
+ // Add the record to the user's favorites
64
+ userFavorites[msg.sender][_albumName] = true;
65
+ // Add the record to the user's favorites index
66
+ userFavoritesIndex[msg.sender].push(_albumName);
67
+ }
68
+ }
69
+
70
+ /**
71
+ * @dev Returns the list of a user's favorite records
72
+ * @param _address The address of the user
73
+ * @return An array of user's favorite record names
74
+ */
75
+ function getUserFavorites(address _address) public view returns (string[] memory) {
76
+ return userFavoritesIndex[_address];
77
+ }
78
+
79
+ /**
80
+ * @dev Resets the caller's list of favorite records
81
+ */
82
+ function resetUserFavorites() public {
83
+ // Iterate through the user's favorite records
84
+ for (uint i = 0; i < userFavoritesIndex[msg.sender].length; i++) {
85
+ // Delete each record from the user's favorites mapping
86
+ delete userFavorites[msg.sender][userFavoritesIndex[msg.sender][i]];
87
+ }
88
+ // Delete the user's favorites index
89
+ delete userFavoritesIndex[msg.sender];
90
+ }
91
+ }