Step 1: implement DTwitter contract methods

This commit is contained in:
Pascal Precht 2018-10-24 20:44:24 +02:00
parent ebdb47f12d
commit 86849aaabb
No known key found for this signature in database
GPG Key ID: 0EE28D8D6FD85D7D
1 changed files with 14 additions and 2 deletions

View File

@ -76,10 +76,14 @@ contract DTwitter {
// add a user to the users mapping and populate details // add a user to the users mapping and populate details
// (creationDate, owner, username, description) // (creationDate, owner, username, description)
users[usernameHash].creationDate = now;
users[usernameHash].owner = msg.sender;
users[usernameHash].username = username;
users[usernameHash].description = description;
// add entry to our owners mapping so we can retrieve // add entry to our owners mapping so we can retrieve
// user by their address // user by their address
owners[msg.sender] = usernameHash;
} }
/** /**
@ -96,9 +100,13 @@ contract DTwitter {
require(users[usernameHash].owner == msg.sender); require(users[usernameHash].owner == msg.sender);
// update the description (could be empty) // update the description (could be empty)
users[usernameHash].description = description;
// only update the user's picture if the hash passed in is // only update the user's picture if the hash passed in is
// not empty or null (essentially disallows deletions) // not empty or null (essentially disallows deletions)
if (bytes(pictureHash).length > 0) {
users[usernameHash].picture = pictureHash;
}
} }
/** /**
@ -110,7 +118,7 @@ contract DTwitter {
*/ */
function userExists(bytes32 usernameHash) public view returns (bool) { function userExists(bytes32 usernameHash) public view returns (bool) {
// must check a property... bc solidity! // must check a property... bc solidity!
return users[usernameHash].creationDate != 0;
} }
/** /**
@ -128,12 +136,16 @@ contract DTwitter {
bytes32 usernameHash = owners[msg.sender]; bytes32 usernameHash = owners[msg.sender];
// get our user // get our user
User storage user = users[usernameHash];
// get our new tweet index // get our new tweet index
uint tweetIndex = user.tweets.length++;
// update the user's tweets at the tweet index // update the user's tweets at the tweet index
user.tweets[tweetIndex] = content;
// emit the tweet event and notify the listeners // emit the tweet event and notify the listeners
emit NewTweet(usernameHash, content, now);
} }
} }