Skip to content

Commit

Permalink
Use handle_continue instead
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamrhay committed Jan 4, 2024
1 parent 2f08875 commit 45497e2
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions apps/cqrs_booking/src/cqrs_booking.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

-behaviour(gen_server).

-export([ init/1 , handle_call/3 , handle_cast/2 , handle_info/2 ]).
-export([ init/1 , handle_call/3 , handle_cast/2 , handle_info/2, handle_continue/2 ]).

-type state() :: #{}.

Expand All @@ -29,8 +29,7 @@ handle_call({book_room, Cmd}, _From, #{available_rooms:=AvailableRooms} = State)
AvailableRoomsForHotel = maps:get(Hotel, AvailableRooms),
AvailableRoomsForDay = maps:get(CheckIn, AvailableRoomsForHotel),
[_RoomInfo] = lists:filter(fun(R) -> maps:get(id, R) == Room end, AvailableRoomsForDay),
self() ! {new_booking, Cmd},
{reply, ok, State};
{reply, ok, State, {continue, {new_booking, Cmd}}};
handle_call({get_bookings, Client}, _From, #{bookings:=Bookings} = State) ->
BookingsForClient = maps:get(Client, Bookings),
{reply, {ok, BookingsForClient}, State};
Expand All @@ -43,7 +42,11 @@ handle_cast(_Request, State) ->
{noreply, State}.

-spec handle_info(any(), state()) -> {noreply, state()}.
handle_info({new_booking, {Client, _Hotel, _Room, _CheckIn, _CheckOut} = Cmd}, #{bookings:=Bookings} = State) ->
handle_info(_Request, State) ->
{noreply, State}.

-spec handle_continue(term(), state()) -> {noreply, state()}.
handle_continue({new_booking, {Client, _Hotel, _Room, _CheckIn, _CheckOut} = Cmd}, #{bookings:=Bookings} = State) ->
NewBookings = case maps:is_key(Client, Bookings) of
true ->
BookingsForClient = maps:get(Client, Bookings),
Expand All @@ -52,5 +55,5 @@ handle_info({new_booking, {Client, _Hotel, _Room, _CheckIn, _CheckOut} = Cmd}, #
maps:put(Client, [Cmd], Bookings)
end,
{noreply, State#{bookings:=NewBookings}};
handle_info(_Request, State) ->
handle_continue(_Continue, State) ->
{noreply, State}.

0 comments on commit 45497e2

Please sign in to comment.