Experiment No.
9
Class : BE (AIML) Sub : BLCLAB Batch : A2
---------------------------------------------------------------------------------
Aim : Decentralized Note Making App Using CRUD Operation
Code : [Link]
// SPDX-License-Identifier: MIT
pragma solidity >=0.5.0 <0.9.0;
contract NoteStorage {
struct Note {
uint256 id;
string title;
string content;
bool exists;
}
mapping(uint256 => Note) private notes;
uint256 private noteCount;
event NoteCreated(uint256 id, string title);
event NoteUpdated(uint256 id, string title);
event NoteDeleted(uint256 id);
// Create a new note
function createNote(string memory _title, string memory _content) public {
noteCount++;
notes[noteCount] = Note(noteCount, _title, _content, true);
emit NoteCreated(noteCount, _title);
}
// Read a note by ID
function getNote(uint256 _id) public view returns (uint256, string memory,
string memory) {
require(notes[_id].exists, "Note does not exist");
Note memory note = notes[_id];
return ([Link], [Link], [Link]);
}
// Update a note
function updateNote(uint256 _id, string memory _title, string memory
_content) public {
require(notes[_id].exists, "Note does not exist");
notes[_id].title = _title;
notes[_id].content = _content;
emit NoteUpdated(_id, _title);
}
// Delete a note
function deleteNote(uint256 _id) public {
require(notes[_id].exists, "Note does not exist");
delete notes[_id];
emit NoteDeleted(_id);
}
// Get all existing note IDs
function getNoteIds() public view returns (uint256[] memory) {
uint256[] memory ids = new uint256[](noteCount);
uint256 counter = 0;
for (uint256 i = 1; i <= noteCount; i++) {
if (notes[i].exists) {
ids[counter] = i;
counter++;
}
}
// Resize array
uint256[] memory result = new uint256[](counter);
for (uint256 j = 0; j < counter; j++) {
result[j] = ids[j];
}
return result;
}
}
Output :