feat: Add book deletion functionality to BookEditingService

This commit is contained in:
2025-02-16 22:15:33 +03:00
parent 82b63c8ab3
commit 3d6496e26e
2 changed files with 28 additions and 0 deletions
@@ -12,5 +12,6 @@ public interface BookEditingService {
void updateBookRating(BotUser user, String newRating);
void handleEditDate(BotUser user);
void updateBookDate(BotUser user, String newDate);
void deleteBook(BotUser user) throws ServiceException;
void handleRestartMessage(BotUser botUser);
}
@@ -32,6 +32,33 @@ public class BookEditingServiceImpl implements BookEditingService {
private final BookRepository bookRepository;
private final UserRepository userRepository;
@Override
public void deleteBook(BotUser user) throws ServiceException {
try {
var bookCache = bookCacheService.getBookEditCache(user.getId());
if (bookCache == null || bookCache.getBookId() == null) {
throw new ServiceException("Не найдена книга для удаления");
}
Long bookId = bookCache.getBookId();
Optional<Book> book = bookRepository.findByIdAndBotUser(bookId, user);
if (book.isPresent()) {
bookRepository.deleteById(bookId);
bookCacheService.clearBookEditCache(user.getId());
telegramClientService.sendMessage(
user,
"Книга успешно удалена \uD83D\uDDD1",
KeyboardHelper.getBookMenuKeyboard()
);
} else {
throw new ServiceException(Constants.ERROR_BOOK_NOT_FOUND);
}
} catch (Exception e) {
throw new ServiceException("Ошибка при удалении книги: " + e.getMessage(), e);
}
}
public void processBookSelection(BotUser user, Long bookId) {
try {
Optional<Book> book = bookRepository.findByIdAndBotUser(bookId, user);