Skip to content

Commit

Permalink
Merge pull request #50 from Microarchitecturovisco/RSWW-112-fix-hotel…
Browse files Browse the repository at this point in the history
…s-reservation-deletion

Rsww 112 fix hotels reservation deletion
  • Loading branch information
Danzigerrr authored May 21, 2024
2 parents 9173593 + e082689 commit 4a9fa48
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class Room {
@Id
private UUID id;

@ManyToOne(cascade = CascadeType.ALL)
@ManyToOne()
@JoinColumn(name="hotel_id")
private Hotel hotel;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public class RoomReservation {
@NotNull
private LocalDateTime dateTo;

@ManyToOne(cascade = CascadeType.ALL)
@ManyToOne()
@JoinColumn(name="room_id", nullable=false)
private Room room;

private UUID mainReservationId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ private void apply(RoomReservationCreatedEvent event){
.dateFrom(event.getDateFrom())
.dateTo(event.getDateTo())
.room(room)
.mainReservationId(event.getIdRoomReservation())
.build();
room.getRoomReservations().add(roomReservation);
roomReservationRepository.save(roomReservation);
Expand All @@ -114,25 +115,23 @@ private void apply(RoomReservationCreatedEvent event){

}
private void apply(RoomReservationDeletedEvent event) {
UUID roomReservationId = event.getIdRoomReservation();
List<HotelEvent> roomReservationIds = eventStore.findAll().stream().filter(e -> e instanceof RoomReservationCreatedEvent).toList();

List<HotelEvent> roomReservationIds2 = roomReservationIds.stream().filter(e -> ((RoomReservationCreatedEvent) e).getIdRoomReservation().equals(roomReservationId)).toList();
List<UUID> roomReservationIds3 = roomReservationIds2.stream().map(HotelEvent::getId).toList();


Room room = roomRepository.findById(event.getIdRoom()).orElseThrow(RuntimeException::new);
List<RoomReservation> roomReservations = room.getRoomReservations();

Iterator<RoomReservation> iterator = roomReservations.iterator();
while (iterator.hasNext()) {
RoomReservation roomReservation = iterator.next();
if (roomReservationIds3.contains(roomReservation.getId())) {
iterator.remove();
roomReservationRepository.deleteById(roomReservation.getId());
roomRepository.save(room);
break;
UUID roomId = event.getIdRoom();
UUID reservationId = event.getIdRoomReservation();

// Find the room
Room room = roomRepository.findById(roomId).orElse(null);
if (room != null) {
List<RoomReservation> roomReservations = room.getRoomReservations();
Iterator<RoomReservation> iterator = roomReservations.iterator();
while (iterator.hasNext()) {
RoomReservation reservation = iterator.next();
if (reservation.getMainReservationId().equals(reservationId)) {
iterator.remove();
roomReservationRepository.delete(reservation);
break; // Exit the loop after deleting the reservation
}
}
roomRepository.save(room); // Save the room to persist changes
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ public void consumeMessageCreateReservation(String reservationRequestJson) {
reservationRequest.getUserId(),
reservationRequest.getId()
);
System.out.println("Reservation created successfully: " + reservation.getId());
System.out.println("Reservation in Reservation module created successfully: " + reservation.getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public String consumeMessageFromQueueCheckTransportAvailability(String requestDt
CheckTransportAvailabilityResponseDto response = CheckTransportAvailabilityResponseDto.builder()
.ifAvailable(false)
.build();
System.out.println("Transport ifAvailable:" + response.isIfAvailable());
System.out.println("Transport available:" + response.isIfAvailable());
String responseJson = JsonConverter.convertToJsonWithLocalDateTime(response);
return responseJson;
}
Expand All @@ -141,7 +141,7 @@ public String consumeMessageFromQueueCheckTransportAvailability(String requestDt
.ifAvailable(ifAvailable)
.build();

System.out.println("Transport ifAvailable:" + response.isIfAvailable());
System.out.println("Transport available:" + response.isIfAvailable());
String responseJson = JsonConverter.convertToJsonWithLocalDateTime(response);

return responseJson;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public void createReservation(CreateTransportReservationCommand command) {
.idTransport(command.getTransportReservationDto().getIdTransport())
.build();

// System.out.println("transportReservationCreatedEvent: " + transportReservationCreatedEvent);

transportEventStore.save(transportReservationCreatedEvent);
eventSourcingHandler.project(List.of(transportReservationCreatedEvent));
Expand All @@ -53,6 +52,8 @@ public void deleteReservation(DeleteTransportReservationCommand command) {
.idTransport(command.getTransportId())
.build();

System.out.println("TransportReservationDeletedEvent: " + reservationDeletedEvent);

transportEventStore.save(reservationDeletedEvent);
eventSourcingHandler.project(List.of(reservationDeletedEvent));
}
Expand Down

0 comments on commit 4a9fa48

Please sign in to comment.