Skip to content

Instantly share code, notes, and snippets.

@herbievine
Last active January 29, 2022 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save herbievine/9ea954d282ba2043acc4946292c7e0e0 to your computer and use it in GitHub Desktop.
Save herbievine/9ea954d282ba2043acc4946292c7e0e0 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/utils/Counters.sol";
contract Hub {
event NoteCreated(
uint256 indexed noteId,
address indexed owner,
bytes32 contentId
);
event ContentAdded(bytes32 indexed contentId, string contentUri);
event ContentUpdated(bytes32 indexed contentId, string contentUri);
struct Note {
uint256 noteId;
address owner;
bytes32 contentId;
}
mapping(uint256 => Note) private noteRegistry;
mapping(bytes32 => string) private contentRegistry;
mapping(address => uint256) private addressToNumberOfNotes;
function createNote(
string calldata _contentUri
) external {
address _owner = msg.sender;
bytes32 _contentId = keccak256(abi.encode(_contentUri));
uint256 _noteId = addressToNumberOfNotes[_owner] + 1;
addressToNumberOfNotes[_owner] = _noteId;
noteRegistry[_noteId] = Note(
_noteId,
_owner,
_contentId
);
contentRegistry[_contentId] = _contentUri;
emit ContentAdded(_contentId, _contentUri);
emit NoteCreated(_noteId, _owner, _contentId);
}
function updateNote(uint256 _noteId, string calldata _contentUri) external {
address _owner = msg.sender;
bytes32 _contentId = keccak256(abi.encode(_contentUri));
noteRegistry[_noteId] = Note(
_noteId,
_owner,
_contentId
);
contentRegistry[_contentId] = _contentUri;
emit ContentAdded(_contentId, _contentUri);
}
function getContent(bytes32 _contentId) public view returns (string memory) {
return contentRegistry[_contentId];
}
function getNote(uint256 _noteId) public view returns (Note memory) {
address _owner = msg.sender;
Note memory noteToCheck = noteRegistry[_noteId];
Note memory noteToReturn;
if (noteToCheck.owner == _owner) {
noteToReturn = noteToCheck;
}
return noteToReturn;
}
function getNotes() public view returns (Note[] memory) {
address _owner = msg.sender;
uint256 totalNotes = addressToNumberOfNotes[_owner];
uint256 currentIndex = 0;
Note[] memory notes = new Note[](totalNotes);
for (uint256 i = 0; i < totalNotes; i++) {
if (noteRegistry[i + 1].owner == _owner) {
Note storage currentNote = noteRegistry[i + 1];
notes[currentIndex] = currentNote;
currentIndex += 1;
}
}
return notes;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment