-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessagewall.mo
176 lines (130 loc) · 3.5 KB
/
messagewall.mo
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import Principal "mo:base/Principal";
import Text "mo:base/Text";
import Nat "mo:base/Nat";
import Result "mo:base/Result";
import HashMap "mo:base/HashMap";
import Hash "mo:base/Hash";
import Buffer "mo:base/Buffer";
import Array "mo:base/Array";
import Iter "mo:base/Iter";
import Order "mo:base/Order";
import Int "mo:base/Int";
actor StudentWall {
public type Content = {
#Text: Text;
#Image: Blob;
#Video: Blob;
};
type Message={
vote : Int;
content : Content;
creator : Principal;
};
var messageId : Nat = 0;
var wall= HashMap.HashMap<Nat,Message>(1,Nat.equal, Hash.hash);
public shared ({ caller }) func writeMessage(c : Content) : async Nat {
let current : Nat = messageId;
messageId+=1;
let newMessage : Message = {
content = c;
vote = 0;
creator = caller;
};
wall.put(current, newMessage);
return current;
};
public query func getMessage(messageId : Nat) : async Result.Result<Message, Text> {
let message : ?Message = wall.get(messageId);
switch(message) {
case(null){
#err("No message found");
};
case(?message){
#ok(message);
};
};
};
public shared ({caller})func updateMessage(messageId : Nat, c : Content) :async Result.Result<(),Text>{
let message : ?Message = wall.get(messageId);
switch(message) {
case(null){
#err("No message found");
};
case(?message){
if(message.creator != caller){
#err("You are not the creator of this message");
} else{
let newMessage : Message = {
content = c;
vote = message.vote;
creator = message.creator;
};
wall.put(messageId, newMessage);
#ok(());
};
};
};
};
public func deleteMessage(messageId:Nat):async Result.Result<(),Text>{
let message:?Message=wall.get(messageId);
switch(message){
case(null)
{#err("No message found");};
case(?message){
wall.delete(messageId);
#ok(());
};
};
};
public func upVote(messageId:Nat):async Result.Result<(),Text>{
let message:?Message=wall.get(messageId);
switch(message){
case(null){
#err("No message found");
};
case(?message){
let newMessage:Message={
content=message.content;
vote=message.vote+1;
creator=message.creator;
};
wall.put(messageId,newMessage);
#ok(());
};
};
};
public func downVote(messageId:Nat):async Result.Result<(),Text>{
let message:?Message=wall.get(messageId);
switch(message){
case(null){
#err("No message found");
};
case(?message){
let newMessage:Message={
content=message.content;
vote=message.vote-1;
creator=message.creator;
};
wall.put(messageId,newMessage);
#ok(());
};
};
};
public query func getAllMessages():async [Message]{
let buffer=Buffer.Buffer<Message>(0);
for (i in wall.vals()){
buffer.add(i);
};
return Buffer.toArray(buffer);
};
func sortingfunc(x : Message, y : Message) : Order.Order {
if (x.vote < y.vote) { #less } else if (x.vote== y.vote) { #equal } else {
#greater;
};
};
public query func getAllMessagesRanked() : async [Message] {
let messages = Iter.toArray(wall.vals());
var rankedmessages=Array.sort(messages,sortingfunc);
return rankedmessages;
};
}