Skip to content

Commit

Permalink
complex type pr fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Sep 30, 2023
1 parent 17f9ac8 commit 8534ee2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,17 @@ export const User = Recursive(() =>
export const Post = Recursive(() =>
Record({
id: text,

author: User,

reactions: Vec(Reaction),

text: text,

thread: Thread
})
);

export const Thread = Record({
id: text,

author: User,

posts: Vec(Post),

title: text
});

Expand Down
8 changes: 1 addition & 7 deletions examples/complex_types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@ import { createThread, getAllThreads } from './threads';
import { createUser, getAllUsers } from './users';

export default Canister({
createPost: update(
[text, text, text, nat32],
Post,
(authorId, text, threadId, joinDepth) => {
return createPost(authorId, text, threadId, joinDepth);
}
),
createPost,

getAllPosts: query([nat32], Vec(Post), (joinDepth) => {
return getAllPosts(joinDepth);
Expand Down
51 changes: 28 additions & 23 deletions examples/complex_types/src/posts.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
import { nat32, Vec } from 'azle';
import { nat32, text, update, Vec } from 'azle';
import { Post } from './candid_types';
import { getReactionFromStateReaction } from './reactions';
import { state, StatePost, StateThread, StateUser } from './state';
import { getThreadFromStateThread } from './threads';
import { getUserFromStateUser } from './users';

export function createPost(
authorId: string,
text: string,
threadId: string,
joinDepth: nat32
): typeof Post {
const id = Object.keys(state.posts).length.toString();
export const createPost = update(
[text, text, text, nat32],
Post,
(authorId, text, threadId, joinDepth) => {
const id = Object.keys(state.posts).length.toString();

const statePost: StatePost = {
id,
authorId,
reactionIds: [],
text,
threadId
};
const updatedStateAuthor = getUpdatedStateAuthor(authorId, statePost.id);
const updatedStateThread = getUpdatedStateThread(threadId, statePost.id);
const statePost: StatePost = {
id,
authorId,
reactionIds: [],
text,
threadId
};
const updatedStateAuthor = getUpdatedStateAuthor(
authorId,
statePost.id
);
const updatedStateThread = getUpdatedStateThread(
threadId,
statePost.id
);

state.posts[id] = statePost;
state.users[authorId] = updatedStateAuthor;
state.threads[threadId] = updatedStateThread;
state.posts[id] = statePost;
state.users[authorId] = updatedStateAuthor;
state.threads[threadId] = updatedStateThread;

const post = getPostFromStatePost(statePost, joinDepth);
const post = getPostFromStatePost(statePost, joinDepth);

return post;
}
return post;
}
);

export function getAllPosts(joinDepth: nat32): (typeof Post)[] {
return Object.values(state.posts).map((statePost) =>
Expand Down

0 comments on commit 8534ee2

Please sign in to comment.