show session numbers in book (#2696)

This commit is contained in:
WorldTeacher
2026-02-11 10:22:01 -07:00
committed by GitHub
parent aac8246bcf
commit 7e6ad212b0
4 changed files with 16 additions and 24 deletions
@@ -37,8 +37,11 @@ public class ReadingSessionController {
@ApiResponse(responseCode = "404", description = "Book not found") @ApiResponse(responseCode = "404", description = "Book not found")
}) })
@GetMapping("/book/{bookId}") @GetMapping("/book/{bookId}")
public ResponseEntity<Page<ReadingSessionResponse>> getReadingSessionsForBook(@PathVariable Long bookId, @RequestParam(defaultValue = "0") int page) { public ResponseEntity<Page<ReadingSessionResponse>> getReadingSessionsForBook(
Page<ReadingSessionResponse> sessions = readingSessionService.getReadingSessionsForBook(bookId, page); @PathVariable Long bookId,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "5") int size) {
Page<ReadingSessionResponse> sessions = readingSessionService.getReadingSessionsForBook(bookId, page, size);
return ResponseEntity.ok(sessions); return ResponseEntity.ok(sessions);
} }
} }
@@ -244,13 +244,13 @@ public class ReadingSessionService {
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
public Page<ReadingSessionResponse> getReadingSessionsForBook(Long bookId, int page) { public Page<ReadingSessionResponse> getReadingSessionsForBook(Long bookId, int page, int size) {
BookLoreUser authenticatedUser = authenticationService.getAuthenticatedUser(); BookLoreUser authenticatedUser = authenticationService.getAuthenticatedUser();
Long userId = authenticatedUser.getId(); Long userId = authenticatedUser.getId();
bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId)); bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
Pageable pageable = PageRequest.of(page, 5); Pageable pageable = PageRequest.of(page, size);
Page<ReadingSessionEntity> sessions = readingSessionRepository.findByUserIdAndBookId(userId, bookId, pageable); Page<ReadingSessionEntity> sessions = readingSessionRepository.findByUserIdAndBookId(userId, bookId, pageable);
return sessions.map(session -> ReadingSessionResponse.builder() return sessions.map(session -> ReadingSessionResponse.builder()
@@ -12,17 +12,13 @@
} @else { } @else {
<p-table <p-table
[value]="sessions" [value]="sessions"
[lazy]="true"
[paginator]="true" [paginator]="true"
[rows]="rows" [rows]="rows"
[totalRecords]="totalRecords"
[loading]="loading" [loading]="loading"
[first]="first"
(onLazyLoad)="onPageChange($event)"
[showCurrentPageReport]="true" [showCurrentPageReport]="true"
[currentPageReportTemplate]="t('pageReport')" currentPageReportTemplate="Showing {first} to {last} of {totalRecords} sessions"
class="p-datatable-sm"> class="p-datatable-sm">
<ng-template #header> <ng-template pTemplate="header">
<tr> <tr>
<th>{{ t('headerSession') }}</th> <th>{{ t('headerSession') }}</th>
<th>{{ t('headerType') }}</th> <th>{{ t('headerType') }}</th>
@@ -32,7 +28,7 @@
<th>{{ t('headerLocation') }}</th> <th>{{ t('headerLocation') }}</th>
</tr> </tr>
</ng-template> </ng-template>
<ng-template #body let-session> <ng-template pTemplate="body" let-session>
<tr> <tr>
<td> <td>
<div class="session-time"> <div class="session-time">
@@ -18,29 +18,29 @@ export class BookReadingSessionsComponent implements OnInit, OnChanges {
private readonly t = inject(TranslocoService); private readonly t = inject(TranslocoService);
sessions: ReadingSessionResponse[] = []; sessions: ReadingSessionResponse[] = [];
totalRecords = 0;
first = 0;
rows = 5; rows = 5;
loading = false; loading = false;
get pageReportTemplate(): string {
return this.t.translate('metadata.readingSessions.pageReport');
}
ngOnInit() { ngOnInit() {
this.loadSessions(); this.loadSessions();
} }
ngOnChanges(changes: SimpleChanges) { ngOnChanges(changes: SimpleChanges) {
if (changes['bookId'] && !changes['bookId'].firstChange) { if (changes['bookId'] && !changes['bookId'].firstChange) {
this.first = 0;
this.loadSessions(); this.loadSessions();
} }
} }
loadSessions(page: number = 0) { loadSessions() {
this.loading = true; this.loading = true;
this.readingSessionService.getSessionsByBookId(this.bookId, page, this.rows) this.readingSessionService.getSessionsByBookId(this.bookId, 0, 9999)
.subscribe({ .subscribe({
next: (response) => { next: (response) => {
this.sessions = response.content; this.sessions = response.content;
this.totalRecords = response.totalElements;
this.loading = false; this.loading = false;
}, },
error: () => { error: () => {
@@ -49,13 +49,6 @@ export class BookReadingSessionsComponent implements OnInit, OnChanges {
}); });
} }
onPageChange(event: { first?: number; rows?: number | null }): void {
if (event.first === undefined || event.rows === undefined || event.rows === null) return;
this.first = event.first;
const page = Math.floor(event.first / event.rows);
this.loadSessions(page);
}
formatDuration(seconds: number): string { formatDuration(seconds: number): string {
const hours = Math.floor(seconds / 3600); const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60); const minutes = Math.floor((seconds % 3600) / 60);