diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/model/entity/ShelfEntity.java b/booklore-api/src/main/java/com/adityachandel/booklore/model/entity/ShelfEntity.java index 3e3e5da1b..686f5845c 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/model/entity/ShelfEntity.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/model/entity/ShelfEntity.java @@ -14,10 +14,7 @@ import java.util.Set; @AllArgsConstructor @NoArgsConstructor @Entity -@Table( - name = "shelf", - uniqueConstraints = @UniqueConstraint(columnNames = {"user_id", "name"}) -) +@Table(name = "shelf") public class ShelfEntity { @Id diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/BooksService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/BooksService.java index 08986e784..63dc492c8 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/BooksService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/BooksService.java @@ -97,68 +97,18 @@ public class BooksService { @Transactional public List assignShelvesToBooks(Set bookIds, Set shelfIdsToAssign, Set shelfIdsToUnassign) { - BookLoreUser user = authenticationService.getAuthenticatedUser(); - Optional bookLoreUserEntity = userRepository.findById(user.getId()); - - if (bookLoreUserEntity.isEmpty()) { - throw ApiError.USER_NOT_FOUND.createException(user.getId()); - } - - // Fetch books by their IDs List bookEntities = bookRepository.findAllById(bookIds); - - // Fetch shelves to assign and unassign List shelvesToAssign = shelfRepository.findAllById(shelfIdsToAssign); - List shelvesToUnassign = shelfRepository.findAllById(shelfIdsToUnassign); - - // Validate that the shelves belong to the authenticated user - validateShelfOwnership(shelvesToAssign, user); - validateShelfOwnership(shelvesToUnassign, user); - - // Explicitly set user on the shelves - shelvesToAssign.forEach(shelf -> { - if (shelf.getUser() == null) { - shelf.setUser(bookLoreUserEntity.get()); - } - }); - shelvesToUnassign.forEach(shelf -> { - if (shelf.getUser() == null) { - shelf.setUser(bookLoreUserEntity.get()); - } - }); - - // Ensure user_id is correctly set on book_shelf_mapping table by setting user_id explicitly when managing many-to-many relationships for (BookEntity bookEntity : bookEntities) { - // Unassign shelves bookEntity.getShelves().removeIf(shelf -> shelfIdsToUnassign.contains(shelf.getId())); - shelvesToUnassign.forEach(shelf -> shelf.getBookEntities().remove(bookEntity)); - - // Assign new shelves and add user_id - shelvesToAssign.forEach(shelf -> { + for (ShelfEntity shelf : shelvesToAssign) { if (!bookEntity.getShelves().contains(shelf)) { bookEntity.getShelves().add(shelf); } - if (!shelf.getBookEntities().contains(bookEntity)) { - shelf.getBookEntities().add(bookEntity); - } - - // Ensure the user_id is set correctly in the relationship table - bookRepository.save(bookEntity); - shelfRepository.save(shelf); // Explicit save after associating user - }); - } - - // Return the updated books - return bookEntities.stream().map(bookMapper::toBook).collect(Collectors.toList()); - } - - @Transactional - protected void validateShelfOwnership(List shelves, BookLoreUser user) { - for (ShelfEntity shelf : shelves) { - if (!shelf.getUser().getId().equals(user.getId())) { - throw ApiError.UNAUTHORIZED.createException("You are not authorized to modify the shelf: " + shelf.getName()); } } + bookRepository.saveAll(bookEntities); + return bookEntities.stream().map(bookMapper::toBook).collect(Collectors.toList()); } public Resource getBookCover(long bookId) { diff --git a/booklore-api/src/main/resources/db/migration/V1__Create_Library_and_Book_Tables.sql b/booklore-api/src/main/resources/db/migration/V1__Create_Library_and_Book_Tables.sql index f134d9e7d..752972069 100644 --- a/booklore-api/src/main/resources/db/migration/V1__Create_Library_and_Book_Tables.sql +++ b/booklore-api/src/main/resources/db/migration/V1__Create_Library_and_Book_Tables.sql @@ -170,7 +170,7 @@ CREATE INDEX idx_user_permissions_user ON user_permissions (user_id); CREATE TABLE IF NOT EXISTS shelf ( id BIGINT AUTO_INCREMENT PRIMARY KEY, - user_id BIGINT NOT NULL, + user_id BIGINT NOT NULL, name VARCHAR(255) NOT NULL, sort VARCHAR(255) NULL, icon VARCHAR(64) NOT NULL, @@ -182,11 +182,9 @@ CREATE TABLE IF NOT EXISTS book_shelf_mapping ( book_id BIGINT NOT NULL, shelf_id BIGINT NOT NULL, - user_id BIGINT NOT NULL, - PRIMARY KEY (book_id, shelf_id, user_id), + PRIMARY KEY (book_id, shelf_id), CONSTRAINT fk_book_shelf_mapping_book FOREIGN KEY (book_id) REFERENCES book (id) ON DELETE CASCADE, - CONSTRAINT fk_book_shelf_mapping_shelf FOREIGN KEY (shelf_id) REFERENCES shelf (id) ON DELETE CASCADE, - CONSTRAINT fk_book_shelf_mapping_user FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE + CONSTRAINT fk_book_shelf_mapping_shelf FOREIGN KEY (shelf_id) REFERENCES shelf (id) ON DELETE CASCADE ); CREATE TABLE app_settings