Refactor(api): rename and organize endpoints in BookController
- Renamed endpoints for improved clarity and consistency. - Grouped similar endpoints logically for better structure. - Updated request/response patterns for maintainability.
This commit is contained in:
+15
-22
@@ -1,7 +1,6 @@
|
||||
package com.adityachandel.booklore.controller;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.Book;
|
||||
import com.adityachandel.booklore.model.dto.BookViewerSetting;
|
||||
import com.adityachandel.booklore.model.dto.BookViewerSettings;
|
||||
import com.adityachandel.booklore.model.dto.request.ReadProgressRequest;
|
||||
import com.adityachandel.booklore.model.dto.request.ShelvesAssignmentRequest;
|
||||
@@ -15,27 +14,21 @@ import org.springframework.web.bind.annotation.*;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
@RequestMapping("/api/v1/book")
|
||||
@RequestMapping("/api/v1/books")
|
||||
@RestController
|
||||
@AllArgsConstructor
|
||||
public class BookController {
|
||||
|
||||
private BooksService booksService;
|
||||
|
||||
@GetMapping("/{bookId}")
|
||||
public ResponseEntity<Book> getBook(@PathVariable long bookId, @RequestParam(required = false, defaultValue = "false") boolean withDescription) {
|
||||
return ResponseEntity.ok(booksService.getBook(bookId, withDescription));
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public ResponseEntity<List<Book>> getBooks(@RequestParam(required = false, defaultValue = "false") boolean withDescription) {
|
||||
return ResponseEntity.ok(booksService.getBooks(withDescription));
|
||||
}
|
||||
|
||||
@GetMapping("/search")
|
||||
public ResponseEntity<List<Book>> searchBooks(@RequestParam String title) {
|
||||
List<Book> books = booksService.search(title);
|
||||
return ResponseEntity.ok(books);
|
||||
@GetMapping("/{bookId}")
|
||||
public ResponseEntity<Book> getBook(@PathVariable long bookId, @RequestParam(required = false, defaultValue = "false") boolean withDescription) {
|
||||
return ResponseEntity.ok(booksService.getBook(bookId, withDescription));
|
||||
}
|
||||
|
||||
@GetMapping("/{bookId}/cover")
|
||||
@@ -43,23 +36,28 @@ public class BookController {
|
||||
return ResponseEntity.ok(booksService.getBookCover(bookId));
|
||||
}
|
||||
|
||||
@GetMapping("/{bookId}/data")
|
||||
public ResponseEntity<byte[]> getBookData(@PathVariable long bookId) throws IOException {
|
||||
return booksService.getBookData(bookId);
|
||||
@GetMapping("/{bookId}/content")
|
||||
public ResponseEntity<byte[]> getBookContent(@PathVariable long bookId) throws IOException {
|
||||
return booksService.getBookContent(bookId);
|
||||
}
|
||||
|
||||
@GetMapping("/{bookId}/book-viewer-setting")
|
||||
@GetMapping("/{bookId}/download")
|
||||
public ResponseEntity<Resource> downloadFile(@PathVariable("bookId") Long bookId) {
|
||||
return booksService.prepareFileForDownload(bookId);
|
||||
}
|
||||
|
||||
@GetMapping("/{bookId}/viewer-settings")
|
||||
public ResponseEntity<BookViewerSettings> getBookViewerSettings(@PathVariable long bookId) {
|
||||
return ResponseEntity.ok(booksService.getBookViewerSetting(bookId));
|
||||
}
|
||||
|
||||
@PutMapping("/{bookId}/book-viewer-setting")
|
||||
@PutMapping("/{bookId}/viewer-settings")
|
||||
public ResponseEntity<Void> updateBookViewerSettings(@RequestBody BookViewerSettings bookViewerSettings, @PathVariable long bookId) {
|
||||
booksService.updateBookViewerSetting(bookId, bookViewerSettings);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
@PostMapping("/assign-shelves")
|
||||
@PostMapping("/shelves")
|
||||
public ResponseEntity<List<Book>> addBookToShelf(@RequestBody @Valid ShelvesAssignmentRequest request) {
|
||||
return ResponseEntity.ok(booksService.assignShelvesToBooks(request.getBookIds(), request.getShelvesToAssign(), request.getShelvesToUnassign()));
|
||||
}
|
||||
@@ -69,9 +67,4 @@ public class BookController {
|
||||
booksService.updateReadProgress(request);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
@GetMapping("/{bookId}/download")
|
||||
public ResponseEntity<Resource> downloadFile(@PathVariable("bookId") Long bookId) {
|
||||
return booksService.prepareFileForDownload(bookId);
|
||||
}
|
||||
}
|
||||
@@ -98,7 +98,7 @@ public class BooksService {
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public ResponseEntity<byte[]> getBookData(long bookId) throws IOException {
|
||||
public ResponseEntity<byte[]> getBookContent(long bookId) throws IOException {
|
||||
BookEntity bookEntity = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
|
||||
byte[] pdfBytes = Files.readAllBytes(new File(FileUtils.getBookFullPath(bookEntity)).toPath());
|
||||
return ResponseEntity.ok()
|
||||
|
||||
Reference in New Issue
Block a user