Skip to content

Commit 0d9e85e

Browse files
committed
Set in the data modelling and examples, that broadcast should be done after transaction commit
1 parent 4a3960d commit 0d9e85e

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

guides/data_modelling/cross_context_boundaries.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -551,12 +551,19 @@ Head back over to your shopping cart context in `lib/hello/shopping_cart.ex` and
551551
|> Ecto.Changeset.cast_assoc(:items, with: &CartItem.changeset/2)
552552

553553
Repo.transact(fn ->
554-
with {:ok, cart} <- Repo.update(changeset) do
555-
Repo.delete_all(from(i in CartItem, where: i.cart_id == ^cart.id and i.quantity == 0))
556-
broadcast_cart(scope, {:updated, cart})
554+
with {:ok, cart} <- Repo.update(changeset),
555+
{_count, _cart_items} = Repo.delete_all(from(i in CartItem, where: i.cart_id == ^cart.id and i.quantity == 0)) do
557556
{:ok, cart}
558557
end
559558
end)
559+
|> case do
560+
{:ok, cart} ->
561+
broadcast_cart(scope, {:updated, cart})
562+
{:ok, cart}
563+
564+
{:error, reason} ->
565+
{:error, reason}
566+
end
560567
end
561568
```
562569

guides/data_modelling/more_examples.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ From our requirements alone, we can start to see why a generic `create_order` fu
202202
alias Hello.Orders.LineItem
203203
alias Hello.ShoppingCart
204204

205-
206205
def complete_order(%Scope{} = scope, %ShoppingCart.Cart{} = cart) do
207206
true = cart.user_id == scope.user.id
208207

@@ -225,10 +224,17 @@ From our requirements alone, we can start to see why a generic `create_order` fu
225224
Repo.transact(fn ->
226225
with {:ok, order} <- Repo.insert(order_changeset),
227226
{:ok, _cart} <- ShoppingCart.prune_cart_items(scope, cart) do
228-
broadcast(scope, {:created, order})
229227
{:ok, order}
230228
end
231229
end)
230+
|> case do
231+
{:ok, order} ->
232+
broadcast_order(scope, {:created, order})
233+
{:ok, order}
234+
235+
{:error, reason} ->
236+
{:error, reason}
237+
end
232238
end
233239
```
234240

0 commit comments

Comments
 (0)