Fix: Resolved Hibernate constraint violation caused by duplicate category mappings
This commit is contained in:
committed by
Aditya Chandel
parent
8d09430043
commit
f43c71b1f1
+4
-4
@@ -42,11 +42,11 @@ public class MetadataController {
|
|||||||
|
|
||||||
@PutMapping("/{bookId}/metadata")
|
@PutMapping("/{bookId}/metadata")
|
||||||
@PreAuthorize("@securityUtil.canEditMetadata() or @securityUtil.isAdmin()")
|
@PreAuthorize("@securityUtil.canEditMetadata() or @securityUtil.isAdmin()")
|
||||||
public ResponseEntity<BookMetadata> updateMetadata(
|
public ResponseEntity<BookMetadata> updateMetadata(@RequestBody BookMetadata setMetadataRequest, @PathVariable long bookId, @RequestParam(defaultValue = "true") boolean mergeCategories) {
|
||||||
@RequestBody BookMetadata setMetadataRequest, @PathVariable long bookId,
|
|
||||||
@RequestParam(defaultValue = "true") boolean mergeCategories) {
|
|
||||||
BookEntity bookEntity = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
|
BookEntity bookEntity = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
|
||||||
BookMetadata bookMetadata = bookMetadataMapper.toBookMetadata(bookMetadataUpdater.setBookMetadata(bookEntity, setMetadataRequest, true, mergeCategories), true);
|
bookMetadataUpdater.setBookMetadata(bookEntity, setMetadataRequest, true, mergeCategories);
|
||||||
|
bookRepository.save(bookEntity);
|
||||||
|
BookMetadata bookMetadata = bookMetadataMapper.toBookMetadata(bookEntity.getMetadata(), true);
|
||||||
return ResponseEntity.ok(bookMetadata);
|
return ResponseEntity.ok(bookMetadata);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-11
@@ -28,19 +28,14 @@ import com.adityachandel.booklore.service.fileprocessor.PdfProcessor;
|
|||||||
import com.adityachandel.booklore.service.metadata.parser.BookParser;
|
import com.adityachandel.booklore.service.metadata.parser.BookParser;
|
||||||
import com.adityachandel.booklore.service.BookQueryService;
|
import com.adityachandel.booklore.service.BookQueryService;
|
||||||
import com.adityachandel.booklore.util.FileService;
|
import com.adityachandel.booklore.util.FileService;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
import com.fasterxml.jackson.databind.ObjectReader;
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
@@ -140,17 +135,13 @@ public class BookMetadataService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
protected BookMetadataEntity updateBookMetadata(BookEntity bookEntity, BookMetadata metadata, boolean replaceCover, boolean mergeCategories) {
|
protected void updateBookMetadata(BookEntity bookEntity, BookMetadata metadata, boolean replaceCover, boolean mergeCategories) {
|
||||||
if (metadata != null) {
|
if (metadata != null) {
|
||||||
BookMetadataEntity bookMetadata = bookMetadataUpdater.setBookMetadata(bookEntity, metadata, replaceCover, mergeCategories);
|
bookMetadataUpdater.setBookMetadata(bookEntity, metadata, replaceCover, mergeCategories);
|
||||||
bookEntity.setMetadata(bookMetadata);
|
|
||||||
bookRepository.save(bookEntity);
|
|
||||||
Book book = bookMapper.toBook(bookEntity);
|
Book book = bookMapper.toBook(bookEntity);
|
||||||
notificationService.sendMessage(Topic.BOOK_METADATA_UPDATE, book);
|
notificationService.sendMessage(Topic.BOOK_METADATA_UPDATE, book);
|
||||||
notificationService.sendMessage(Topic.LOG, createLogNotification("Book metadata updated: " + book.getMetadata().getTitle()));
|
notificationService.sendMessage(Topic.LOG, createLogNotification("Book metadata updated: " + book.getMetadata().getTitle()));
|
||||||
return bookMetadata;
|
|
||||||
}
|
}
|
||||||
return bookEntity.getMetadata();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
|
|||||||
+2
-9
@@ -36,9 +36,7 @@ import java.util.stream.Collectors;
|
|||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class BookMetadataUpdater {
|
public class BookMetadataUpdater {
|
||||||
|
|
||||||
private final BookRepository bookRepository;
|
|
||||||
private final AuthorRepository authorRepository;
|
private final AuthorRepository authorRepository;
|
||||||
private final BookMetadataRepository bookMetadataRepository;
|
|
||||||
private final CategoryRepository categoryRepository;
|
private final CategoryRepository categoryRepository;
|
||||||
private final FileService fileService;
|
private final FileService fileService;
|
||||||
private final MetadataMatchService metadataMatchService;
|
private final MetadataMatchService metadataMatchService;
|
||||||
@@ -47,14 +45,14 @@ public class BookMetadataUpdater {
|
|||||||
private final EpubMetadataWriter epubMetadataWriter;
|
private final EpubMetadataWriter epubMetadataWriter;
|
||||||
|
|
||||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||||
public BookMetadataEntity setBookMetadata(BookEntity bookEntity, BookMetadata newMetadata, boolean setThumbnail, boolean mergeCategories) {
|
public void setBookMetadata(BookEntity bookEntity, BookMetadata newMetadata, boolean setThumbnail, boolean mergeCategories) {
|
||||||
BookMetadataEntity metadata = bookEntity.getMetadata();
|
BookMetadataEntity metadata = bookEntity.getMetadata();
|
||||||
|
|
||||||
updateLocks(newMetadata, metadata);
|
updateLocks(newMetadata, metadata);
|
||||||
|
|
||||||
if (metadata.areAllFieldsLocked()) {
|
if (metadata.areAllFieldsLocked()) {
|
||||||
log.warn("Attempted to update metadata for book with ID {}, but all fields are locked. No update performed.", bookEntity.getId());
|
log.warn("Attempted to update metadata for book with ID {}, but all fields are locked. No update performed.", bookEntity.getId());
|
||||||
return metadata;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
MetadataPersistenceSettings settings = appSettingService.getAppSettings().getMetadataPersistenceSettings();
|
MetadataPersistenceSettings settings = appSettingService.getAppSettings().getMetadataPersistenceSettings();
|
||||||
@@ -76,12 +74,9 @@ public class BookMetadataUpdater {
|
|||||||
updateCategoriesIfNeeded(newMetadata, metadata, mergeCategories);
|
updateCategoriesIfNeeded(newMetadata, metadata, mergeCategories);
|
||||||
updateThumbnailIfNeeded(bookEntity.getId(), newMetadata, metadata, setThumbnail);
|
updateThumbnailIfNeeded(bookEntity.getId(), newMetadata, metadata, setThumbnail);
|
||||||
|
|
||||||
bookMetadataRepository.save(metadata);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Float score = metadataMatchService.calculateMatchScore(bookEntity);
|
Float score = metadataMatchService.calculateMatchScore(bookEntity);
|
||||||
bookEntity.setMetadataMatchScore(score);
|
bookEntity.setMetadataMatchScore(score);
|
||||||
bookRepository.save(bookEntity);
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn("Failed to calculate/save metadata match score for book ID {}: {}", bookEntity.getId(), e.getMessage());
|
log.warn("Failed to calculate/save metadata match score for book ID {}: {}", bookEntity.getId(), e.getMessage());
|
||||||
}
|
}
|
||||||
@@ -95,8 +90,6 @@ public class BookMetadataUpdater {
|
|||||||
log.warn("Failed to write metadata to EPUB for book ID {}: {}", bookEntity.getId(), e.getMessage());
|
log.warn("Failed to write metadata to EPUB for book ID {}: {}", bookEntity.getId(), e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return metadata;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateBasicFields(BookMetadata newMetadata, BookMetadataEntity metadata) {
|
private void updateBasicFields(BookMetadata newMetadata, BookMetadataEntity metadata) {
|
||||||
|
|||||||
Reference in New Issue
Block a user