Skip to content

Instantly share code, notes, and snippets.

@kislayverma
Created April 10, 2021 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kislayverma/5497931c93250768518d2d62a47d5791 to your computer and use it in GitHub Desktop.
Save kislayverma/5497931c93250768518d2d62a47d5791 to your computer and use it in GitHub Desktop.
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