Skip to content

Commit

Permalink
Split cmd validation from processing
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamrhay committed Jan 4, 2024
1 parent c024ca4 commit 2f08875
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 38 deletions.
21 changes: 12 additions & 9 deletions apps/cqrs_booking/src/cqrs_booking.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,13 @@ init(unused) ->

-spec handle_call(any(), {pid(), any()}, state()) ->
{reply, any(), state()} | {noreply, state()}.
handle_call({book_room, Cmd}, _From, #{available_rooms:=AvailableRooms, bookings:=Bookings} = State) ->
handle_call({book_room, Cmd}, _From, #{available_rooms:=AvailableRooms} = State) ->
{Client, Hotel, Room, CheckIn, _CheckOut} = Cmd,
AvailableRoomsForHotel = maps:get(Hotel, AvailableRooms),
AvailableRoomsForDay = maps:get(CheckIn, AvailableRoomsForHotel),
[_RoomInfo] = lists:filter(fun(R) -> maps:get(id, R) == Room end, AvailableRoomsForDay),
NewBookings = case maps:is_key(Client, Bookings) of
true ->
BookingsForClient = maps:get(Client, Bookings),
maps:update(Client, [Cmd | BookingsForClient], Bookings);
false ->
maps:put(Client, [Cmd], Bookings)
end,
{reply, ok, State#{bookings:=NewBookings}};
self() ! {new_booking, Cmd},
{reply, ok, State};
handle_call({get_bookings, Client}, _From, #{bookings:=Bookings} = State) ->
BookingsForClient = maps:get(Client, Bookings),
{reply, {ok, BookingsForClient}, State};
Expand All @@ -49,5 +43,14 @@ 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) ->
NewBookings = case maps:is_key(Client, Bookings) of
true ->
BookingsForClient = maps:get(Client, Bookings),
maps:update(Client, [Cmd | BookingsForClient], Bookings);
false ->
maps:put(Client, [Cmd], Bookings)
end,
{noreply, State#{bookings:=NewBookings}};
handle_info(_Request, State) ->
{noreply, State}.
40 changes: 11 additions & 29 deletions apps/cqrs_booking/test/cqrs_booking_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,24 @@ init_bookings_list_is_empty() ->
?_assertEqual(0, length(maps:keys(Bookings))).

handle_call_test_() ->
book_room() ++ get_bookings().

book_room() ->
[
new_booking_should_be_added(),
{setup, fun start/0, fun stop/1, fun new_booking_should_be_added/1},
unavailable_room_cannot_be_booked()
].

new_booking_should_be_added() ->
start() ->
{ok, Pid} = cqrs_booking:start_link(),
Pid.

stop(Pid) ->
gen_server:stop(Pid).

new_booking_should_be_added(Pid) ->
Client = 1,
Cmd = {Client, 2, <<"101">>, {2023, 12, 1}, {2023, 12, 2}},
State = new_state(),
{reply, ok, #{bookings:=Bookings}} = cqrs_booking:handle_call({book_room, Cmd}, self(), State),
BookingsForClient = maps:get(Client, Bookings),
ok = gen_server:call(Pid, {book_room, Cmd}),
{ok, BookingsForClient} = gen_server:call(Pid, {get_bookings, Client}),
[
?_assertEqual(1, length(maps:keys(Bookings))),
?_assertEqual(1, length(BookingsForClient)),
?_assertEqual([Cmd], BookingsForClient)
].
Expand All @@ -42,26 +44,6 @@ unavailable_room_cannot_be_booked() ->
State = new_state(),
?_assertError({badmatch, []}, cqrs_booking:handle_call({book_room, Cmd}, self(), State)).

get_bookings() ->
[
return_all_bookings_for_client()
].

return_all_bookings_for_client() ->
Client = 1,
Cmd = {Client, 2, <<"101">>, {2023, 12, 1}, {2023, 12, 2}},
State1 = new_state(),
#{bookings:=Bookings} = State1,
NewBookings = maps:put(Client, [Cmd], Bookings),
State2 = State1#{bookings:=NewBookings},

{reply, {ok, BookingsForClient}, State2} = cqrs_booking:handle_call({get_bookings, Client}, self(), State2),

[
?_assertEqual(1, length(BookingsForClient)),
?_assertEqual([Cmd], BookingsForClient)
].

new_state() ->
#{
available_rooms => cqrs_booking_hotels:get_available_rooms(),
Expand Down

0 comments on commit 2f08875

Please sign in to comment.