-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToDoList.sol
74 lines (58 loc) · 2.09 KB
/
ToDoList.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract ToDoList {
struct Task {
uint id;
string name;
string date;
}
address owner;
Task task;
mapping (uint => Task) tasks; // list of all tasks
uint taskId = 1;
event taskCreate(uint taskId, string name);
event taskUpdate(uint taskId, string name);
event taskDelete(uint taskId);
/*Modifiers in Solidity are used to add custom conditions
or checks to functions before they can be executed.
They provide a way to encapsulate common checks and conditions,
making code more readable, maintainable, and secure. */
modifier checkId(uint id){
require(id != 0 && id < taskId, "Invalid Id");
_;
}
modifier onlyOwner(){
require(msg.sender==owner,"Invalid Owner");
_;
}
constructor() {
owner = msg.sender;
}
// In Solidity, calldata is a special location where function arguments are stored.
// It is used for function parameters that are read-only and used for input data.
function createTask( string calldata _taskName, string calldata _data ) public {
tasks[taskId] = Task(taskId, _taskName, _data);
taskId++;
emit taskCreate(taskId,_taskName);
}
function updateTask( uint _taskId, string calldata _taskName, string calldata _data) checkId(_taskId) public {
tasks[_taskId] = Task(_taskId, _taskName, _data);
emit taskUpdate(taskId,_taskName);
}
function allTask() public view returns(Task[] memory){
Task[] memory taskList = new Task[](taskId-1);// array -> lenght -> taskId-1
for(uint i=0;i<taskId-1;i++){
taskList[i]=tasks[i+1];
}
return taskList;
}
function viewTask(uint _taskId) checkId(_taskId) public view returns(Task memory){
return tasks[_taskId];
}
function deleteTask(uint _taskId) checkId(_taskId) public {
// takes the values to their initial
delete tasks[_taskId];
emit taskDelete(_taskId);
}
}
// contarct address -> 0x51f36149ff5ffb1dc7310c0226ce08ed4cbd711d