Solution: Implementing Library Analytics
Here’s the solution to the library analytics challenge.
We'll cover the following...
Let’s discuss the solution for implementing library analytics.
Create the BookAnalytics class
First, we create the BookAnalytics class in the com.smartdiscover.model package.
Press + to interact
package com.smartdiscover.model;import lombok.Data;import org.springframework.data.cassandra.core.mapping.Column;import org.springframework.data.cassandra.core.mapping.PrimaryKey;import org.springframework.data.cassandra.core.mapping.Table;import java.util.UUID;@Data@Tablepublic class BookAnalytics {@PrimaryKeyprivate UUID bookId;@Columnprivate String bookName;@Columnprivate long borrowedCount;@Columnprivate long viewedCount;@Columnprivate double averageRating;@Columnprivate long totalRatings;@Overridepublic String toString() {return "BookAnalytics{" +"bookId=" + bookId +", bookName='" + bookName + '\'' +", borrowedCount='" + borrowedCount + '\'' +", viewedCount='" + viewedCount + '\'' +", averageRating='" + averageRating + '\'' +", totalRatings='" + totalRatings + '\'' +'}';}public BookAnalytics(UUID bookId, String bookName) {this.bookId = bookId;this.bookName = bookName;this.borrowedCount = 0;this.viewedCount = 0L;this.averageRating = 0D;}public void incrementBorrowedCount() {this.borrowedCount += 1;}public void incrementViewedCount() {this.viewedCount += 1;}public void incrementTotalRatings() {this.totalRatings += 1;}}
Code explanation:
-
Lines 10–12: We add annotations like
@Dataand@Tableto refer to a Cassandra table from the POJO. -
Line 14: We add the
bookIdproperty as the primary key using the@PrimaryKeyannotation. -
Lines 17–30: We add properties like
bookName,borrowedCount,viewedCount,averageRating, andtotalRatingsand annotate them with the@Columnannotation. -
Lines 32–41: We ...
Ask