...

/

Solution: Reservation Queue

Solution: Reservation Queue

Here's the solution to the reservation queue challenge.

Solution

Let’s discuss the solution for implementing a book reservation queue.

Update the Book class

First, we add the Boolean available property to the Book class.

Press + to interact
package com.smartdiscover.model;
import lombok.Data;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.data.redis.core.index.Indexed;
import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;
@RedisHash("Book")
@Data
public class Book implements Serializable {
private String id;
@Indexed
private String name;
private String summary;
private Boolean available;
private List<Author> authors;
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", summary='" + summary + '\'' +
((null != authors) ? ", authors=" + authors.stream().map(i -> i.getFullName()).collect(Collectors.toList()) + '\'' : "") +
'}';
}
}

Create the User class

Then, we create the User class in the com.smartdiscover.model package.

Press + to interact
package com.smartdiscover.model;
import lombok.Data;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.data.redis.core.index.Indexed;
@RedisHash("User")
@Data
public class User {
private String id;
@Indexed
private String firstName;
@Indexed
private String lastName;
}

Here’s an explanation of the code:

  • Lines 7–9: We add annotations like @Data and @RedisHash to transform a class into a Redis hash.

  • Line 11: We add the id identifier.

  • Lines 13 and 14: We add the firstName property with the @Indexed annotation to let Redis index the property values.

  • Lines 16 and 17: We add the lastName property with the @Indexed annotation. ... ...