@Entity
public class ReservationRequest extends AssertionConcern {
code
Assistant
@Id
@GeneratedValue
private UUID requestId;
@Column(nullable = false)
private UUID arrivingUserId;
@Column(nullable = false)
private UUID leavingUserId;
@Enumerated(EnumType.STRING)
@Column(nullable = false)
private RequestStatus status;
@Column(nullable = false)
private LocalDateTime requestedAt;
private LocalDateTime respondedAt;
private String message;
@ManyToOne
private Availability availability;
protected ReservationRequest() {
}
public static ReservationRequest create(UUID arrivingUserId, UUID leavingUserId,
String message, Availability availability) {
return new ReservationRequest(arrivingUserId, leavingUserId, message, availability);
}
private ReservationRequest(UUID arrivingUserId, UUID leavingUserId,
String message, Availability availability) {
this.setArrivingUserId(arrivingUserId);
this.setLeavingUserId(leavingUserId);
this.setStatus(RequestStatus.PENDING);
this.setRequestedAt(LocalDateTime.now());
this.setMessage(message);
this.setAvailability(availability);
}
// Business methods
public void respond(UUID leavingUserId, boolean accepted) {
assertArgumentTrue(this.leavingUserId.equals(leavingUserId), "You do not have permission to respond to this request");
assertArgumentTrue(this.status.equals(RequestStatus.PENDING), "Request has already been responded to");
this.setRespondedAt(LocalDateTime.now());
this.setStatus(accepted ? RequestStatus.ACCEPTED : RequestStatus.REJECTED);
}
public void cancel() {
assertArgumentTrue(this.status == RequestStatus.PENDING, "Only pending requests can be canceled.");
this.setStatus(RequestStatus.CANCELED);
this.setRespondedAt(LocalDateTime.now());
}
// Getters and Setters with validation
public UUID getRequestId() {
return requestId;
}
private void setAvailability(Availability availability) {
this.availability = availability;
}
public Availability getAvailability() {
return availability;
}
public UUID getArrivingUserId() {
return arrivingUserId;
}
private void setArrivingUserId(UUID arrivingUserId) {
assertArgumentNotNull(arrivingUserId, "ArrivingUserId cannot be null");
this.arrivingUserId = arrivingUserId;
}
public UUID getLeavingUserId() {
return leavingUserId;
}
private void setLeavingUserId(UUID leavingUserId) {
assertArgumentNotNull(leavingUserId, "LeavingUserId cannot be null");
this.leavingUserId = leavingUserId;
}
public RequestStatus getStatus() {
return status;
}
private void setStatus(RequestStatus status) {
assertArgumentNotNull(status, "Status cannot be null");
this.status = status;
}
public LocalDateTime getRequestedAt() {
return requestedAt;
}
private void setRequestedAt(LocalDateTime requestedAt) {
this.requestedAt = requestedAt;
}
public LocalDateTime getRespondedAt() {
return respondedAt;
}
private void setRespondedAt(LocalDateTime respondedAt) {
this.respondedAt = respondedAt;
}
public String getMessage() {
return message;
}
private void setMessage(String message) {
this.message = message;
}
}
Provide documentation
Documentation for the ReservationRequest Class
Overview
The ReservationRequest class represents a reservation request in the parking exchange system. It models the interaction between an arriving user who wants to reserve a parking spot and a leaving user who is offering the spot. The class includes details such as the users involved, the status of the request, timestamps of when the request was made and responded to, any messages included with the request, and the associated parking availability.
This class is an entity managed by JPA (Java Persistence API), making it persistable in a relational database.
Description: Initializes a new reservation request with the provided details and sets the status to PENDING.
Static Factory Method
create
java
public static ReservationRequest create(UUID arrivingUserId, UUID leavingUserId,
String message, Availability availability) {
return new ReservationRequest(arrivingUserId, leavingUserId, message, availability);
}
Parameters:
arrivingUserId: UUID of the arriving user.
leavingUserId: UUID of the leaving user.
message: Optional message for the request.
availability: Associated parking availability.
Returns: A new instance of ReservationRequest.
Description: Creates a new reservation request.
Business Methods
respond
java
public void respond(UUID leavingUserId, boolean accepted) {
assertArgumentTrue(this.leavingUserId.equals(leavingUserId),
"You do not have permission to respond to this request");
assertArgumentTrue(this.status.equals(RequestStatus.PENDING),
"Request has already been responded to");
this.setRespondedAt(LocalDateTime.now());
this.setStatus(accepted ? RequestStatus.ACCEPTED : RequestStatus.REJECTED);
}
Parameters:
leavingUserId: UUID of the leaving user responding to the request.
accepted: true if the request is accepted; false otherwise.
Description: Allows the leaving user to respond to the reservation request, updating the status to accepted or rejected and recording the response time.
cancel
java
public void cancel() {
assertArgumentTrue(this.status == RequestStatus.PENDING,
"Only pending requests can be canceled.");
this.setStatus(RequestStatus.CANCELED);
this.setRespondedAt(LocalDateTime.now());
}
Description: Allows the arriving user to cancel the reservation request if it's still pending, updating the status to canceled and recording the cancellation time.
Accessors (Getters and Setters)
getRequestId
java
public UUID getRequestId() {
return requestId;
}
Returns: The unique identifier for the reservation request.
UUID leavingUserId = request.getLeavingUserId();
// The leaving user accepts the request
request.respond(leavingUserId, true);
Canceling a Reservation Request
java
// The arriving user cancels the request
request.cancel();
Important Notes
Access Control: Only the leaving user can respond to the request, and only if the request is pending.
Status Management: Once a request is responded to or canceled, it cannot be modified further.
Timestamp Recording: The requestedAt and respondedAt timestamps record when actions occur, using the system's current time.
JPA Annotations
@Entity: Marks the class as a JPA entity.
@Id: Specifies the primary key.
@GeneratedValue: Indicates that the primary key value is automatically generated.
@Column(nullable = false): Specifies that the column cannot be NULL in the database.
@Enumerated(EnumType.STRING): Maps the enum to its string representation in the database.
@ManyToOne: Defines a many-to-one relationship with another entity (Availability).
Conclusion
The ReservationRequest class effectively models a reservation request in the parking exchange system, encapsulating all necessary data and providing methods to manage the request lifecycle. It uses validation to maintain a consistent state and utilizes JPA annotations for persistence. The class design ensures that only authorized actions can be performed, adhering to business rules.