ToDo List Smart Contract
// SPDX-License-Identifier: MIT
pragma solidity 0.8.18;
contract TaskContract {
    constructor() {
    }
    struct Task {
        uint id;
        string taskTitle;
        string taskText;
        bool isDeleted;
    }
    Task[] private tasks;
    mapping (uint => address) taskToOwner;
    event AddTask(address recepient,uint taskId);
    event DeleteTask(uint taskId,bool isDeleted);
    function addTask(string memory taskText,string memory taskTitle, bool isDeleted) external {
        uint taskId = tasks.length;
        tasks.push(Task(taskId,taskTitle, taskText, isDeleted));
        taskToOwner[taskId] = msg.sender;
        emit AddTask(msg.sender,taskId);
    }
    function deleteTask(uint taskId) external {
        require(taskToOwner[taskId] == msg.sender, "You are not the owner of this task");
        tasks[taskId].isDeleted = true;
        emit DeleteTask(taskId,tasks[taskId].isDeleted);
    }
    function getMyTask()public view returns(Task[] memory){
        Task[] memory myTasks = new Task[](tasks.length);
        uint counter = 0;
        for(uint i = 0; i < tasks.length; i++){
            if(taskToOwner[i] == msg.sender && tasks[i].isDeleted == false){
                myTasks[counter] = tasks[i];
                counter++;
            }
        }
        Task[] memory result = new Task[](counter);
        for(uint i = 0; i < counter; i++){
            result[i] = myTasks[i];
        }
        return result;
    }
}
Explanation and Breakdown
// SPDX-License-Identifier: MIT
pragma solidity 0.8.18;
contract TaskContract {
    constructor() {
    }
struct Task {
        uint id;
        string taskTitle;
        string taskText;
        bool isDeleted;
    }
    Task[] private tasks;
    mapping (uint => address) taskToOwner;
    event AddTask(address recepient,uint taskId);
    event DeleteTask(uint taskId,bool isDeleted);
The code defines a struct called Task with four fields: an ID (uint), a task title (string), a task text (string), and a Boolean flag indicating whether the task has been deleted (bool).
The code declares a private Task array called tasks to store all the tasks created by the contract.
The code defines a mapping called taskToOwner, which maps a task ID (uint) to the address of the user who created the task.
- 
The code includes two events:
- AddTask: emitted when a new task is added to the tasks array. The event includes the address of the user who added the task and the ID of the new task.
 - DeleteTask: emitted when a task is marked as deleted in the tasks array. The event includes the ID of the deleted task and a flag indicating that it has been deleted.
 
 The code uses these constructs to define the data model for a task management system, where tasks are represented by the Task struct, stored in the tasks array, and associated with their creators via the taskToOwner mapping. The events allow for tracking and auditing of task creation and deletion.
 function addTask(string memory taskText,string memory taskTitle, bool isDeleted) external {
        uint taskId = tasks.length;
        tasks.push(Task(taskId,taskTitle, taskText, isDeleted));
        taskToOwner[taskId] = msg.sender;
        emit AddTask(msg.sender,taskId);
    }
    function deleteTask(uint taskId) external {
        require(taskToOwner[taskId] == msg.sender, "You are not the owner of this task");
        tasks[taskId].isDeleted = true;
        emit DeleteTask(taskId,tasks[taskId].isDeleted);
    }
The
addTaskfunction is a public function that takes three arguments: a string for the task text, a string for the task title, and a boolean indicating whether the task is marked as deleted. It is used to add new tasks to thetasksarray and associate the new task with the caller's address via thetaskToOwnermapping.Within the function, the ID for the new task is set to the length of the
tasksarray. A new task with the given text, title, and deletion flag is created using theTaskstruct constructor and added to thetasksarray.The caller's address is then associated with the new task ID in the
taskToOwnermapping.Finally, an
AddTaskevent is emitted with the caller's address and the ID of the new task.The
deleteTaskfunction is a public function that takes one argument: the ID of the task to be deleted. It is used to mark a task as deleted in thetasksarray.Within the function, the caller's address is checked against the address associated with the given task ID in the
taskToOwnermapping to ensure that the caller is the owner of the task.If the caller is the owner of the task, the
isDeletedflag for the task is set totruein thetasksarray.Finally, a
DeleteTaskevent is emitted with the ID of the deleted task and a flag indicating that it has been marked as deleted.
function getMyTask()public view returns(Task[] memory){
        Task[] memory myTasks = new Task[](tasks.length);
        uint counter = 0;
        for(uint i = 0; i < tasks.length; i++){
            if(taskToOwner[i] == msg.sender && tasks[i].isDeleted == false){
                myTasks[counter] = tasks[i];
                counter++;
            }
        }
        Task[] memory result = new Task[](counter);
        for(uint i = 0; i < counter; i++){
            result[i] = myTasks[i];
        }
        return result;
    }
The
getMyTaskfunction is a public function that returns an array ofTaskstruct objects for the caller. It is used to retrieve all non-deleted tasks associated with the caller's address in thetasksarray.Within the function, a new
Taskarray calledmyTasksis created with the same length as thetasksarray. A counter variable is also initialized to 0.A for loop is used to iterate over all tasks in the
tasksarray. If a task is associated with the caller's address in thetaskToOwnermapping and is not marked as deleted, it is added to themyTasksarray at the current index indicated by thecountervariable, and thecountervariable is incremented.After the first for loop, a new
Taskarray calledresultis created with a length equal to thecountervariable.A second for loop is used to iterate over the
myTasksarray and populate theresultarray with the non-nullTaskobjects.Finally, the
resultarray is returned as the result of thegetMyTaskfunction.
              
    
Top comments (0)