...

/

Solution: Book Loan System

Solution: Book Loan System

Here's the solution to the book loan system challenge.

Solution

Let’s discuss the solution for implementing a book loan system.

Update the Book class

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

Press + to interact
package com.smartdiscover.entity;
import jakarta.persistence.*;
import lombok.Data;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
@Data
@Entity
@EntityListeners(AuditingEntityListener.class)
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String summary;
private Boolean available;
@CreatedBy
private String createdBy;
@CreatedDate
private Date createdDate;
@LastModifiedBy
private String lastModifiedBy;
@LastModifiedDate
private Date lastModifiedDate;
@ManyToMany
@JoinTable(
name = "book_authors",
joinColumns = @JoinColumn(name = "book_id"),
inverseJoinColumns = @JoinColumn(name = "author_id"))
private List<Author> authors;
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", summary='" + summary + '\'' +
", authors='" + (null != authors && authors.size() > 0 ? (authors.stream().map(i -> i.getFullName()).collect(Collectors.toList())) : "[]") + '\'' +
", createdBy='" + createdBy + '\'' +
", createdDate='" + createdDate + '\'' +
", lastModifiedBy='" + lastModifiedBy + '\'' +
", lastModifiedDate='" + lastModifiedDate + '\'' +
'}';
}
}

Update the BookRepository interface

Then, we add the findByNameAndAvailableIsNullOrAvailableIsTrue method in the BookRepository interface to search a book by matching the name and its availability as Null or True.

Press + to interact
package com.smartdiscover.repository;
import com.smartdiscover.entity.Book;
import org.springframework.data.jpa.repository.JpaRepository;
public interface BookRepository extends JpaRepository<Book, Long> {
Book findByNameAndAvailableIsNullOrAvailableIsTrue(String name);
Book findFirstBySummaryIsLikeOrderByNameAsc(String summary);
}

Create the

...