Refactor(api): rename and organize endpoints in BookController
- Renamed endpoints for improved clarity and consistency. - Grouped similar endpoints logically for better structure. - Updated request/response patterns for maintainability.
This commit is contained in:
+15
-22
@@ -1,7 +1,6 @@
|
||||
package com.adityachandel.booklore.controller;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.Book;
|
||||
import com.adityachandel.booklore.model.dto.BookViewerSetting;
|
||||
import com.adityachandel.booklore.model.dto.BookViewerSettings;
|
||||
import com.adityachandel.booklore.model.dto.request.ReadProgressRequest;
|
||||
import com.adityachandel.booklore.model.dto.request.ShelvesAssignmentRequest;
|
||||
@@ -15,27 +14,21 @@ import org.springframework.web.bind.annotation.*;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
@RequestMapping("/api/v1/book")
|
||||
@RequestMapping("/api/v1/books")
|
||||
@RestController
|
||||
@AllArgsConstructor
|
||||
public class BookController {
|
||||
|
||||
private BooksService booksService;
|
||||
|
||||
@GetMapping("/{bookId}")
|
||||
public ResponseEntity<Book> getBook(@PathVariable long bookId, @RequestParam(required = false, defaultValue = "false") boolean withDescription) {
|
||||
return ResponseEntity.ok(booksService.getBook(bookId, withDescription));
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public ResponseEntity<List<Book>> getBooks(@RequestParam(required = false, defaultValue = "false") boolean withDescription) {
|
||||
return ResponseEntity.ok(booksService.getBooks(withDescription));
|
||||
}
|
||||
|
||||
@GetMapping("/search")
|
||||
public ResponseEntity<List<Book>> searchBooks(@RequestParam String title) {
|
||||
List<Book> books = booksService.search(title);
|
||||
return ResponseEntity.ok(books);
|
||||
@GetMapping("/{bookId}")
|
||||
public ResponseEntity<Book> getBook(@PathVariable long bookId, @RequestParam(required = false, defaultValue = "false") boolean withDescription) {
|
||||
return ResponseEntity.ok(booksService.getBook(bookId, withDescription));
|
||||
}
|
||||
|
||||
@GetMapping("/{bookId}/cover")
|
||||
@@ -43,23 +36,28 @@ public class BookController {
|
||||
return ResponseEntity.ok(booksService.getBookCover(bookId));
|
||||
}
|
||||
|
||||
@GetMapping("/{bookId}/data")
|
||||
public ResponseEntity<byte[]> getBookData(@PathVariable long bookId) throws IOException {
|
||||
return booksService.getBookData(bookId);
|
||||
@GetMapping("/{bookId}/content")
|
||||
public ResponseEntity<byte[]> getBookContent(@PathVariable long bookId) throws IOException {
|
||||
return booksService.getBookContent(bookId);
|
||||
}
|
||||
|
||||
@GetMapping("/{bookId}/book-viewer-setting")
|
||||
@GetMapping("/{bookId}/download")
|
||||
public ResponseEntity<Resource> downloadFile(@PathVariable("bookId") Long bookId) {
|
||||
return booksService.prepareFileForDownload(bookId);
|
||||
}
|
||||
|
||||
@GetMapping("/{bookId}/viewer-settings")
|
||||
public ResponseEntity<BookViewerSettings> getBookViewerSettings(@PathVariable long bookId) {
|
||||
return ResponseEntity.ok(booksService.getBookViewerSetting(bookId));
|
||||
}
|
||||
|
||||
@PutMapping("/{bookId}/book-viewer-setting")
|
||||
@PutMapping("/{bookId}/viewer-settings")
|
||||
public ResponseEntity<Void> updateBookViewerSettings(@RequestBody BookViewerSettings bookViewerSettings, @PathVariable long bookId) {
|
||||
booksService.updateBookViewerSetting(bookId, bookViewerSettings);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
@PostMapping("/assign-shelves")
|
||||
@PostMapping("/shelves")
|
||||
public ResponseEntity<List<Book>> addBookToShelf(@RequestBody @Valid ShelvesAssignmentRequest request) {
|
||||
return ResponseEntity.ok(booksService.assignShelvesToBooks(request.getBookIds(), request.getShelvesToAssign(), request.getShelvesToUnassign()));
|
||||
}
|
||||
@@ -69,9 +67,4 @@ public class BookController {
|
||||
booksService.updateReadProgress(request);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
@GetMapping("/{bookId}/download")
|
||||
public ResponseEntity<Resource> downloadFile(@PathVariable("bookId") Long bookId) {
|
||||
return booksService.prepareFileForDownload(bookId);
|
||||
}
|
||||
}
|
||||
@@ -98,7 +98,7 @@ public class BooksService {
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public ResponseEntity<byte[]> getBookData(long bookId) throws IOException {
|
||||
public ResponseEntity<byte[]> getBookContent(long bookId) throws IOException {
|
||||
BookEntity bookEntity = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
|
||||
byte[] pdfBytes = Files.readAllBytes(new File(FileUtils.getBookFullPath(bookEntity)).toPath());
|
||||
return ResponseEntity.ok()
|
||||
|
||||
@@ -11,7 +11,7 @@ import {API_CONFIG} from '../../config/api-config';
|
||||
})
|
||||
export class BookService {
|
||||
|
||||
private readonly url = `${API_CONFIG.BASE_URL}/api/v1/book`;
|
||||
private readonly url = `${API_CONFIG.BASE_URL}/api/v1/books`;
|
||||
|
||||
private bookStateSubject = new BehaviorSubject<BookState>({
|
||||
books: null,
|
||||
@@ -53,7 +53,7 @@ export class BookService {
|
||||
shelvesToAssign: Array.from(shelvesToAssign),
|
||||
shelvesToUnassign: Array.from(shelvesToUnassign),
|
||||
};
|
||||
return this.http.post<Book[]>(`${this.url}/assign-shelves`, requestPayload).pipe(
|
||||
return this.http.post<Book[]>(`${this.url}/shelves`, requestPayload).pipe(
|
||||
map(updatedBooks => {
|
||||
const currentState = this.bookStateSubject.value;
|
||||
const currentBooks = currentState.books || [];
|
||||
@@ -92,11 +92,11 @@ export class BookService {
|
||||
}
|
||||
|
||||
getBookSetting(bookId: number): Observable<BookSetting> {
|
||||
return this.http.get<BookSetting>(`${this.url}/${bookId}/book-viewer-setting`);
|
||||
return this.http.get<BookSetting>(`${this.url}/${bookId}/viewer-settings`);
|
||||
}
|
||||
|
||||
updateViewerSetting(bookSetting: BookSetting, bookId: number): Observable<void> {
|
||||
return this.http.put<void>(`${this.url}/${bookId}/book-viewer-setting`, bookSetting);
|
||||
return this.http.put<void>(`${this.url}/${bookId}/viewer-setting`, bookSetting);
|
||||
}
|
||||
|
||||
updateLastReadTime(bookId: number) {
|
||||
@@ -140,11 +140,7 @@ export class BookService {
|
||||
}
|
||||
|
||||
getPdfData(bookId: number): Observable<Blob> {
|
||||
return this.http.get<Blob>(`${this.url}/${bookId}/data`, {responseType: 'blob' as 'json'});
|
||||
}
|
||||
|
||||
getBookCoverUrl(bookId: number): string {
|
||||
return `${this.url}/${bookId}/cover`;
|
||||
return this.http.get<Blob>(`${this.url}/${bookId}/content`, {responseType: 'blob' as 'json'});
|
||||
}
|
||||
|
||||
getBookByIdFromAPI(bookId: number, withDescription: boolean) {
|
||||
@@ -203,21 +199,4 @@ export class BookService {
|
||||
const book = this.bookStateSubject.value.books?.find(book => book.id === bookId);
|
||||
return book?.metadata ?? null;
|
||||
}
|
||||
|
||||
updateLock(bookId: number, field: string, isLocked: boolean): void {
|
||||
const currentState = this.bookStateSubject.value;
|
||||
const updatedBooks = (currentState.books || []).map(book => {
|
||||
if (book.id === bookId && book.metadata) {
|
||||
return {
|
||||
...book,
|
||||
metadata: {
|
||||
...book.metadata,
|
||||
[`${field}Locked`]: isLocked
|
||||
}
|
||||
};
|
||||
}
|
||||
return book;
|
||||
});
|
||||
this.bookStateSubject.next({...currentState, books: updatedBooks});
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -25,7 +25,7 @@
|
||||
[ngClass]="{'green-outlined-button': isValueSaved('title'), 'yellow-outlined-button': isValueCopied('title')}"
|
||||
class="arrow-button"
|
||||
(click)="copyFetchedToCurrent('title')"/>
|
||||
<input pInputText [value]="fetchedMetadata.title ?? null" class="input" readonly/>
|
||||
<input pInputText [value]="fetchedMetadata.title" class="input" readonly/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user