Skip to content

Instantly share code, notes, and snippets.

@ptisserand
Last active June 10, 2022 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ptisserand/61e857e2babf178d295763af161df3fa to your computer and use it in GitHub Desktop.
Save ptisserand/61e857e2babf178d295763af161df3fa to your computer and use it in GitHub Desktop.
blog post
import HashMap "mo:base/HashMap";
import Iter "mo:base/Iter";
import Nat "mo:base/Nat";
import Hash "mo:base/Hash";
import Principal "mo:base/Principal";
import Result "mo:base/Result";
import Text "mo:base/Text";
import Time "mo:base/Time";
import TrieMap "mo:base/TrieMap";
import TrieSet "mo:base/TrieSet";
import Types "./types";
actor class() {
type Post = Types.Post;
type PostExt = Types.PostExt;
// counter to have unique ID for post
private stable var id_count = 0;
private stable var stable_posts: [(Nat, Post)] = [];
private let posts = TrieMap.fromEntries<Nat, Post>(stable_posts.vals(), Nat.equal, Hash.hash);
public shared(msg) func makePost(title: Text, tagline: Text, story: Text, image: ?Blob): async Result.Result<Nat, Text> {
let caller = msg.caller;
let time = Time.now();
let id = id_count;
let post: Post = {
id = id;
author = caller;
var title = title;
var tagline = tagline;
var story = story;
var image = image;
time = time;
var likes = TrieSet.empty<Principal>();
};
posts.put(id, post);
id_count +=1;
#ok(id);
};
public shared(msg) func likePost(postId: Nat): async Result.Result<Bool, Text> {
let caller = msg.caller;
let post = switch(posts.get(postId)) {
case (null) {
return #err("post doesn't exist");
};
case (?p) {
p
};
};
if (TrieSet.mem(post.likes, caller, Principal.hash(caller), Principal.equal)) {
// already liked this article, unlike
post.likes := TrieSet.delete(post.likes, caller, Principal.hash(caller), Principal.equal);
#ok(false)
} else {
// like this article
post.likes := TrieSet.put(post.likes, caller, Principal.hash(caller), Principal.equal);
#ok(true)
};
};
public query func getAllPosts(): async [PostExt] {
let postExts = Iter.map(posts.vals(), Types.postToExt);
Iter.toArray(postExts);
};
}
import Iter "mo:base/Iter";
import Nat "mo:base/Nat";
import Result "mo:base/Result";
import Text "mo:base/Text";
import Time "mo:base/Time";
import TrieSet "mo:base/TrieSet";
module {
public type Post = {
id: Nat;
author: Principal;
var title: Text;
var tagline: Text;
var story: Text;
var image: ?Blob;
time: Time.Time; // creation time
var likes: TrieSet.Set<Principal>;
};
public type PostExt = {
id: Nat;
author: Principal;
title: Text;
tagline: Text;
story: Text;
image: ?Blob;
time: Time.Time;
likes: [Principal];
};
public func postToExt(post: Post) : PostExt {
{
id = post.id;
author = post.author;
title = post.title;
tagline = post.tagline;
story = post.story;
image = post.image;
time = post.time;
likes = TrieSet.toArray(post.likes);
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment