- Merge book metadata service to book service
- Standardize API endpoints
This commit is contained in:
+1
-1
@@ -7,7 +7,7 @@ import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RequestMapping("/api/v1/author")
|
||||
@RequestMapping("/api/v1/authors")
|
||||
@RestController
|
||||
@AllArgsConstructor
|
||||
public class AuthorController {
|
||||
|
||||
+4
-3
@@ -7,6 +7,7 @@ import com.adityachandel.booklore.model.dto.request.ShelvesAssignmentRequest;
|
||||
import com.adityachandel.booklore.service.BooksService;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@@ -37,13 +38,13 @@ public class BookController {
|
||||
}
|
||||
|
||||
@GetMapping("/{bookId}/content")
|
||||
public ResponseEntity<byte[]> getBookContent(@PathVariable long bookId) throws IOException {
|
||||
public ResponseEntity<ByteArrayResource> getBookContent(@PathVariable long bookId) throws IOException {
|
||||
return booksService.getBookContent(bookId);
|
||||
}
|
||||
|
||||
@GetMapping("/{bookId}/download")
|
||||
public ResponseEntity<Resource> downloadFile(@PathVariable("bookId") Long bookId) {
|
||||
return booksService.prepareFileForDownload(bookId);
|
||||
public ResponseEntity<Resource> downloadBook(@PathVariable("bookId") Long bookId) {
|
||||
return booksService.downloadBook(bookId);
|
||||
}
|
||||
|
||||
@GetMapping("/{bookId}/viewer-settings")
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
package com.adityachandel.booklore.controller;
|
||||
|
||||
import com.adityachandel.booklore.service.EpubService;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/epub")
|
||||
public class EpubController {
|
||||
|
||||
private final EpubService epubService;
|
||||
|
||||
public EpubController(EpubService epubService) {
|
||||
this.epubService = epubService;
|
||||
}
|
||||
|
||||
@GetMapping("/{bookId}/download")
|
||||
public ResponseEntity<ByteArrayResource> downloadEpub(@PathVariable Long bookId) throws IOException {
|
||||
ByteArrayResource epubFile = epubService.getEpubFile(bookId);
|
||||
return ResponseEntity.ok()
|
||||
.header("Content-Type", "application/epub+zip")
|
||||
.header("Content-Disposition", "attachment; filename=\"book.epub\"")
|
||||
.body(epubFile);
|
||||
}
|
||||
}
|
||||
+7
-10
@@ -2,9 +2,7 @@ package com.adityachandel.booklore.controller;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.Book;
|
||||
import com.adityachandel.booklore.model.dto.Library;
|
||||
import com.adityachandel.booklore.model.dto.Sort;
|
||||
import com.adityachandel.booklore.model.dto.request.CreateLibraryRequest;
|
||||
import com.adityachandel.booklore.service.BooksService;
|
||||
import com.adityachandel.booklore.service.LibraryService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@@ -13,25 +11,24 @@ import org.springframework.web.bind.annotation.*;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/library")
|
||||
@RequestMapping("/api/v1/libraries")
|
||||
@AllArgsConstructor
|
||||
public class LibraryController {
|
||||
|
||||
private LibraryService libraryService;
|
||||
private BooksService booksService;
|
||||
|
||||
@GetMapping("/{libraryId}")
|
||||
public ResponseEntity<Library> getLibrary(@PathVariable long libraryId) {
|
||||
return ResponseEntity.ok(libraryService.getLibrary(libraryId));
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public ResponseEntity<List<Library>> getLibraries() {
|
||||
return ResponseEntity.ok(libraryService.getLibraries());
|
||||
}
|
||||
|
||||
@GetMapping("/{libraryId}")
|
||||
public ResponseEntity<Library> getLibrary(@PathVariable long libraryId) {
|
||||
return ResponseEntity.ok(libraryService.getLibrary(libraryId));
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ResponseEntity<Library> createLibraryNew(@RequestBody CreateLibraryRequest request) {
|
||||
public ResponseEntity<Library> createLibrary(@RequestBody CreateLibraryRequest request) {
|
||||
return ResponseEntity.ok(libraryService.createLibrary(request));
|
||||
}
|
||||
|
||||
|
||||
+9
-14
@@ -17,7 +17,7 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/metadata")
|
||||
@RequestMapping("/api/v1/books")
|
||||
@AllArgsConstructor
|
||||
public class MetadataController {
|
||||
|
||||
@@ -26,35 +26,30 @@ public class MetadataController {
|
||||
private JobSchedulerService jobSchedulerService;
|
||||
private BookMetadataMapper bookMetadataMapper;
|
||||
|
||||
@PostMapping("/{bookId}")
|
||||
public ResponseEntity<List<BookMetadata>> getProspectiveMetadataList(@RequestBody(required = false) FetchMetadataRequest fetchMetadataRequest, @PathVariable Long bookId) {
|
||||
@PostMapping("/{bookId}/metadata/prospective")
|
||||
public ResponseEntity<List<BookMetadata>> getMetadataList(@RequestBody(required = false) FetchMetadataRequest fetchMetadataRequest, @PathVariable Long bookId) {
|
||||
return ResponseEntity.ok(bookMetadataService.getProspectiveMetadataListForBookId(bookId, fetchMetadataRequest));
|
||||
}
|
||||
|
||||
@PutMapping("/{bookId}")
|
||||
public ResponseEntity<BookMetadata> updateMetadataFromFetch(@RequestBody BookMetadata setMetadataRequest, @PathVariable long bookId) {
|
||||
@PutMapping("/{bookId}/metadata")
|
||||
public ResponseEntity<BookMetadata> updateMetadata(@RequestBody BookMetadata setMetadataRequest, @PathVariable long bookId) {
|
||||
BookMetadata bookMetadata = bookMetadataMapper.toBookMetadata(bookMetadataUpdater.setBookMetadata(bookId, setMetadataRequest, true, true), true);
|
||||
return ResponseEntity.ok(bookMetadata);
|
||||
}
|
||||
|
||||
@PutMapping(path = "/{bookId}/refresh-book-sync")
|
||||
public ResponseEntity<BookMetadata> quickRefreshBookSynchronized(@PathVariable Long bookId) {
|
||||
return ResponseEntity.ok(bookMetadataService.quickRefreshBookSynchronized(bookId));
|
||||
}
|
||||
|
||||
@PutMapping(path = "/refreshV2")
|
||||
@PutMapping(path = "/metadata/refresh")
|
||||
public ResponseEntity<String> scheduleRefreshV2(@Validated @RequestBody MetadataRefreshRequest request) {
|
||||
jobSchedulerService.scheduleMetadataRefreshV2(request);
|
||||
jobSchedulerService.scheduleMetadataRefresh(request);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
@PostMapping("/{bookId}/upload-cover")
|
||||
@PostMapping("/{bookId}/metadata/cover")
|
||||
public ResponseEntity<BookMetadata> uploadCover(@PathVariable Long bookId, @RequestParam("file") MultipartFile file) {
|
||||
BookMetadata updatedMetadata = bookMetadataService.handleCoverUpload(bookId, file);
|
||||
return ResponseEntity.ok(updatedMetadata);
|
||||
}
|
||||
|
||||
@PutMapping("/lock")
|
||||
@PutMapping("/{bookId}/metadata/lock")
|
||||
public ResponseEntity<BookMetadata> updateFieldLockState(@RequestBody FieldLockRequest request) {
|
||||
long bookId = request.getBookId();
|
||||
String field = request.getField();
|
||||
|
||||
+11
-11
@@ -15,34 +15,29 @@ import java.util.List;
|
||||
|
||||
@AllArgsConstructor
|
||||
@RestController
|
||||
@RequestMapping(("/api/v1/shelf"))
|
||||
@RequestMapping(("/api/v1/shelves"))
|
||||
public class ShelfController {
|
||||
|
||||
private final ShelfService shelfService;
|
||||
|
||||
@GetMapping
|
||||
public ResponseEntity<List<Shelf>> findAll() {
|
||||
public ResponseEntity<List<Shelf>> getAllShelves() {
|
||||
return ResponseEntity.ok(shelfService.getShelves());
|
||||
}
|
||||
|
||||
@GetMapping("/{shelfId}")
|
||||
public ResponseEntity<Shelf> findById(@PathVariable Long shelfId) {
|
||||
public ResponseEntity<Shelf> getShelf(@PathVariable Long shelfId) {
|
||||
return ResponseEntity.ok(shelfService.getShelf(shelfId));
|
||||
}
|
||||
|
||||
@GetMapping("/{shelfId}/books")
|
||||
public ResponseEntity<List<Book>> getShelfBooks(@PathVariable Long shelfId) {
|
||||
return ResponseEntity.ok(shelfService.getShelfBooks(shelfId));
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ResponseEntity<Shelf> createShelf(@Valid @RequestBody ShelfCreateRequest request) {
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(shelfService.createShelf(request));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public ResponseEntity<Shelf> updateShelf(@PathVariable Long id, @Valid @RequestBody ShelfCreateRequest request) {
|
||||
return ResponseEntity.ok(shelfService.updateShelf(id, request));
|
||||
@PutMapping("/{shelfId}")
|
||||
public ResponseEntity<Shelf> updateShelf(@Valid @RequestBody ShelfCreateRequest request, @PathVariable Long shelfId) {
|
||||
return ResponseEntity.ok(shelfService.updateShelf(shelfId, request));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{shelfId}")
|
||||
@@ -50,4 +45,9 @@ public class ShelfController {
|
||||
shelfService.deleteShelf(shelfId);
|
||||
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
|
||||
}
|
||||
|
||||
@GetMapping("/{shelfId}/books")
|
||||
public ResponseEntity<List<Book>> getShelfBooks(@PathVariable Long shelfId) {
|
||||
return ResponseEntity.ok(shelfService.getShelfBooks(shelfId));
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -20,7 +20,7 @@ public class JobSchedulerService {
|
||||
private final BlockingQueue<MetadataRefreshRequest> jobQueue = new LinkedBlockingQueue<>();
|
||||
private boolean isJobRunning = false;
|
||||
|
||||
public synchronized void scheduleMetadataRefreshV2(MetadataRefreshRequest request) {
|
||||
public synchronized void scheduleMetadataRefresh(MetadataRefreshRequest request) {
|
||||
log.info("Received request to schedule metadata refresh: {}", request);
|
||||
jobQueue.offer(request);
|
||||
log.debug("Added request to job queue. Queue size: {}", jobQueue.size());
|
||||
|
||||
-10
@@ -88,16 +88,6 @@ public class BookMetadataService {
|
||||
return getParser(provider).fetchTopMetadata(book, buildFetchMetadataRequestFromBook(book));
|
||||
}
|
||||
|
||||
|
||||
@Transactional
|
||||
public BookMetadata quickRefreshBookSynchronized(Long bookId) {
|
||||
BookEntity bookEntity = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
|
||||
AppSettings appSettings = appSettingService.getAppSettings();
|
||||
MetadataRefreshRequest refreshRequest = MetadataRefreshRequest.builder().refreshOptions(appSettings.getMetadataRefreshOptions()).build();
|
||||
Map<MetadataProvider, BookMetadata> metadataMap = fetchMetadataForBook(prepareProviders(refreshRequest), bookEntity);
|
||||
return buildFetchMetadata(bookId, refreshRequest, metadataMap);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void refreshMetadata(MetadataRefreshRequest request) {
|
||||
log.info("Refresh Metadata task started!");
|
||||
|
||||
@@ -19,6 +19,7 @@ import com.adityachandel.booklore.util.FileService;
|
||||
import com.adityachandel.booklore.util.FileUtils;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.UrlResource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
@@ -27,9 +28,8 @@ import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.time.Instant;
|
||||
@@ -98,19 +98,6 @@ public class BooksService {
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
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()
|
||||
.header(HttpHeaders.CONTENT_TYPE, "text/plain")
|
||||
.body(pdfBytes);
|
||||
}
|
||||
|
||||
public List<Book> search(String title) {
|
||||
List<BookEntity> bookEntities = bookRepository.findByTitleContainingIgnoreCase(title);
|
||||
return bookEntities.stream().map(bookMapper::toBook).toList();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public List<Book> assignShelvesToBooks(Set<Long> bookIds, Set<Long> shelfIdsToAssign, Set<Long> shelfIdsToUnassign) {
|
||||
List<BookEntity> bookEntities = bookRepository.findAllById(bookIds);
|
||||
@@ -150,21 +137,26 @@ public class BooksService {
|
||||
bookRepository.save(book);
|
||||
}
|
||||
|
||||
public ResponseEntity<Resource> prepareFileForDownload(Long bookId) {
|
||||
public ResponseEntity<Resource> downloadBook(Long bookId) {
|
||||
try {
|
||||
BookEntity bookEntity = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
|
||||
Path file = Paths.get(FileUtils.getBookFullPath(bookEntity)).toAbsolutePath().normalize();
|
||||
Resource resource = new UrlResource(file.toUri());
|
||||
String contentType = Files.probeContentType(file);
|
||||
if (contentType == null) {
|
||||
contentType = "application/octet-stream";
|
||||
}
|
||||
return ResponseEntity.ok()
|
||||
.contentType(MediaType.parseMediaType(contentType))
|
||||
.contentType(MediaType.APPLICATION_OCTET_STREAM)
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getFileName() + "\"")
|
||||
.body(resource);
|
||||
} catch (Exception e) {
|
||||
throw ApiError.FAILED_TO_DOWNLOAD_FILE.createException(bookId);
|
||||
}
|
||||
}
|
||||
|
||||
public ResponseEntity<ByteArrayResource> getBookContent(long bookId) throws IOException {
|
||||
BookEntity bookEntity = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
|
||||
try (FileInputStream inputStream = new FileInputStream(FileUtils.getBookFullPath(bookEntity))) {
|
||||
return ResponseEntity.ok()
|
||||
.contentType(MediaType.APPLICATION_OCTET_STREAM)
|
||||
.body(new ByteArrayResource(inputStream.readAllBytes()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package com.adityachandel.booklore.service;
|
||||
|
||||
import com.adityachandel.booklore.exception.ApiError;
|
||||
import com.adityachandel.booklore.model.entity.BookEntity;
|
||||
import com.adityachandel.booklore.repository.BookRepository;
|
||||
import com.adityachandel.booklore.util.FileUtils;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Service
|
||||
public class EpubService {
|
||||
|
||||
private final BookRepository bookRepository;
|
||||
|
||||
public ByteArrayResource getEpubFile(Long bookId) throws IOException {
|
||||
BookEntity bookEntity = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
|
||||
try (FileInputStream inputStream = new FileInputStream(FileUtils.getBookFullPath(bookEntity))) {
|
||||
byte[] fileContent = inputStream.readAllBytes();
|
||||
return new ByteArrayResource(fileContent);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user