Last active
January 5, 2019 09:43
-
-
Save vikiival/0a031323d11f94f0ec74e6b04c3838d7 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.5.2+commit.1df8f40c.js&optimize=false&gist=
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity >=0.4.22 <0.6.0; | |
contract SimpleContract { | |
struct TodoItem { | |
uint256 id; | |
string name; | |
bool completed; | |
} | |
struct User { | |
string name; | |
TodoItem[] todos; | |
} | |
address owner; | |
mapping(address => User) users; | |
constructor() public { | |
owner = msg.sender; | |
} | |
function addTodo(address _toUser, string memory _name) public { | |
uint256 arraySize = users[_toUser].todos.length; | |
users[_toUser].todos.push(TodoItem({id: arraySize, name: _name, completed: false})); | |
} | |
function getTodoLength(address _byUser) public view returns (uint256) { | |
return users[_byUser].todos.length; | |
} | |
function getTodoItem(address _byUser, uint _todoId) public view returns (uint, string memory, bool) { | |
require(users[_byUser].todos.length > _todoId, "Bad Id inseted"); | |
TodoItem memory todo = users[_byUser].todos[_todoId]; | |
return (todo.id, todo.name, todo.completed); | |
} | |
function toggleTodo(address _byUser, uint _todoId, bool _completed) public { | |
require(msg.sender == owner || msg.sender == _byUser, | |
"Only owner or assigned user can change task to completed"); | |
users[_byUser].todos[_todoId].completed = _completed; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment