Skip to content

Commit

Permalink
Added instructions for "Implementing Custom Business Logic" in blog_e…
Browse files Browse the repository at this point in the history
…xample README. (#405)
  • Loading branch information
wubuku authored Jul 4, 2023
1 parent 54d7678 commit 3546383
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 74 deletions.
34 changes: 34 additions & 0 deletions examples/blog_example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,40 @@ Now open them and remove those redundant `use` statements. If your IDE has some

Then use `rooch move build` command to recompile Move project. Now there should be no warning messages.

### Implementing Custom Business Logic (when necessary)

In this example, the `MOVE_CRUD_IT` preprocessor already generates the full CRUD methods for us. If CRUD is all the business logic you need, then you don't have to write another line of code.

It is possible that you feel that the default generated CRUD logic does not meet your needs, for example, you may want to add comment without passing the `Owner` argument to `entry fun add_comment` and directly use the sender account address as the owner, then this requirement can currently be satisfied as follows.

First, define a custom method in the model file like this:

```yaml
aggregates:
Article:
# ...
methods:
AddComment:
event:
name: CommentAdded
properties:
Owner:
type: address
parameters:
CommentSeqId:
type: u64
Commenter:
type: String
Body:
type: String
```

Note that the `Owner` parameter is no longer present in the method parameters above.

Then, delete `article_add_comment_logic.move`, run the dddappp tool again. (Note that since the tool does not overwrite the already existing `*_logic.move` file by default, you will need to delete it manually.)

Open the regenerated `article_add_comment_logic.move` file, find the `verify` function, fill the function body with the business logic code you want.

## Test Application

### Run Rooch Server and Publish Contracts
Expand Down
35 changes: 35 additions & 0 deletions examples/blog_example/README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,41 @@ rooch move build --named-addresses rooch_examples={ACCOUNT_ADDRESS}

然后使用 `rooch move build` 命令重新编译 Move 项目,现在应该没有警告信息了。


### 实现自定义业务逻辑(在必要时)

在这个例子中,`MOVE_CRUD_IT` 预处理器已经为我们生成了完整的 CRUD 方法。如果 CRUD 是你需要的所有业务逻辑,那么你不需要再写一行代码。

有可能你觉得默认生成的 CRUD 逻辑不符合你的需求,比如,你可能想要添加评论时不需要传递 `Owner` 参数给 `entry fun add_comment`,而是直接使用发送者的账户地址作为 Owner,那么这个需求目前可以这样满足:

首先,在模型文件中像这样自定义一个方法:

```yaml
aggregates:
Article:
# ...
methods:
AddComment:
event:
name: CommentAdded
properties:
Owner:
type: address
parameters:
CommentSeqId:
type: u64
Commenter:
type: String
Body:
type: String
```

注意,上面的方法参数列表中已经没有 `Owner` 参数。

然后,删除 `article_add_comment_logic.move` 文件,再次运行 dddappp 工具。(注意,因为工具默认不会覆盖已经存在的 `*_logic.move` 文件,所以你需要手动删除它。)

打开重新生成的 `article_add_comment_logic.move` 文件中,找到 `verify` 函数,在函数体中填充你想要的业务逻辑代码。

## 测试应用

### 运行 Rooch Server 以及发布合约
Expand Down
13 changes: 13 additions & 0 deletions examples/blog_example/dddml/blog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,16 @@ aggregates:
Move:
"assert!(std::signer::address_of(account) == comment::owner(comment), 111);"

AddComment:
event:
name: CommentAdded
properties:
Owner:
type: address
parameters:
CommentSeqId:
type: u64
Commenter:
type: String
Body:
type: String
90 changes: 45 additions & 45 deletions examples/blog_example/sources/article.move
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ module rooch_examples::article {
friend rooch_examples::article_delete_logic;
friend rooch_examples::article_update_comment_logic;
friend rooch_examples::article_remove_comment_logic;
friend rooch_examples::article_create_logic;
friend rooch_examples::article_add_comment_logic;
friend rooch_examples::article_create_logic;
friend rooch_examples::article_aggregate;

const EID_DATA_TOO_LONG: u64 = 102;
Expand Down Expand Up @@ -253,46 +253,6 @@ module rooch_examples::article {
}
}

struct ArticleCreated has key {
id: option::Option<ObjectID>,
title: String,
body: String,
owner: address,
}

public fun article_created_id(article_created: &ArticleCreated): option::Option<ObjectID> {
article_created.id
}

public(friend) fun set_article_created_id(article_created: &mut ArticleCreated, id: ObjectID) {
article_created.id = option::some(id);
}

public fun article_created_title(article_created: &ArticleCreated): String {
article_created.title
}

public fun article_created_body(article_created: &ArticleCreated): String {
article_created.body
}

public fun article_created_owner(article_created: &ArticleCreated): address {
article_created.owner
}

public(friend) fun new_article_created(
title: String,
body: String,
owner: address,
): ArticleCreated {
ArticleCreated {
id: option::none(),
title,
body,
owner,
}
}

struct CommentAdded has key {
id: ObjectID,
version: u64,
Expand Down Expand Up @@ -339,6 +299,46 @@ module rooch_examples::article {
}
}

struct ArticleCreated has key {
id: option::Option<ObjectID>,
title: String,
body: String,
owner: address,
}

public fun article_created_id(article_created: &ArticleCreated): option::Option<ObjectID> {
article_created.id
}

public(friend) fun set_article_created_id(article_created: &mut ArticleCreated, id: ObjectID) {
article_created.id = option::some(id);
}

public fun article_created_title(article_created: &ArticleCreated): String {
article_created.title
}

public fun article_created_body(article_created: &ArticleCreated): String {
article_created.body
}

public fun article_created_owner(article_created: &ArticleCreated): address {
article_created.owner
}

public(friend) fun new_article_created(
title: String,
body: String,
owner: address,
): ArticleCreated {
ArticleCreated {
id: option::none(),
title,
body,
owner,
}
}


public(friend) fun create_article(
storage_ctx: &mut StorageContext,
Expand Down Expand Up @@ -421,12 +421,12 @@ module rooch_examples::article {
event::emit_event(storage_ctx, comment_removed);
}

public(friend) fun emit_article_created(storage_ctx: &mut StorageContext, article_created: ArticleCreated) {
event::emit_event(storage_ctx, article_created);
}

public(friend) fun emit_comment_added(storage_ctx: &mut StorageContext, comment_added: CommentAdded) {
event::emit_event(storage_ctx, comment_added);
}

public(friend) fun emit_article_created(storage_ctx: &mut StorageContext, article_created: ArticleCreated) {
event::emit_event(storage_ctx, article_created);
}

}
4 changes: 1 addition & 3 deletions examples/blog_example/sources/article_add_comment_logic.move
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@ module rooch_examples::article_add_comment_logic {
comment_seq_id: u64,
commenter: String,
body: String,
owner: address,
article_obj: &Object<article::Article>,
): article::CommentAdded {
let _ = storage_ctx;
let _ = account;
article::new_comment_added(
article_obj,
comment_seq_id,
commenter,
body,
owner,
std::signer::address_of(account),
)
}

Expand Down
48 changes: 23 additions & 25 deletions examples/blog_example/sources/article_aggregate.move
Original file line number Diff line number Diff line change
Expand Up @@ -115,56 +115,54 @@ module rooch_examples::article_aggregate {
}


public entry fun create(
public entry fun add_comment(
storage_ctx: &mut StorageContext,
account: &signer,
title: String,
id: ObjectID,
comment_seq_id: u64,
commenter: String,
body: String,
owner: address,
) {
let article_created = article_create_logic::verify(
let article_obj = article::remove_article(storage_ctx, id);
let comment_added = article_add_comment_logic::verify(
storage_ctx,
account,
title,
comment_seq_id,
commenter,
body,
owner,
&article_obj,
);
let article_obj = article_create_logic::mutate(
let updated_article_obj = article_add_comment_logic::mutate(
storage_ctx,
&article_created,
&comment_added,
article_obj,
);
article::set_article_created_id(&mut article_created, article::id(&article_obj));
article::add_article(storage_ctx, article_obj);
article::emit_article_created(storage_ctx, article_created);
article::update_version_and_add(storage_ctx, updated_article_obj);
article::emit_comment_added(storage_ctx, comment_added);
}


public entry fun add_comment(
public entry fun create(
storage_ctx: &mut StorageContext,
account: &signer,
id: ObjectID,
comment_seq_id: u64,
commenter: String,
title: String,
body: String,
owner: address,
) {
let article_obj = article::remove_article(storage_ctx, id);
let comment_added = article_add_comment_logic::verify(
let article_created = article_create_logic::verify(
storage_ctx,
account,
comment_seq_id,
commenter,
title,
body,
owner,
&article_obj,
);
let updated_article_obj = article_add_comment_logic::mutate(
let article_obj = article_create_logic::mutate(
storage_ctx,
&comment_added,
article_obj,
&article_created,
);
article::update_version_and_add(storage_ctx, updated_article_obj);
article::emit_comment_added(storage_ctx, comment_added);
article::set_article_created_id(&mut article_created, article::id(&article_obj));
article::add_article(storage_ctx, article_obj);
article::emit_article_created(storage_ctx, article_created);
}

}
2 changes: 1 addition & 1 deletion examples/blog_example/sources/comment.move
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ module rooch_examples::comment {
friend rooch_examples::article_delete_logic;
friend rooch_examples::article_update_comment_logic;
friend rooch_examples::article_remove_comment_logic;
friend rooch_examples::article_create_logic;
friend rooch_examples::article_add_comment_logic;
friend rooch_examples::article_create_logic;
friend rooch_examples::article;

const EID_DATA_TOO_LONG: u64 = 102;
Expand Down

0 comments on commit 3546383

Please sign in to comment.