Add deleting books

This commit is contained in:
2025-02-16 22:33:52 +03:00
parent 3d6496e26e
commit 0d7e1b76d3
6 changed files with 25 additions and 19 deletions
@@ -1,7 +1,6 @@
package ru.cathub.telegabot.bot;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboardMarkup;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton;
@@ -10,7 +9,6 @@ import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.Keyboard
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardRow;
import ru.cathub.telegabot.model.Book;
import ru.cathub.telegabot.model.BotUser;
import ru.cathub.telegabot.repository.BookRepository;
import java.util.ArrayList;
import java.util.List;
@@ -78,7 +76,7 @@ public class KeyboardHelper {
row2.add(new KeyboardButton(EDIT_BOOKS));
KeyboardRow row3 = new KeyboardRow();
row3.add(new KeyboardButton(SET_BOOKS_GOAL2));
row3.add(new KeyboardButton(SET_BOOKS_GOAL_FROM_BOOKS));
row3.add(new KeyboardButton(BACK));
keyboardRows.add(row1);
@@ -177,29 +175,27 @@ public class KeyboardHelper {
.callbackData("BOOK_EDIT_RATING")
.build()
);
navRow.add(
rows.add(navRow);
// Новая строка с кнопкой удаления
InlineKeyboardRow actionRow = new InlineKeyboardRow();
actionRow.add(
InlineKeyboardButton.builder()
.text("Дата \uD83D\uDCC5")
.callbackData("BOOK_EDIT_DATE")
.build()
);
rows.add(navRow);
// Новая строка с кнопкой удаления
InlineKeyboardRow actionRow = new InlineKeyboardRow();
actionRow.add(
InlineKeyboardButton.builder()
.text("Удалить книгу \uD83D\uDDD1")
.callbackData("BOOK_DELETE")
.build()
);
actionRow.add(
InlineKeyboardButton.builder()
.text("Отмена ❌")
.callbackData("BOOK_EDIT_CANCEL")
.build()
);
rows.add(actionRow);
return new InlineKeyboardMarkup(rows);
@@ -12,6 +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 deleteBook(BotUser user);
void handleRestartMessage(BotUser botUser);
}
@@ -4,6 +4,7 @@ import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import ru.cathub.telegabot.bot.KeyboardHelper;
import ru.cathub.telegabot.exception.ServiceException;
import ru.cathub.telegabot.model.Book;
import ru.cathub.telegabot.model.BookEditStateCache;
import ru.cathub.telegabot.model.BotUser;
@@ -21,6 +22,7 @@ import ru.cathub.telegabot.service.TelegramClientService;
import ru.cathub.telegabot.utils.Constants;
import static ru.cathub.telegabot.bot.KeyboardHelper.getEditOptions;
import static ru.cathub.telegabot.utils.Constants.ERROR_DELETE_BOOKS;
@Service
@RequiredArgsConstructor
@@ -33,7 +35,7 @@ public class BookEditingServiceImpl implements BookEditingService {
private final UserRepository userRepository;
@Override
public void deleteBook(BotUser user) throws ServiceException {
public void deleteBook(BotUser user) {
try {
var bookCache = bookCacheService.getBookEditCache(user.getId());
if (bookCache == null || bookCache.getBookId() == null) {
@@ -55,7 +57,8 @@ public class BookEditingServiceImpl implements BookEditingService {
throw new ServiceException(Constants.ERROR_BOOK_NOT_FOUND);
}
} catch (Exception e) {
throw new ServiceException("Ошибка при удалении книги: " + e.getMessage(), e);
log.error("Error deleting book for user {}", user.getId(), e);
telegramClientService.sendMessage(user,ERROR_DELETE_BOOKS);
}
}
@@ -77,6 +80,7 @@ public class BookEditingServiceImpl implements BookEditingService {
() -> telegramClientService.sendMessage(user, Constants.ERROR_BOOK_NOT_FOUND)
);
} catch (NumberFormatException e) {
log.error("Invalid book ID for user {}", user.getId(), e);
telegramClientService.sendMessage(user, Constants.ERROR_INVALID_BOOK_ID);
}
}
@@ -129,10 +133,12 @@ public class BookEditingServiceImpl implements BookEditingService {
book.setReadDate(parsedDate);
telegramClientService.sendMessage(user, "Дата прочтения обновлена ✅");
} catch (ParseException e) {
log.error("Invalid date format for user {}", user.getId(), e);
throw new IllegalArgumentException("Ошибка формата даты. Используйте ДД.ММ.ГГГГ");
}
break;
default:
log.error("Invalid field name {} for user {}", fieldName, user.getId());
throw new IllegalArgumentException("Invalid field name: " + fieldName);
}
@@ -119,6 +119,9 @@ public class BookManageServiceImpl implements BookManageService {
case "BOOK_EDIT_DATE":
bookEditingService.handleEditDate(user);
break;
case "BOOK_DELETE":
bookEditingService.deleteBook(user);
break;
case "BOOK_EDIT_CANCEL":
// cancelEditing(user);
break;
@@ -50,7 +50,7 @@ public class TelegramBotServiceImpl implements TelegramBotService {
case EDIT_BOOKS:
bookManageService.processBookInput(botUser,text);
break;
case SET_BOOKS_GOAL:
case SET_BOOKS_GOAL_FROM_BOOKS:
profileService.handleBooksGoalUpdate(botUser);
break;
default:
@@ -7,7 +7,7 @@ public class Constants {
// Menu options
public static final String EDIT_PROFILE = "Редактировать профиль";
public static final String SET_BOOKS_GOAL = "Книг для прочтения";
public static final String SET_BOOKS_GOAL2 = "Цель на год";
public static final String SET_BOOKS_GOAL_FROM_BOOKS = "Цель на год";
public static final String PROFILE_UPDATED = "Цель обновлена! \uD83D\uDCD7";
public static final String ADD_BOOK = "Добавить книгу";
public static final String LIST_BOOKS = "Список книг";
@@ -35,6 +35,7 @@ public class Constants {
public static final String ERROR_BOOK_NOT_FOUND = "Книга не найдена";
public static final String ERROR_INVALID_BOOK_ID = "Некорректный ID книги";
public static final String ERROR_LOADING_BOOKS = "❌ Ошибка при загрузке списка книг";
public static final String ERROR_DELETE_BOOKS = "❌ Ошибка при удвлении книги";
public static final String ERROR_NO_ACTION = "❌ Нет текущего действия. Выберите действие из меню или перейдите в начальное меню командой /start";
public static final String ERROR_INVALID_DATE_FORMAT = "Неверный формат даты. Используйте ДД.ММ.ГГГГ";
}