53 lines
1.8 KiB
Java
53 lines
1.8 KiB
Java
package pl.edu.amu.wmi.bookapi.service;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import pl.edu.amu.wmi.bookapi.api.dto.BookDto;
|
|
import pl.edu.amu.wmi.bookapi.exceptions.NoEanCodeDetectedException;
|
|
import pl.edu.amu.wmi.bookapi.models.BookDocument;
|
|
import pl.edu.amu.wmi.bookapi.repositories.BookRepository;
|
|
|
|
import java.util.List;
|
|
|
|
@Service
|
|
public class BookService {
|
|
|
|
BookRepository bookRepository;
|
|
ImageProcessingService imageProcessingService;
|
|
|
|
@Autowired
|
|
public BookService(BookRepository bookRepository, ImageProcessingService imageProcessingService) {
|
|
this.imageProcessingService = imageProcessingService;
|
|
this.bookRepository = bookRepository;
|
|
}
|
|
|
|
public void deleteBook(String userName, String bookId) {
|
|
bookRepository.deleteByIdAndOwnerUsername(bookId, userName);
|
|
}
|
|
|
|
public List<BookDocument> findAllForUser(String userName) {
|
|
return bookRepository.findAllByOwnerUsername(userName);
|
|
}
|
|
|
|
public void saveBook(String userName, BookDto bookDto) {
|
|
bookRepository.save(BookDocument.from(userName, bookDto));
|
|
}
|
|
|
|
public void handleImageUpload(MultipartFile imageFile, String author, String title, String username) throws Exception {
|
|
String detectedEan = imageProcessingService.getDecodedEan(imageFile);
|
|
saveBook(username, new BookDto(detectedEan, author, title));
|
|
if (detectedEan.isEmpty()) {
|
|
throw new NoEanCodeDetectedException();
|
|
}
|
|
}
|
|
|
|
public List<BookDocument> findAll() {
|
|
return bookRepository.findAll();
|
|
}
|
|
|
|
public BookDocument updateBook(String bookId, String userId, BookDto bookDto) {
|
|
return bookRepository.updateBook(bookId, userId, bookDto);
|
|
}
|
|
}
|