feat(bookmark): improve bookmark feature by adding rename, note, color, priority functionalities (#1946)
* feat(bookmark): add bookmark editing, priority, color, notes, and improved sorting - Add UpdateBookMarkRequest DTO and bookmark editing dialog/component in frontend - Extend BookMark model/entity with color, notes, priority, updatedAt fields - Implement bookmark update API and service logic with validation - Sort bookmarks by priority and creation date - Add Flyway migrations for new columns and index - Update tests for new bookmark features Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * fix(bookmark): prevent notes length display error in edit dialog Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * fix(bookmark): reset editing state and improve dialog cancel handling Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * fix(bookmark): improve edit dialog template with Angular @if and conditional error display Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * feat(bookmark): add view dialog, search, and improved display for bookmarks in reader Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * feat(bookmark): redesign bookmarks section UI with improved layout, styling, and interactions Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * feat(bookmark): enhance view dialog UI with improved layout, styling, and priority display Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * chore(migration): rename migration files to maintain sequential versioning Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * feat(bookmark): add view and edit actions to bookmark list with improved UI and tooltips Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * feat(bookmark): add search and filter functionality to bookmark list in EPUB reader Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> * feat(bookmark): update search input to use PrimeNG IconField and InputIcon components Signed-off-by: Balázs Szücs <bszucs1209@gmail.com> --------- Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
This commit is contained in:
@@ -2,11 +2,15 @@ package com.adityachandel.booklore;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
import com.adityachandel.booklore.config.BookmarkProperties;
|
||||
|
||||
@EnableScheduling
|
||||
@EnableAsync
|
||||
@EnableConfigurationProperties(BookmarkProperties.class)
|
||||
@SpringBootApplication
|
||||
public class BookloreApplication {
|
||||
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.adityachandel.booklore.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "bookmarks")
|
||||
@Data
|
||||
public class BookmarkProperties {
|
||||
private int defaultPriority = 3;
|
||||
private int minPriority = 1;
|
||||
private int maxPriority = 5;
|
||||
private int maxNotesLength = 2000;
|
||||
private int maxTitleLength = 255;
|
||||
private int maxCfiLength = 500;
|
||||
}
|
||||
+18
@@ -2,6 +2,7 @@ package com.adityachandel.booklore.controller;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.BookMark;
|
||||
import com.adityachandel.booklore.model.dto.CreateBookMarkRequest;
|
||||
import com.adityachandel.booklore.model.dto.UpdateBookMarkRequest;
|
||||
import com.adityachandel.booklore.service.book.BookMarkService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
@@ -30,6 +31,14 @@ public class BookMarkController {
|
||||
return bookMarkService.getBookmarksForBook(bookId);
|
||||
}
|
||||
|
||||
@Operation(summary = "Get a specific bookmark", description = "Retrieve a specific bookmark by its ID.")
|
||||
@ApiResponse(responseCode = "200", description = "Bookmark returned successfully")
|
||||
@GetMapping("/{bookmarkId}")
|
||||
public BookMark getBookmarkById(
|
||||
@Parameter(description = "ID of the bookmark") @PathVariable Long bookmarkId) {
|
||||
return bookMarkService.getBookmarkById(bookmarkId);
|
||||
}
|
||||
|
||||
@Operation(summary = "Create a bookmark", description = "Create a new bookmark for a book.")
|
||||
@ApiResponse(responseCode = "200", description = "Bookmark created successfully")
|
||||
@PostMapping
|
||||
@@ -38,6 +47,15 @@ public class BookMarkController {
|
||||
return bookMarkService.createBookmark(request);
|
||||
}
|
||||
|
||||
@Operation(summary = "Update a bookmark", description = "Update an existing bookmark's properties (title, location, color, etc.).")
|
||||
@ApiResponse(responseCode = "200", description = "Bookmark updated successfully")
|
||||
@PutMapping("/{bookmarkId}")
|
||||
public BookMark updateBookmark(
|
||||
@Parameter(description = "ID of the bookmark to update") @PathVariable Long bookmarkId,
|
||||
@Parameter(description = "Bookmark update request") @Valid @RequestBody UpdateBookMarkRequest request) {
|
||||
return bookMarkService.updateBookmark(bookmarkId, request);
|
||||
}
|
||||
|
||||
@Operation(summary = "Delete a bookmark", description = "Delete a specific bookmark by its ID.")
|
||||
@ApiResponse(responseCode = "204", description = "Bookmark deleted successfully")
|
||||
@DeleteMapping("/{bookmarkId}")
|
||||
|
||||
@@ -7,5 +7,8 @@ import org.mapstruct.Mapping;
|
||||
|
||||
@Mapper(componentModel = "spring")
|
||||
public interface BookMarkMapper {
|
||||
|
||||
@Mapping(source = "book.id", target = "bookId")
|
||||
@Mapping(source = "user.id", target = "userId")
|
||||
BookMark toDto(BookMarkEntity entity);
|
||||
}
|
||||
|
||||
@@ -13,8 +13,13 @@ import java.time.LocalDateTime;
|
||||
@AllArgsConstructor
|
||||
public class BookMark {
|
||||
private Long id;
|
||||
private Long userId;
|
||||
private Long bookId;
|
||||
private String cfi;
|
||||
private String title;
|
||||
private String color;
|
||||
private String notes;
|
||||
private Integer priority;
|
||||
private LocalDateTime createdAt;
|
||||
private LocalDateTime updatedAt;
|
||||
}
|
||||
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.adityachandel.booklore.model.dto;
|
||||
|
||||
import jakarta.validation.constraints.Max;
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.Pattern;
|
||||
import jakarta.validation.constraints.Size;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class UpdateBookMarkRequest {
|
||||
|
||||
@Size(max = 255, message = "Title must not exceed 255 characters")
|
||||
private String title;
|
||||
|
||||
@Size(max = 500, message = "CFI must not exceed 500 characters")
|
||||
private String cfi;
|
||||
|
||||
@Pattern(regexp = "^#[0-9A-Fa-f]{6}$", message = "Color must be a valid hex color (e.g., #FF5733)")
|
||||
private String color;
|
||||
|
||||
@Size(max = 2000, message = "Notes must not exceed 2000 characters")
|
||||
private String notes;
|
||||
|
||||
@Min(value = 1, message = "Priority must be at least 1")
|
||||
@Max(value = 5, message = "Priority must not exceed 5")
|
||||
private Integer priority;
|
||||
}
|
||||
+18
@@ -3,6 +3,7 @@ package com.adityachandel.booklore.model.entity;
|
||||
import jakarta.persistence.*;
|
||||
import lombok.*;
|
||||
import org.hibernate.annotations.CreationTimestamp;
|
||||
import org.hibernate.annotations.UpdateTimestamp;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@@ -39,7 +40,24 @@ public class BookMarkEntity {
|
||||
@Column(name = "title")
|
||||
private String title;
|
||||
|
||||
@Column(name = "color")
|
||||
private String color;
|
||||
|
||||
@Column(name = "notes", length = 2000)
|
||||
private String notes;
|
||||
|
||||
@Column(name = "priority")
|
||||
private Integer priority;
|
||||
|
||||
@jakarta.persistence.Version
|
||||
@Column(name = "version", nullable = false)
|
||||
private Long version;
|
||||
|
||||
@CreationTimestamp
|
||||
@Column(name = "created_at", nullable = false, updatable = false)
|
||||
private LocalDateTime createdAt;
|
||||
|
||||
@UpdateTimestamp
|
||||
@Column(name = "updated_at", nullable = false)
|
||||
private LocalDateTime updatedAt;
|
||||
}
|
||||
|
||||
+10
-4
@@ -9,11 +9,17 @@ import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface BookMarkRepository extends JpaRepository<BookMarkEntity, Long> {
|
||||
|
||||
|
||||
Optional<BookMarkEntity> findByIdAndUserId(Long id, Long userId);
|
||||
|
||||
@Query("SELECT b FROM BookMarkEntity b WHERE b.bookId = :bookId AND b.userId = :userId ORDER BY b.createdAt DESC")
|
||||
List<BookMarkEntity> findByBookIdAndUserIdOrderByCreatedAtDesc(@Param("bookId") Long bookId, @Param("userId") Long userId);
|
||||
|
||||
@Query("SELECT b FROM BookMarkEntity b WHERE b.bookId = :bookId AND b.userId = :userId ORDER BY b.priority ASC, b.createdAt DESC")
|
||||
List<BookMarkEntity> findByBookIdAndUserIdOrderByPriorityAscCreatedAtDesc(@Param("bookId") Long bookId, @Param("userId") Long userId);
|
||||
|
||||
boolean existsByCfiAndBookIdAndUserId(String cfi, Long bookId, Long userId);
|
||||
|
||||
@Query("SELECT COUNT(b) > 0 FROM BookMarkEntity b WHERE b.cfi = :cfi AND b.bookId = :bookId AND b.userId = :userId AND b.id != :excludeId")
|
||||
boolean existsByCfiAndBookIdAndUserIdExcludeId(@Param("cfi") String cfi, @Param("bookId") Long bookId, @Param("userId") Long userId, @Param("excludeId") Long excludeId);
|
||||
|
||||
// New: count bookmarks per book
|
||||
long countByBookIdAndUserId(Long bookId, Long userId);
|
||||
}
|
||||
|
||||
+89
-25
@@ -1,8 +1,10 @@
|
||||
package com.adityachandel.booklore.service.book;
|
||||
|
||||
import com.adityachandel.booklore.config.BookmarkProperties;
|
||||
import com.adityachandel.booklore.mapper.BookMarkMapper;
|
||||
import com.adityachandel.booklore.model.dto.BookMark;
|
||||
import com.adityachandel.booklore.model.dto.CreateBookMarkRequest;
|
||||
import com.adityachandel.booklore.model.dto.UpdateBookMarkRequest;
|
||||
import com.adityachandel.booklore.model.entity.BookEntity;
|
||||
import com.adityachandel.booklore.model.entity.BookLoreUserEntity;
|
||||
import com.adityachandel.booklore.model.entity.BookMarkEntity;
|
||||
@@ -10,63 +12,125 @@ import com.adityachandel.booklore.repository.BookMarkRepository;
|
||||
import com.adityachandel.booklore.repository.BookRepository;
|
||||
import com.adityachandel.booklore.repository.UserRepository;
|
||||
import com.adityachandel.booklore.config.security.service.AuthenticationService;
|
||||
import com.adityachandel.booklore.exception.APIException;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.stereotype.Service;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class BookMarkService {
|
||||
|
||||
private final BookMarkRepository bookMarkRepository;
|
||||
private final BookRepository bookRepository;
|
||||
private final UserRepository userRepository;
|
||||
private final BookMarkMapper mapper;
|
||||
private final AuthenticationService authenticationService;
|
||||
private final BookMarkMapper mapper;
|
||||
private final BookmarkProperties bookmarkProperties;
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<BookMark> getBookmarksForBook(Long bookId) {
|
||||
Long userId = authenticationService.getAuthenticatedUser().getId();
|
||||
return bookMarkRepository.findByBookIdAndUserIdOrderByCreatedAtDesc(bookId, userId)
|
||||
Long userId = getCurrentUserId();
|
||||
return bookMarkRepository.findByBookIdAndUserIdOrderByPriorityAscCreatedAtDesc(bookId, userId)
|
||||
.stream()
|
||||
.map(mapper::toDto)
|
||||
.toList();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public BookMark getBookmarkById(Long bookmarkId) {
|
||||
return mapper.toDto(findBookmarkByIdAndUser(bookmarkId));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public BookMark createBookmark(CreateBookMarkRequest request) {
|
||||
Long userId = authenticationService.getAuthenticatedUser().getId();
|
||||
|
||||
// Check for existing bookmark
|
||||
if (bookMarkRepository.existsByCfiAndBookIdAndUserId(request.getCfi(), request.getBookId(), userId)) {
|
||||
throw new IllegalArgumentException("Bookmark already exists at this location");
|
||||
}
|
||||
|
||||
BookLoreUserEntity currentUser = userRepository.findById(userId)
|
||||
.orElseThrow(() -> new EntityNotFoundException("User not found: " + userId));
|
||||
|
||||
BookEntity book = bookRepository.findById(request.getBookId())
|
||||
.orElseThrow(() -> new EntityNotFoundException("Book not found: " + request.getBookId()));
|
||||
Long userId = getCurrentUserId();
|
||||
validateNoDuplicateBookmark(request.getCfi(), request.getBookId(), userId);
|
||||
|
||||
BookMarkEntity entity = BookMarkEntity.builder()
|
||||
.user(currentUser)
|
||||
.book(book)
|
||||
BookMarkEntity bookmark = BookMarkEntity.builder()
|
||||
.cfi(request.getCfi())
|
||||
.title(request.getTitle())
|
||||
.book(findBook(request.getBookId()))
|
||||
.user(findUser(userId))
|
||||
.priority(bookmarkProperties.getDefaultPriority())
|
||||
.build();
|
||||
|
||||
BookMarkEntity saved = bookMarkRepository.save(entity);
|
||||
return mapper.toDto(saved);
|
||||
log.info("Creating bookmark for book {} by user {}", request.getBookId(), userId);
|
||||
return mapper.toDto(bookMarkRepository.save(bookmark));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public BookMark updateBookmark(Long bookmarkId, UpdateBookMarkRequest request) {
|
||||
BookMarkEntity bookmark = findBookmarkByIdAndUser(bookmarkId);
|
||||
|
||||
// Validate CFI uniqueness if CFI is being updated
|
||||
if (request.getCfi() != null) {
|
||||
validateNoDuplicateBookmark(request.getCfi(), bookmark.getBookId(), bookmark.getUserId(), bookmarkId);
|
||||
}
|
||||
|
||||
applyUpdates(bookmark, request);
|
||||
|
||||
log.info("Updating bookmark {}", bookmarkId);
|
||||
return mapper.toDto(bookMarkRepository.save(bookmark));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteBookmark(Long bookmarkId) {
|
||||
Long userId = authenticationService.getAuthenticatedUser().getId();
|
||||
BookMarkEntity bookmark = bookMarkRepository.findByIdAndUserId(bookmarkId, userId)
|
||||
.orElseThrow(() -> new EntityNotFoundException("Bookmark not found: " + bookmarkId));
|
||||
BookMarkEntity bookmark = findBookmarkByIdAndUser(bookmarkId);
|
||||
log.info("Deleting bookmark {}", bookmarkId);
|
||||
bookMarkRepository.delete(bookmark);
|
||||
}
|
||||
|
||||
private Long getCurrentUserId() {
|
||||
return authenticationService.getAuthenticatedUser().getId();
|
||||
}
|
||||
|
||||
private BookMarkEntity findBookmarkByIdAndUser(Long bookmarkId) {
|
||||
Long userId = getCurrentUserId();
|
||||
return bookMarkRepository.findByIdAndUserId(bookmarkId, userId)
|
||||
.orElseThrow(() -> new EntityNotFoundException("Bookmark not found: " + bookmarkId));
|
||||
}
|
||||
|
||||
private BookEntity findBook(Long bookId) {
|
||||
return bookRepository.findById(bookId)
|
||||
.orElseThrow(() -> new EntityNotFoundException("Book not found: " + bookId));
|
||||
}
|
||||
|
||||
private BookLoreUserEntity findUser(Long userId) {
|
||||
return userRepository.findById(userId)
|
||||
.orElseThrow(() -> new EntityNotFoundException("User not found: " + userId));
|
||||
}
|
||||
|
||||
private void validateNoDuplicateBookmark(String cfi, Long bookId, Long userId) {
|
||||
validateNoDuplicateBookmark(cfi, bookId, userId, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Priority: 1 (highest/most important) to 5 (lowest/least important).
|
||||
* Bookmarks are sorted by priority ascending (1 first), then by creation date descending.
|
||||
*/
|
||||
private void validateNoDuplicateBookmark(String cfi, Long bookId, Long userId, Long excludeBookmarkId) {
|
||||
boolean exists = (excludeBookmarkId == null)
|
||||
? bookMarkRepository.existsByCfiAndBookIdAndUserId(cfi, bookId, userId)
|
||||
: bookMarkRepository.existsByCfiAndBookIdAndUserIdExcludeId(cfi, bookId, userId, excludeBookmarkId);
|
||||
|
||||
if (exists) {
|
||||
throw new APIException("Bookmark already exists at this location", HttpStatus.CONFLICT);
|
||||
}
|
||||
}
|
||||
|
||||
private void applyUpdates(BookMarkEntity bookmark, UpdateBookMarkRequest request) {
|
||||
Optional.ofNullable(request.getTitle()).ifPresent(bookmark::setTitle);
|
||||
Optional.ofNullable(request.getCfi()).ifPresent(bookmark::setCfi);
|
||||
Optional.ofNullable(request.getColor()).ifPresent(bookmark::setColor);
|
||||
Optional.ofNullable(request.getNotes()).ifPresent(bookmark::setNotes);
|
||||
Optional.ofNullable(request.getPriority()).ifPresent(bookmark::setPriority);
|
||||
}
|
||||
}
|
||||
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
ALTER TABLE book_marks
|
||||
ADD COLUMN color VARCHAR(7) DEFAULT NULL,
|
||||
ADD COLUMN notes VARCHAR(2000) DEFAULT NULL,
|
||||
ADD COLUMN priority INTEGER DEFAULT NULL,
|
||||
ADD COLUMN updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
ADD COLUMN version BIGINT NOT NULL DEFAULT 1;
|
||||
|
||||
-- Update existing records
|
||||
UPDATE book_marks SET updated_at = created_at WHERE updated_at IS NULL;
|
||||
@@ -0,0 +1,2 @@
|
||||
CREATE INDEX idx_bookmark_book_user_priority
|
||||
ON book_marks(book_id, user_id, priority, created_at);
|
||||
+79
-9
@@ -1,5 +1,6 @@
|
||||
package com.adityachandel.booklore.service;
|
||||
|
||||
import com.adityachandel.booklore.config.BookmarkProperties;
|
||||
import com.adityachandel.booklore.config.security.service.AuthenticationService;
|
||||
import com.adityachandel.booklore.mapper.BookMarkMapper;
|
||||
import com.adityachandel.booklore.model.dto.BookLoreUser;
|
||||
@@ -40,6 +41,8 @@ class BookMarkServiceTest {
|
||||
private BookMarkMapper mapper;
|
||||
@Mock
|
||||
private AuthenticationService authenticationService;
|
||||
@Mock
|
||||
private BookmarkProperties bookmarkProperties;
|
||||
|
||||
@InjectMocks
|
||||
private BookMarkService bookMarkService;
|
||||
@@ -58,14 +61,14 @@ class BookMarkServiceTest {
|
||||
userDto = BookLoreUser.builder().id(userId).build();
|
||||
userEntity = BookLoreUserEntity.builder().id(userId).build();
|
||||
bookEntity = BookEntity.builder().id(bookId).build();
|
||||
bookmarkEntity = BookMarkEntity.builder().id(bookmarkId).user(userEntity).book(bookEntity).cfi("cfi").title("title").build();
|
||||
bookmarkEntity = BookMarkEntity.builder().id(bookmarkId).user(userEntity).book(bookEntity).cfi("cfi").title("title").version(1L).build();
|
||||
bookmarkDto = BookMark.builder().id(bookmarkId).bookId(bookId).cfi("cfi").title("title").build();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getBookmarksForBook_Success() {
|
||||
when(authenticationService.getAuthenticatedUser()).thenReturn(userDto);
|
||||
when(bookMarkRepository.findByBookIdAndUserIdOrderByCreatedAtDesc(bookId, userId)).thenReturn(List.of(bookmarkEntity));
|
||||
when(bookMarkRepository.findByBookIdAndUserIdOrderByPriorityAscCreatedAtDesc(bookId, userId)).thenReturn(List.of(bookmarkEntity));
|
||||
when(mapper.toDto(bookmarkEntity)).thenReturn(bookmarkDto);
|
||||
|
||||
List<BookMark> result = bookMarkService.getBookmarksForBook(bookId);
|
||||
@@ -73,7 +76,7 @@ class BookMarkServiceTest {
|
||||
assertNotNull(result);
|
||||
assertEquals(1, result.size());
|
||||
assertEquals(bookmarkId, result.get(0).getId());
|
||||
verify(bookMarkRepository).findByBookIdAndUserIdOrderByCreatedAtDesc(bookId, userId);
|
||||
verify(bookMarkRepository).findByBookIdAndUserIdOrderByPriorityAscCreatedAtDesc(bookId, userId);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -81,6 +84,7 @@ class BookMarkServiceTest {
|
||||
CreateBookMarkRequest request = new CreateBookMarkRequest(bookId, "new-cfi", "New Bookmark");
|
||||
|
||||
when(authenticationService.getAuthenticatedUser()).thenReturn(userDto);
|
||||
when(bookmarkProperties.getDefaultPriority()).thenReturn(3);
|
||||
when(bookMarkRepository.existsByCfiAndBookIdAndUserId("new-cfi", bookId, userId)).thenReturn(false);
|
||||
when(userRepository.findById(userId)).thenReturn(Optional.of(userEntity));
|
||||
when(bookRepository.findById(bookId)).thenReturn(Optional.of(bookEntity));
|
||||
@@ -94,16 +98,26 @@ class BookMarkServiceTest {
|
||||
verify(bookMarkRepository).save(any(BookMarkEntity.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createBookmark_Duplicate() {
|
||||
CreateBookMarkRequest request = new CreateBookMarkRequest(bookId, "new-cfi", "New Bookmark");
|
||||
|
||||
when(authenticationService.getAuthenticatedUser()).thenReturn(userDto);
|
||||
when(bookMarkRepository.existsByCfiAndBookIdAndUserId("new-cfi", bookId, userId)).thenReturn(true); // Duplicate exists
|
||||
|
||||
assertThrows(com.adityachandel.booklore.exception.APIException.class, () -> bookMarkService.createBookmark(request));
|
||||
verify(bookMarkRepository, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createBookmark_BookNotFound() {
|
||||
CreateBookMarkRequest request = new CreateBookMarkRequest(bookId, "new-cfi", "New Bookmark");
|
||||
|
||||
when(authenticationService.getAuthenticatedUser()).thenReturn(userDto);
|
||||
when(bookMarkRepository.existsByCfiAndBookIdAndUserId("new-cfi", bookId, userId)).thenReturn(false);
|
||||
when(userRepository.findById(userId)).thenReturn(Optional.of(userEntity));
|
||||
when(bookRepository.findById(bookId)).thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(EntityNotFoundException.class, () -> bookMarkService.createBookmark(request));
|
||||
when(authenticationService.getAuthenticatedUser()).thenReturn(userDto);
|
||||
when(bookMarkRepository.existsByCfiAndBookIdAndUserId("new-cfi", bookId, userId)).thenReturn(false); // No duplicate
|
||||
when(bookRepository.findById(bookId)).thenReturn(Optional.empty()); // Book doesn't exist
|
||||
|
||||
assertThrows(jakarta.persistence.EntityNotFoundException.class, () -> bookMarkService.createBookmark(request));
|
||||
verify(bookMarkRepository, never()).save(any());
|
||||
}
|
||||
|
||||
@@ -125,4 +139,60 @@ class BookMarkServiceTest {
|
||||
assertThrows(EntityNotFoundException.class, () -> bookMarkService.deleteBookmark(bookmarkId));
|
||||
verify(bookMarkRepository, never()).delete(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateBookmark_Success() {
|
||||
var updateRequest = com.adityachandel.booklore.model.dto.UpdateBookMarkRequest.builder()
|
||||
.title("Updated Title")
|
||||
.color("#FF0000")
|
||||
.notes("Updated notes")
|
||||
.priority(3)
|
||||
.build();
|
||||
|
||||
when(authenticationService.getAuthenticatedUser()).thenReturn(userDto);
|
||||
when(bookMarkRepository.findByIdAndUserId(bookmarkId, userId)).thenReturn(Optional.of(bookmarkEntity));
|
||||
when(bookMarkRepository.save(any(BookMarkEntity.class))).thenReturn(bookmarkEntity);
|
||||
when(mapper.toDto(bookmarkEntity)).thenReturn(bookmarkDto);
|
||||
|
||||
BookMark result = bookMarkService.updateBookmark(bookmarkId, updateRequest);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(bookmarkId, result.getId());
|
||||
verify(bookMarkRepository).save(any(BookMarkEntity.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateBookmark_NotFound() {
|
||||
var updateRequest = com.adityachandel.booklore.model.dto.UpdateBookMarkRequest.builder()
|
||||
.title("Updated Title")
|
||||
.build();
|
||||
|
||||
when(authenticationService.getAuthenticatedUser()).thenReturn(userDto);
|
||||
when(bookMarkRepository.findByIdAndUserId(bookmarkId, userId)).thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(EntityNotFoundException.class, () -> bookMarkService.updateBookmark(bookmarkId, updateRequest));
|
||||
verify(bookMarkRepository, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getBookmarkById_Success() {
|
||||
when(authenticationService.getAuthenticatedUser()).thenReturn(userDto);
|
||||
when(bookMarkRepository.findByIdAndUserId(bookmarkId, userId)).thenReturn(Optional.of(bookmarkEntity));
|
||||
when(mapper.toDto(bookmarkEntity)).thenReturn(bookmarkDto);
|
||||
|
||||
BookMark result = bookMarkService.getBookmarkById(bookmarkId);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(bookmarkId, result.getId());
|
||||
verify(bookMarkRepository).findByIdAndUserId(bookmarkId, userId);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getBookmarkById_NotFound() {
|
||||
when(authenticationService.getAuthenticatedUser()).thenReturn(userDto);
|
||||
when(bookMarkRepository.findByIdAndUserId(bookmarkId, userId)).thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(EntityNotFoundException.class, () -> bookMarkService.getBookmarkById(bookmarkId));
|
||||
verify(bookMarkRepository).findByIdAndUserId(bookmarkId, userId);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user