Make metadata update reactive

This commit is contained in:
aditya.chandel
2024-12-25 23:48:28 -07:00
parent 4220a3e324
commit f3bf5e9c0b
7 changed files with 38 additions and 12 deletions
@@ -75,9 +75,8 @@ public class BookController {
}
@PutMapping("/{bookId}/set-metadata")
public ResponseEntity<Void> setBookMetadata(@RequestBody SetMetadataRequest setMetadataRequest, @PathVariable long bookId) {
booksService.setMetadata(setMetadataRequest, bookId);
return ResponseEntity.noContent().build();
public ResponseEntity<BookDTO> setBookMetadata(@RequestBody SetMetadataRequest setMetadataRequest, @PathVariable long bookId) {
return ResponseEntity.ok(booksService.setMetadata(setMetadataRequest, bookId));
}
@PostMapping("/assign-shelves")
@@ -1,6 +1,7 @@
package com.adityachandel.booklore.exception;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@@ -11,6 +12,7 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
@@ -52,6 +54,7 @@ public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleGenericException(Exception ex) {
ErrorResponse errorResponse = new ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), "An unexpected error occurred.");
log.error(ex.getMessage(), ex);
return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@@ -112,11 +112,11 @@ public class BooksService {
public List<GoogleBooksMetadata> fetchProspectiveMetadataListByBookId(long bookId) {
Book book = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
StringBuilder searchString = new StringBuilder();
if (!book.getMetadata().getTitle().isEmpty()) {
if (book.getMetadata().getTitle() != null && !book.getMetadata().getTitle().isEmpty()) {
searchString.append(book.getMetadata().getTitle());
}
if (searchString.isEmpty()) {
searchString.append(book.getFileName());
searchString.append(cleanFileName(book.getFileName()));
}
if (book.getMetadata().getAuthors() != null && !book.getMetadata().getAuthors().isEmpty()) {
if (!searchString.isEmpty()) {
@@ -129,11 +129,19 @@ public class BooksService {
return googleBookMetadataService.queryByTerm(searchString.toString());
}
private char[] cleanFileName(String fileName) {
if (fileName == null) {
return null;
}
String cleanedFileName = fileName.replace("(Z-Library)", "").trim();
return cleanedFileName.toCharArray();
}
public List<GoogleBooksMetadata> fetchProspectiveMetadataListBySearchTerm(String searchTerm) {
return googleBookMetadataService.queryByTerm(searchTerm);
}
public void setMetadata(SetMetadataRequest setMetadataRequest, long bookId) {
public BookDTO setMetadata(SetMetadataRequest setMetadataRequest, long bookId) {
Book book = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
GoogleBooksMetadata gMetadata = googleBookMetadataService.getByGoogleBookId(setMetadataRequest.getGoogleBookId());
BookMetadata metadata = book.getMetadata();
@@ -178,6 +186,8 @@ public class BooksService {
authorRepository.saveAll(metadata.getAuthors());
categoryRepository.saveAll(metadata.getCategories());
metadataRepository.save(metadata);
return BookTransformer.convertToBookDTO(book);
}
public String normalizeDate(String input) {
@@ -3,7 +3,7 @@ CREATE TABLE IF NOT EXISTS library
id BIGINT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) UNIQUE NOT NULL,
paths TEXT,
sort VARCHAR(255)
sort VARCHAR(255) NULL
);
CREATE TABLE IF NOT EXISTS book
@@ -85,7 +85,7 @@ CREATE TABLE IF NOT EXISTS shelf
(
id BIGINT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL UNIQUE,
sort VARCHAR(255)
sort VARCHAR(255) NULL
);
CREATE TABLE IF NOT EXISTS book_shelf_mapping
@@ -0,0 +1,2 @@
INSERT INTO booklore.shelf (name) VALUES ('Favorites');
INSERT INTO booklore.shelf (name) VALUES ('Read Later');
@@ -10,7 +10,7 @@
border: 1px solid var(--surface-border);
border-radius: var(--border-radius);
z-index: 1000;
max-height: 300px;
max-height: 500px;
overflow-y: auto;
width: 600px;
opacity: 0;
@@ -56,7 +56,7 @@ export class BookService {
removeBooksFromShelf(shelfId: number): void {
const books = this.books.value.filter(book => {
if(book.shelves) {
if (book.shelves) {
book.shelves = book.shelves.filter(shelf => shelf.id !== shelfId);
}
return book;
@@ -129,9 +129,21 @@ export class BookService {
return this.http.get<BookMetadata[]>(`${this.url}/fetch-metadata?term=${term}`);
}
setBookMetadata(googleBookId: string, bookId: number): Observable<void> {
setBookMetadata(googleBookId: string, bookId: number): Observable<Book> {
const requestBody = {googleBookId: googleBookId};
return this.http.put<void>(`${this.url}/${bookId}/set-metadata`, requestBody);
return this.http.put<Book>(`${this.url}/${bookId}/set-metadata`, requestBody).pipe(
map(book => {
const updatedBooks = this.books.getValue().map(existingBook =>
existingBook.id === book.id ? book : existingBook
);
this.books.next(updatedBooks);
return book;
}),
catchError(error => {
console.error('Error assigning shelves to books:', error);
throw error;
})
);
}
handleNewlyCreatedBook(book: Book): void {