-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblog-smart-contract.sol
More file actions
151 lines (121 loc) · 3.8 KB
/
Copy pathblog-smart-contract.sol
File metadata and controls
151 lines (121 loc) · 3.8 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//SPDX-License-Identifier: Unlicensed
pragma solidity ^0.8.7;
contract Blog {
address public owner;
uint public postCounter;
mapping(uint => address) public authorOf;
mapping(address => uint) public postsOf;
mapping(uint => bool) public postExist;
enum Deactivated { NO, YES }
struct PostStruct {
uint id;
string title;
string description;
address author;
Deactivated deleted;
uint created;
uint updated;
}
PostStruct[] activePosts;
event Action(
uint id,
string actionType,
address indexed executor,
uint timestamp
);
modifier ownerOnly() {
require(msg.sender == owner, "Reserved for owners only");
_;
}
constructor() {
owner = msg.sender;
}
function createPost(
string memory title,
string memory description
) public returns (bool) {
require(bytes(title).length > 0, "Title cannot be empty");
require(bytes(description).length > 0, "Description cannot be empty");
postCounter++;
authorOf[postCounter] = msg.sender;
postsOf[msg.sender]++;
postExist[postCounter] = true;
activePosts.push(
PostStruct(
postCounter,
title,
description,
msg.sender,
Deactivated.NO,
block.timestamp,
block.timestamp
)
);
emit Action(
postCounter,
"POST CREATED",
msg.sender,
block.timestamp
);
return true;
}
function updatePost(
uint id,
string memory title,
string memory description
) public returns (bool) {
require(postExist[id], "Blog post not found");
require(bytes(title).length > 0, "Title cannot be empty");
require(bytes(description).length > 0, "Description cannot be empty");
require(msg.sender == activePosts[id - 1].author, "Reserved for author only");
activePosts[id - 1].title = title;
activePosts[id - 1].description = description;
activePosts[id - 1].updated = block.timestamp;
emit Action(
id,
"POST UPDATED",
msg.sender,
block.timestamp
);
return true;
}
function deletePost(uint id) public returns (bool) {
require(postExist[id], "Blog post not found");
require(msg.sender == activePosts[id - 1].author, "Reserved for author only");
activePosts[id - 1].deleted = Deactivated.YES;
postCounter--;
emit Action(
id,
"POST DELETED",
msg.sender,
block.timestamp
);
return true;
}
function restorePost(uint id) public ownerOnly returns (bool) {
require(postExist[id], "Blog post not found");
activePosts[id - 1].deleted = Deactivated.NO;
postCounter++;
emit Action(
id,
"POST RESTORED",
msg.sender,
block.timestamp
);
return true;
}
function showPost(uint id) public view returns (PostStruct memory) {
return activePosts[id - 1];
}
function getPosts() public view returns (PostStruct[] memory) {
return activePosts;
}
function getActivePosts() public view returns (PostStruct[] memory posts) {
posts = new PostStruct[](postCounter);
for(uint i=0; i < activePosts.length; i++) {
if(activePosts[i].deleted != Deactivated.YES) {
posts[i] = activePosts[i];
}
}
}
}