Skip to content

Commit

Permalink
Update superheroes to functional runtime syntax
Browse files Browse the repository at this point in the history
Blocked by recursive definitions
  • Loading branch information
dansteren committed Sep 25, 2023
1 parent 9465d4f commit ba87aaf
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 45 deletions.
3 changes: 2 additions & 1 deletion examples/motoko_examples/superheroes/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"root": "src/superheroes",
"ts": "src/superheroes/index.ts",
"candid": "src/superheroes/index.did",
"wasm": ".azle/superheroes/superheroes.wasm.gz",
"wasm": ".azle/superheroes/superheroes.wasm",
"gzip": true,
"declarations": {
"output": "src/declarations",
"node_compatibility": true
Expand Down
87 changes: 43 additions & 44 deletions examples/motoko_examples/superheroes/src/superheroes/index.ts
Original file line number Diff line number Diff line change
@@ -1,84 +1,83 @@
import {
bool,
candid,
nat32,
None,
Opt,
query,
Record,
Recursive,
Service,
Some,
text,
Tuple,
update
} from 'azle';

// Note: This won't be reflected in the candid until
// Note: This won't be reflected in the candid
export type SuperheroId = nat32;
const SuperheroId = nat32;

export type List = [text, Opt<List>];
const List: List = Tuple(text, Opt(List));
const List = Tuple(
text,
Recursive(() => Opt(List))
);

// The type of a superhero.
class Superhero extends Record {
@candid(text)
name: text;

@candid(Opt(List))
superpowers: Opt<List>;
}
const Superhero = Record({
name: text,
superpowers: Opt(List)
});

/**
* High-Level API
* Application State
*/
export default class extends Service {
/**
* Application State
*/

// The next available superhero identifier.
next: SuperheroId = 0;
// The next available superhero identifier.
let next: SuperheroId = 0;

// The superhero data store.
superheroes: Map<SuperheroId, Superhero> = new Map();
// The superhero data store.
let superheroes: Map<SuperheroId, typeof Superhero> = new Map();

/**
* High-Level API
*/
export default Service({
// Create a superhero.
@update([Superhero], SuperheroId)
create(superhero: Superhero): SuperheroId {
let superheroId = this.next;
this.next += 1;
this.superheroes.set(superheroId, superhero);
create: update([Superhero], SuperheroId, (superhero) => {
let superheroId = next;
next += 1;
superheroes.set(superheroId, superhero);

return superheroId;
}
}),

// Read a superhero.
@query([SuperheroId], Opt(Superhero))
read(superheroId: SuperheroId): Opt<Superhero> {
const superheroOrUndefined = this.superheroes.get(superheroId);
read: query([SuperheroId], Opt(Superhero), (superheroId) => {
const superheroOrUndefined = superheroes.get(superheroId);
return superheroOrUndefined ? Some(superheroOrUndefined) : None;
}
}),

// Update a superhero.
@update([SuperheroId, Superhero], bool)
update(superheroId: SuperheroId, superhero: Superhero): bool {
let result = this.superheroes.get(superheroId);
if (result) {
this.superheroes.set(superheroId, superhero);
}
update: update(
[SuperheroId, Superhero],
bool,
(superheroId: SuperheroId, superhero) => {
let result = superheroes.get(superheroId);
if (result) {
superheroes.set(superheroId, superhero);
}

return !!result;
}
return !!result;
}
),

// Delete a superhero.
@update([SuperheroId], bool)
deleteHero(superheroId: SuperheroId): bool {
let result = this.superheroes.get(superheroId);
deleteHero: update([SuperheroId], bool, (superheroId) => {
let result = superheroes.get(superheroId);
if (result) {
this.superheroes.delete(superheroId);
superheroes.delete(superheroId);
}

return !!result;
}
}
})
});

0 comments on commit ba87aaf

Please sign in to comment.