Last active
April 18, 2024 14:08
-
-
Save igalshilman/d9169fe45e8e8ae54d93d36c3c6dd3b3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type TodoItem = { | |
title: string, | |
completed: boolean, | |
} | |
export default restate.object({ | |
name : "todo", | |
handlers: { | |
add: async (ctx: ObjectContext, item: TodoItem) => { | |
const items = await ctx.get("items") ?? []; | |
items.push(item); | |
ctx.set("items", items); | |
return { id: items.length - 1} | |
}, | |
all: async(ctx: ObjectContext) => { return await ctx.get("items") ?? [] }, | |
complete: async(ctx: ObjectContext, id: number) => { | |
const items = await ctx.get("items") ?? []; | |
const item = items[i]; | |
item.completed = true; | |
ctx.set("items", items); | |
return item; | |
} | |
} | |
}); | |
/// $ curl myapp/todo/bob/add --json '{ "title" : "Learn restate" }'; | |
/// { "id" : 0 } | |
/// curl myapp/todo/bob/all | |
/// [ { "title" : "Learn restate", "completed" : false } ] | |
/// curl myapp/todo/bob/complete --json '{ "id" : 0 }' | |
/// { "title" : "Learn restate" , "completed" : true } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment