It was your third day at Motoko School, and you were determined to start making some good friends.
Being a big fan of music, you thought organizing a little music concert could be a great way to meet other like-minded students. 🎶
As you wandered the halls during lunchtime, you overheard two students discussing how they wished there was a better way to stay informed about school events and activities. They complained about how they had missed out on attending the previous year's ICP Community Conference because they weren't aware of it in time, and how they were determined not to let that happen again this year. 💪
This sparked an idea in your mind.
What if you could create a digital platform where students share upcoming events and activities. It would be the perfect way to advertise your music concert and help other students stay informed about everything happening on campus. 📅
You quickly shared your idea with the two students and they loved it. They introduced you to a few others who were also interested in the idea, and before you knew it, you had a group of motivated students ready to bring your idea to life. 🚀
You all gathered in the computer lab and began brainstorming the features of your student wall. You wanted it to be more than just a bulletin board, so you came up with the idea to include images and surveys as well. You divided the work and got started coding.
Your task is to develop the code for a student wall, implemented as a canister - a digital platform that will revolutionize how students communicate and stay informed about events, clubs, and activities in the community. Imagine a dynamic and interactive space where students can share their thoughts, ideas, and upcoming events with each other. This wall will be the go-to destination for students looking to get the latest scoop on what's happening in the school.
We define a variant type called Content
that represents the type of content of the messages that can be published on the wall.
public type Content = {
#Text: Text;
#Image: Blob;
#Video: Blob;
};
-
Define a new type called
Message
which consists of three fields:vote
of typeInt
,content
of typeContent
, andcreator
of typePrincipal
. -
Declare a variable named
messageId
to serve as a continuously increasing counter, keeping track of the total number of messages posted. -
Create a variable called
wall
, which is a HashMap designed to store messages. The keys in the wall are of typeNat
and represent message IDs, while the values are of typeMessage
. -
Implement the
writeMessage
function, which accepts a contentc
of typeContent
, creates a new message from the content, adds the message to the wall, and returns the ID of the message.
writeMessage: shared (c : Content) -> async Nat;
- Implement the
getMessage
function, which accepts amessageId
of typeNat
and returns the corresponding message wrapped in anOk
result. If themessageId
is invalid, the function should return an error message wrapped in anErr
result.
getMessage: shared query (messageId: Nat) -> async Result.Result<Message, Text>;
- Implement the
updateMessage
function, which accepts amessageId
of typeNat
and a contentc
of typeContent
. This function updates the content of the corresponding message only if the caller is the creator of the message. If themessageId
is invalid or thecaller
is not the creator, the function should return an error message wrapped in anErr
result. If the update is successful, the function should return a unit value wrapped in anOk
result.
updateMessage: shared (messageId : Nat, c : Content) -> async Result.Result<(), Text>;
- Implement the
deleteMessage
function, which accepts amessageId
of typeNat
, removes the corresponding message from the wall, and returns a unit value wrapped in anOk
result. If themessageId
is invalid, the function should return an error message wrapped in anErr
result.
deleteMessage: shared (messageId : Nat) -> async Result.Result<(), Text>;
- Implement the
upVote
function, which accepts amessageId
of typeNat
, increments the vote count of the corresponding message by one, and returns a unit value wrapped in anOk
result. If themessageId
is invalid, the function should return an error message wrapped in anErr
result.
upVote: shared (messageId : Nat) -> async Result.Result<(), Text>;
- Implement the
downVote
function, which accepts amessageId
of typeNat
, decrements the vote count of the corresponding message by one, and returns a unit value wrapped in anOk
result. If themessageId
is invalid, the function should return an error message wrapped in anErr
result.
downVote: shared (messageId : Nat) -> async Result.Result<(), Text>;
- Implement the query function
getAllMessages
, which returns a list of all messages stored in the wall.
getAllMessages : query () -> async [Message];
- Implement the query function
getAllMessagesRanked
, which returns a list of all messages from the wall, ordered by the number of votes in descending order.
getAllMessagesRanked : query () -> async [Message];
- Deploy the student wall on the Internet Computer.
Your canister should implement the following interface:
actor {
// Add a new message to the wall
writeMessage: shared (c : Content) -> async Nat;
//Get a specific message by ID
getMessage: shared query (messageId : Nat) -> async Result.Result<Message, Text>;
// Update the content for a specific message by ID
updateMessage: shared (messageId : Nat, c : Content) -> async Result.Result<(), Text>;
//Delete a specific message by ID
deleteMessage: shared (messageId : Nat) -> async Result.Result<(), Text>;
// Voting
upVote: shared (messageId : Nat) -> async Result.Result<(), Text>;
downVote: shared (messageId : Nat) -> async Result.Result<(), Text>;
//Get all messages
getAllMessages : query () -> async [Message];
//Get all messages
getAllMessagesRanked : query () -> async [Message];
};