Skip to content

Instantly share code, notes, and snippets.

@jedsada-gh
Created February 13, 2022 04:03
Show Gist options
  • Save jedsada-gh/55bd8a5270367ad8984fa187f38daa9c to your computer and use it in GitHub Desktop.
Save jedsada-gh/55bd8a5270367ad8984fa187f38daa9c to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
contract TodoList {
struct Task {
bytes32 id;
string title;
string description;
bool mark;
}
mapping(address => Task[]) taskLookups;
function create(string memory _title, string memory _description) public {
bytes32 id = keccak256(abi.encodePacked(block.timestamp,
taskLookups[msg.sender].length + 1)
);
taskLookups[msg.sender].push(Task(id, _title, _description, false));
}
function getById(bytes32 id) public view returns(Task memory) {
uint index = findIndexById(id);
return taskLookups[msg.sender][index];
}
function getAll() public view returns(Task[] memory) {
return taskLookups[msg.sender];
}
function marking(bytes32 id, bool status) public returns(bool) {
uint index = findIndexById(id);
taskLookups[msg.sender][index].mark = status;
return true;
}
function remove(bytes32 id) public returns(bool) {
uint index = findIndexById(id);
taskLookups[msg.sender][index] = taskLookups[msg.sender][taskLookups[msg.sender].length - 1];
taskLookups[msg.sender].pop();
return true;
}
function update(bytes32 id, string memory title, string memory description) public {
uint index = findIndexById(id);
taskLookups[msg.sender][index].title = title;
taskLookups[msg.sender][index].description = description;
}
function findIndexById(bytes32 id) private view returns(uint) {
Task[] memory tasks = taskLookups[msg.sender];
for(uint i = 0; i < tasks.length; i++) {
if (tasks[i].id == id) {
return i;
}
}
revert("not found item");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment