Revamp state management

This commit is contained in:
aditya.chandel
2024-12-22 11:57:06 -07:00
parent e343e7e7f3
commit 4704d9f5f0
17 changed files with 624 additions and 419 deletions

View File

@@ -30,6 +30,11 @@ public class ShelfController {
return ResponseEntity.ok(shelfService.getShelf(shelfId));
}
@GetMapping("/{shelfId}/books")
public ResponseEntity<List<BookDTO>> getShelfBooks(@PathVariable Long shelfId) {
return ResponseEntity.ok(shelfService.getShelfBooks(shelfId));
}
@PostMapping
public ResponseEntity<ShelfDTO> createShelf(@Valid @RequestBody ShelfCreateRequest request) {
return ResponseEntity.status(HttpStatus.CREATED).body(shelfService.createShelf(request));

View File

@@ -33,5 +33,8 @@ public interface BookRepository extends JpaRepository<Book, Long>, JpaSpecificat
Optional<Book> findFirstByLibraryIdAndIdLessThanOrderByIdDesc(Long libraryId, Long currentBookId);
Optional<Book> findFirstByLibraryIdAndIdGreaterThanOrderByIdAsc(Long libraryId, Long currentBookId);
@Query("SELECT b FROM Book b JOIN b.shelves s WHERE s.id = :shelfId")
List<Book> findByShelfId(@Param("shelfId") Long shelfId);
}

View File

@@ -75,4 +75,10 @@ public class ShelfService {
shelfRepository.findById(shelfId).orElseThrow(() -> ApiError.SHELF_NOT_FOUND.createException(shelfId));
shelfRepository.deleteById(shelfId);
}
public List<BookDTO> getShelfBooks(Long shelfId) {
shelfRepository.findById(shelfId).orElseThrow(() -> ApiError.SHELF_NOT_FOUND.createException(shelfId));
List<Book> books = bookRepository.findByShelfId(shelfId);
return books.stream().map(BookTransformer::convertToBookDTO).toList();
}
}