Skip to content

Commit a69d9ed

Browse files
committed
Add deleteBookById() with idempotency; Update tests to use findById()
1 parent c4e0725 commit a69d9ed

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/main/java/com/codesungrape/hmcts/bookapi/service/BookService.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@
22

33
import com.codesungrape.hmcts.bookapi.dto.BookRequest;
44
import com.codesungrape.hmcts.bookapi.entity.Book;
5+
import com.codesungrape.hmcts.bookapi.exception.ResourceNotFoundException;
56
import com.codesungrape.hmcts.bookapi.repository.BookRepository;
67
import lombok.RequiredArgsConstructor;
78
import org.springframework.stereotype.Service;
89

10+
import java.util.UUID;
11+
912
/**
1013
* Service layer responsible for all business logic related to the Book resource.
1114
*/
1215
@Service
1316
@RequiredArgsConstructor // Lombok creates constructor for dependency injection
1417
public class BookService {
1518

16-
// Create a field to store the repo
19+
// Create a field to store the repo in this scope to be accessed and used/reused by methods below
1720
private final BookRepository bookRepository;
1821

1922
/**
@@ -54,4 +57,18 @@ public Book createBook(BookRequest request) {
5457

5558
return savedBook;
5659
}
60+
61+
// Soft Delete
62+
public void deleteBookById(UUID bookId) {
63+
64+
// find the book only if it's not already soft-deleted
65+
Book book = bookRepository.findById(bookId)
66+
.orElseThrow(() -> new ResourceNotFoundException("Book not found"));
67+
68+
// Idempotent way to mark soft-delete and save
69+
if (!book.isDeleted()) {
70+
book.setDeleted(true);
71+
bookRepository.save(book);
72+
}
73+
}
5774
}

src/test/java/com/codesungrape/hmcts/bookapi/BookServiceTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ void testDelete_Book_ShouldThrowException_WhenIdNotFound() {
283283

284284
// Arrange: As goal is to test what happens when the resource doesn't exist,
285285
// we intentionally simulate DB returning NO result
286-
when(testBookRepository.findByIdAndDeletedFalse(testId)).thenReturn(Optional.empty());
286+
when(testBookRepository.findById(testId)).thenReturn(Optional.empty());
287287

288288
// ACT and ASSERT: throw ResourceNotFoundException when calling the delete method.
289289
assertThrows(
@@ -303,7 +303,7 @@ void testDeleteBookById_Success() {
303303
// Arrange:
304304
persistedBook.setDeleted(false); // ensure starting state
305305

306-
when(testBookRepository.findByIdAndDeletedFalse(testId))
306+
when(testBookRepository.findById(testId))
307307
.thenReturn(Optional.of(persistedBook));
308308

309309
when(testBookRepository.save(any(Book.class)))
@@ -316,7 +316,7 @@ void testDeleteBookById_Success() {
316316
assertTrue(persistedBook.isDeleted());
317317

318318
// Assert: repository methods were called correctly
319-
verify(testBookRepository, times(1)).findByIdAndDeletedFalse(testId);
319+
verify(testBookRepository, times(1)).findById(testId);
320320
verify(testBookRepository, times(1)).save(persistedBook);
321321
}
322322
}

0 commit comments

Comments
 (0)