Dear Diary Example "Blog Contract"


In this post, i will be creating a Blogging Contract meant for Diary Entries and posts of articles or News.
Notice the use of "block.number" in this post as a way to see new blocks as they come in or get changed



contract BlogAresh {
    address public owner; 

    blog[] blogLogs;

    struct blog {
        string title;
        string headline;
        string content;
        string url;
        address owner;
        uint blocknumber;
    }
    uint counter;
    
    function BlogAresh() public {
        owner = msg.sender;
        counter = 1;
        blogLogs.push(blog("BlogAresh:", "My New Blog", "1.0","ethereumacademy.blogspot.com", owner, block.number));
    }
    //anyone can add a post to the blog (multiple writers)
    function addPost(string title, string headline, string content, string link) public {
        blogLogs.push(blog(title, headline, content, link,msg.sender, block.number));
        counter += 1;
    }
    //only the person who created the entry can edit
    function editPost(uint index, string title, string headline, string content,string link) public {
        if (msg.sender != blogLogs[index].owner ) revert(); //can do with a modifier
        blogLogs[index] = blog(title, headline, content, link,msg.sender, block.number);
    }

    function readBlog(uint i) public constant returns(string, string, string,string, uint) {
        blog memory l = blogLogs[i];
        return (l.title, l.headline, l.content, l.url, l.blocknumber);
    }
    
    function countPost() public constant returns (uint){
        return counter;
    }
}

Comments

Popular posts from this blog

Solidity Hash of Structs (Testing hash uniqueness)

Is Making the Crypto Space Legally Compliant Paving the Road to Mass (Blockchain) Adoption?

Parity installation on Ubuntu Virtual Machine