File tree Expand file tree Collapse file tree 2 files changed +21
-4
lines changed
main/java/com/codesungrape/hmcts/bookapi/service
test/java/com/codesungrape/hmcts/bookapi Expand file tree Collapse file tree 2 files changed +21
-4
lines changed Original file line number Diff line number Diff line change 22
33import com .codesungrape .hmcts .bookapi .dto .BookRequest ;
44import com .codesungrape .hmcts .bookapi .entity .Book ;
5+ import com .codesungrape .hmcts .bookapi .exception .ResourceNotFoundException ;
56import com .codesungrape .hmcts .bookapi .repository .BookRepository ;
67import lombok .RequiredArgsConstructor ;
78import 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
1417public 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}
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments