Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AN update API with clearly defined actions on entity
public class Booking {
String uniqueId;
User guest;
User host;
Date bookingTime;
Date confirmationTime;
Date cancellationTime;
Status status; //PENDING, CONFIRMED, CANCELLED_BY_GUEST, CANCELLED_BY_HOST
User lastUpdatedBy;
}
public class BookingUpdateRequest {
AllowedActionOnBooking action;
Date updateTime;
Booking updatedBooking;
}
// Explicitly define the ways of modiying the Booking entity
public enum AllowedActionOnBooking {
GUEST_CANCELLATION,
HOST_CANCELLATION,
CONFIRM,
DATE_CHANGE
}
// In Booking Service
public void updateBooking(BookingUpdateRequest request) {
Booking originalBooking = readFromDB(request.updatedBooking.uniqueId);
switch (request.action) {
case GUEST_CANCELLATION:
handleGuestCancellationRequest(originalBooking, request);
break;
case HOST_CANCELLATION:
handleHostCancellationRequest(originalBooking, request);
break;
case CONFIRM:
handleConfirmationRequest(originalBooking, request);
break;
case DATE_CHANGE:
handleDateChangeRequest(originalBooking, request);
break;
default: throw new Exception("Unhandled action on booking");
}
}
// Or move all these private methods to their own handler simplifying this class further
private void handleGuestCancellationRequest(Booking originalBooking, BookingUpdateRequest request) {
originalBooking.lastUpdatedBy = originalBooking.guest;
originalBooking.cancellationTime = cancelltaionTime;
originalBooking.status = CANCELLED_BY_GUEST;
// Trigger notification to host
// Trigger refund if payment was taken
updateInDB(originalBooking);
}
private void handleHostCancellationRequest(Booking originalBooking, BookingUpdateRequest request) {
originalBooking.lastUpdatedBy = originalBooking.host;
originalBooking.cancellationTime = cancelltaionTime;
originalBooking.status = CANCELLED_BY_HOST;
// Trigger notification to guest
// Trigger refund if payment was taken
updateInDB(originalBooking);
}
private void handleConfirmationRequest(Booking originalBooking, BookingUpdateRequest request) {
// Business logic
}
private void handleDateChangeRequest(Booking originalBooking, BookingUpdateRequest request) {
// Business logic
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment