Creating A Simple Blog Engine With Solidity
Published: 2022-10-09 05:49:56

---
title: Creating A Simple Blog Engine With Solidity
published: true
description: 
tags: 
# cover_image: https://direct_url_to_image.jpg
# Use a ratio of 100:42 for best results.
---

Blockchain has been one of the top buzzwords in the world lately, and Ethereum is one of the top Blockchains,  which uses Solidity for building DAPPS (decentralized apps) or smart contracts. Today I will be showing you how easy it is to develop a DAPPS. So excited about this.   
A quick overview of what we will be doing. 
   - Create Post
   - Publish Post
   - List Post 
   - Get Post.

[Remix](https://remix.ethereum.org/) is an online solidity editor which you can use in writing and deploying your smart contract. we are going to be using it today.
 Go to remix and you should have something like this below.
![remix dashboad](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5esss31je897gdt2aqef.png)
Click the contract directory and create a file ``Blog.sol``
Add this snippet to your file.
```solidity
// SPDX-License-Identifier: unlicensed
pragma solidity ^0.8.0;

contract Blog {}
```
what is going on up there, what is SPDX License Identifier?
``
SPDX License Identifiers can be used to indicate relevant license information at any level, from package to the source code file level
``
``pragma solidity ^0.8.0 `` tells the EVM (Ethereum Virtual Machine) what version of solidity we are using. To check everything is working as expected, go to solidity compiler and click the blue button ``compile Blog.sol`` it should work fine.
  Next, let's define the structure of what a Post should look like.
```solidity
     uint256 _id = 1;

    struct Post{
       uint256 id;
       string  title;
       address owner;
       string body;
       bool isPublished;
       uint256 createdAt;
       uint256 publishedAt;
    }

    mapping(uint256 => Post) posts;
```
Here we have a variable _id which serves as a counter, and a struct which is used to define a new data type. Mapping is very similar to objects in Javascript,  which is just a key-value pair. 

### Creating A Post
To create a post, add this snippet below
```solidity
    function createPost(string memory _title, string memory _body, bool _isPublished) public {
        Post storage newPost = post[_id];
        newPost.id = _id;
        newPost.title = _title;
        newPost.owner = msg.sender;
        newPost.body = _body;
        newPost.isPublished = _isPublished;
        newPost.createdAt = block.timestamp;
        
        if (_isPublished){
            newPost.publishedAt = block.timestamp;
        }
        _id++;
    }
```
So we created a publicly available method. ``msg.sender`` this is a global variable accessible on the blockchain to get the user interacting with the contract at the moment. In solidity, ``block.timestamp`` gives the current time.
 Note `` memory `` this is used to hold temporary values. It is erased between (external) function calls and is cheaper to use. 

### Get Post
To get a single post we are going to find it by using the ``id`` of the post as shown below.
```solidity
 
    function getPost(uint256  id) public view  returns ( Post memory) {
        return post[id];
    }
```
The above function has visibility of public and ``view`` tells the EVM no state was modifier and is a getter method that returns a ``Post``.

### List Posts
We are going to return a list of all the posts.
```solidity
function listPost() public view returns (Post [] memory){
        Post[] memory posts = new Post[](_id);
        for (uint i = 0; i < _id; i++) {
            posts[i] = post[_id];
        }
        return posts;
    }
```
We are just returning a list of posts since we can't return mapping we created a list of the length of the id counter.

### Publish Post
Here we will update the state of a post and add the published date
```solidity
   
    function publishPost(uint256 id, bool isPublished) public  returns (Post memory){
        // error handling
        require(posts[id].owner == msg.sender, "Not allowed");
        posts[id].isPublished = isPublished;
        posts[id].publishedAt = block.timestamp;
        return posts[id];
    }
```
You should have something like this now 
```solidity
// SPDX-License-Identifier: unlicensed
pragma solidity ^0.8.0;

contract Blog {
    uint256 _id;

    struct Post{
       uint256 id;
       string  title;
       address owner;
       string body;
       bool isPublished;
       uint256 createdAt;
       uint256 publishedAt;
    }

    mapping(uint256 => Post) posts;


    function createPost(string memory _title, string memory _body, bool _isPublished) public {
        Post storage newPost = posts[_id];
        newPost.id = _id;
        newPost.title = _title;
        newPost.owner = msg.sender;
        newPost.body = _body;
        newPost.isPublished = _isPublished;
        newPost.createdAt = block.timestamp;
        
        if (_isPublished){
            newPost.publishedAt = block.timestamp;
        }
        _id++;
    }

    function getPost(uint256  id) public view  returns ( Post memory) {
        return posts[id];
    }

    function listPost() public view returns (Post [] memory){
        Post[] memory result = new Post[](_id);
        for (uint i = 0; i < _id; i++) {
            result[i] = posts[_id];
        }
        return result;
    }

    function publishPost(uint256 id, bool isPublished) public  returns (Post memory){
        // error handling
        require(posts[id].owner == msg.sender, "Not allowed");
        posts[id].isPublished = isPublished;
        posts[id].publishedAt = block.timestamp;
        return posts[id];
    }
}
```
Now to test your smart contract go to deploy and smart contract tab and deploy it so you can test. If you hung on till the end you did it, congrats.