Display other books from the same series on the metadata viewer page (#151)
* Display other books from the same series on the metadata viewer page * feat(recommendations): filter out books from the same series as the target book --------- Co-authored-by: adityachandelgit <>
This commit is contained in:
co-authored by
adityachandelgit
parent
b66862d81b
commit
aee22fb0cd
+14
-1
@@ -6,6 +6,7 @@ import com.adityachandel.booklore.model.dto.Book;
|
||||
import com.adityachandel.booklore.model.dto.BookRecommendation;
|
||||
import com.adityachandel.booklore.model.entity.AuthorEntity;
|
||||
import com.adityachandel.booklore.model.entity.BookEntity;
|
||||
import com.adityachandel.booklore.model.entity.BookMetadataEntity;
|
||||
import com.adityachandel.booklore.repository.BookRepository;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -30,8 +31,20 @@ public class BookRecommendationService {
|
||||
|
||||
List<BookEntity> candidates = bookRepository.findAll();
|
||||
|
||||
String targetSeriesName = Optional.ofNullable(target.getMetadata())
|
||||
.map(BookMetadataEntity::getSeriesName)
|
||||
.map(String::toLowerCase)
|
||||
.orElse(null);
|
||||
|
||||
List<SimpleEntry<BookEntity, Double>> scored = candidates.stream()
|
||||
.filter(candidate -> !candidate.getId().equals(bookId))
|
||||
.filter(candidate -> !candidate.getId().equals(bookId)) // exclude self
|
||||
.filter(candidate -> {
|
||||
String candidateSeriesName = Optional.ofNullable(candidate.getMetadata())
|
||||
.map(BookMetadataEntity::getSeriesName)
|
||||
.map(String::toLowerCase)
|
||||
.orElse(null);
|
||||
return targetSeriesName == null || !targetSeriesName.equals(candidateSeriesName);
|
||||
})
|
||||
.map(candidate -> new SimpleEntry<>(candidate, similarityService.calculateSimilarity(target, candidate)))
|
||||
.filter(entry -> entry.getValue() > 0.0)
|
||||
.sorted(Map.Entry.<BookEntity, Double>comparingByValue().reversed())
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import {inject, Injectable} from '@angular/core';
|
||||
import {BehaviorSubject, Observable, of} from 'rxjs';
|
||||
import {BehaviorSubject, first, Observable, of} from 'rxjs';
|
||||
import {HttpClient, HttpParams} from '@angular/common/http';
|
||||
import {catchError, map, tap} from 'rxjs/operators';
|
||||
import {catchError, filter, map, tap} from 'rxjs/operators';
|
||||
import {Book, BookMetadata, BookRecommendation, BookSetting} from '../model/book.model';
|
||||
import {BookState} from '../model/state/book-state.model';
|
||||
import {API_CONFIG} from '../../config/api-config';
|
||||
@@ -280,6 +280,27 @@ export class BookService {
|
||||
});
|
||||
}
|
||||
|
||||
getBooksInSeries(bookId: number): Observable<Book[]> {
|
||||
return this.bookStateSubject.asObservable().pipe(
|
||||
filter(state => state.loaded),
|
||||
first(),
|
||||
map(state => {
|
||||
const allBooks = state.books || [];
|
||||
const currentBook = allBooks.find(b => b.id === bookId);
|
||||
|
||||
if (!currentBook || !currentBook.metadata?.seriesName) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const seriesName = currentBook.metadata.seriesName.toLowerCase();
|
||||
return allBooks.filter(b =>
|
||||
b.id !== bookId &&
|
||||
b.metadata?.seriesName?.toLowerCase() === seriesName
|
||||
);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/*------------------ All the websocket handlers go below ------------------*/
|
||||
|
||||
|
||||
@@ -42,6 +42,7 @@ export class BookMetadataCenterComponent implements OnInit, OnDestroy {
|
||||
private routeSubscription: Subscription = Subscription.EMPTY;
|
||||
|
||||
ngOnInit(): void {
|
||||
this.bookService.loadBooks();
|
||||
this.routeSubscription = this.route.paramMap
|
||||
.pipe(
|
||||
switchMap(params => {
|
||||
|
||||
+45
-29
@@ -1,7 +1,6 @@
|
||||
<div class="pt-2 flex flex-col h-full">
|
||||
<div class="pt-2 flex flex-col h-full overflow-hidden">
|
||||
<div *ngIf="metadata$ | async as metadata">
|
||||
<div class="flex flex-row">
|
||||
|
||||
<img [attr.src]="urlHelper.getCoverUrl(metadata.bookId, metadata?.coverUpdatedOn)"
|
||||
class="book-cover placeholder"
|
||||
alt="Cover of {{ metadata.title }}"
|
||||
@@ -12,7 +11,7 @@
|
||||
<div class="pl-6 flex flex-col w-full">
|
||||
<div class="flex flex-row items-center justify-between">
|
||||
<div>
|
||||
<p *ngIf="metadata.seriesName" class="italic">{{ metadata.seriesName }} #{{ metadata.seriesNumber }} </p>
|
||||
<p *ngIf="metadata.seriesName" class="italic">{{ metadata.seriesName }} #{{ metadata.seriesNumber }}</p>
|
||||
<div class="flex items-center gap-2">
|
||||
<p class="text-2xl font-bold m-0">{{ metadata.title }}</p>
|
||||
<i
|
||||
@@ -61,7 +60,6 @@
|
||||
</div>
|
||||
|
||||
<p class="pt-2"><span class="font-bold">File Path:</span> {{ book?.filePath }}</p>
|
||||
|
||||
</div>
|
||||
|
||||
<div *ngIf="userService.userData$ | async as userData" class="pl-6 pt-4 flex flex-row w-full gap-4">
|
||||
@@ -71,13 +69,12 @@
|
||||
<p-splitbutton *ngIf="userData.permissions.canEmailBook" label="Quick Send" icon="pi pi-send" [model]="items" (onClick)="quickSend(metadata.bookId)" outlined severity="info"/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p-divider></p-divider>
|
||||
|
||||
<div *ngIf="metadata$ | async as metadata" class="space-y-2 px-2">
|
||||
<div *ngIf="metadata$ | async as metadata" class="flex-1 overflow-auto space-y-2 px-2 pb-4">
|
||||
<div [ngClass]="{ 'line-clamp-5': !isExpanded, 'line-clamp-none': isExpanded }"
|
||||
class="transition-all duration-300 overflow-hidden description-container">
|
||||
<div class="description-content" [innerHTML]="metadata.description || 'No description available.'"></div>
|
||||
@@ -93,28 +90,47 @@
|
||||
(click)="toggleExpand()">
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<p-divider></p-divider>
|
||||
|
||||
<ng-container *ngIf="recommendedBooks.length > 0">
|
||||
<div>
|
||||
<h3 class="text-xl font-semibold">Similar Books</h3>
|
||||
<div class="dashboard-scroller-infinite"
|
||||
infiniteScroll
|
||||
[infiniteScrollDistance]="2"
|
||||
[infiniteScrollThrottle]="50"
|
||||
[horizontal]="true">
|
||||
<div class="dashboard-scroller-card" *ngFor="let book of recommendedBooks">
|
||||
<app-book-card
|
||||
[book]="book.book"
|
||||
[isCheckboxEnabled]="false"
|
||||
[readButtonHidden]="true"
|
||||
[bottomBarHidden]="true">
|
||||
</app-book-card>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</ng-container>
|
||||
|
||||
<ng-container *ngIf="bookInSeries.length > 0">
|
||||
<div>
|
||||
<h3 class="text-xl font-semibold pt-2">More in the Series</h3>
|
||||
<div class="dashboard-scroller-infinite"
|
||||
infiniteScroll
|
||||
[infiniteScrollDistance]="2"
|
||||
[infiniteScrollThrottle]="50"
|
||||
[horizontal]="true">
|
||||
<div class="dashboard-scroller-card" *ngFor="let book of bookInSeries">
|
||||
<app-book-card
|
||||
[book]="book"
|
||||
[isCheckboxEnabled]="false"
|
||||
[readButtonHidden]="true"
|
||||
[bottomBarHidden]="true">
|
||||
</app-book-card>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</ng-container>
|
||||
</div>
|
||||
|
||||
<p-divider></p-divider>
|
||||
|
||||
<div class="recommended-books-header pb-2">
|
||||
<h3 class="text-xl font-semibold">Similar Books</h3>
|
||||
</div>
|
||||
|
||||
<div class="dashboard-scroller-infinite"
|
||||
*ngIf="recommendedBooks.length > 0"
|
||||
infiniteScroll
|
||||
[infiniteScrollDistance]="2"
|
||||
[infiniteScrollThrottle]="50"
|
||||
[horizontal]="true">
|
||||
<div class="dashboard-scroller-card" *ngFor="let book of recommendedBooks">
|
||||
<app-book-card
|
||||
[book]="book.book"
|
||||
[isCheckboxEnabled]="false"
|
||||
[readButtonHidden]="true"
|
||||
[bottomBarHidden]="true">
|
||||
</app-book-card>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
-1
@@ -36,7 +36,6 @@
|
||||
}
|
||||
|
||||
.dashboard-scroller-card {
|
||||
height: 165px;
|
||||
width: 101px;
|
||||
flex-shrink: 0;
|
||||
flex-grow: 0;
|
||||
|
||||
+19
-8
@@ -45,16 +45,9 @@ export class MetadataViewerComponent implements OnInit {
|
||||
metadata$: Observable<BookMetadata | null> = this.metadataCenterService.currentMetadata$;
|
||||
items: MenuItem[] | undefined;
|
||||
recommendedBooks: BookRecommendation[] = [];
|
||||
|
||||
bookInSeries: Book[] = [];
|
||||
isExpanded = false;
|
||||
|
||||
toggleExpand(): void {
|
||||
this.isExpanded = !this.isExpanded;
|
||||
}
|
||||
|
||||
shouldShowToggle(description?: string): boolean {
|
||||
return (description?.length || 0) > 300;
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.items = [
|
||||
@@ -88,6 +81,18 @@ export class MetadataViewerComponent implements OnInit {
|
||||
this.getBookRecommendations(metadata.bookId);
|
||||
}
|
||||
});
|
||||
|
||||
this.metadata$
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe((metadata) => {
|
||||
if (metadata) {
|
||||
this.getBooksInSeries(metadata.bookId);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
toggleExpand(): void {
|
||||
this.isExpanded = !this.isExpanded;
|
||||
}
|
||||
|
||||
getBookRecommendations(bookId: number): void {
|
||||
@@ -96,6 +101,12 @@ export class MetadataViewerComponent implements OnInit {
|
||||
});
|
||||
}
|
||||
|
||||
getBooksInSeries(bookId: number): void {
|
||||
this.bookService.getBooksInSeries(bookId).subscribe((bookInSeries) => {
|
||||
this.bookInSeries = bookInSeries;
|
||||
});
|
||||
}
|
||||
|
||||
read(bookId: number): void {
|
||||
this.bookService.readBook(bookId);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user