forked from Sanders/TelegaBot
refactor: Centralize constants and improve error handling
This commit is contained in:
@@ -41,10 +41,10 @@ public class BookEditingServiceImpl implements BookEditingService {
|
||||
log.info("Found book with id {}", bookId);
|
||||
telegramClientService.sendMessage(user, "Выберите поле для редактирования:", getEditOptions(user));
|
||||
} else {
|
||||
telegramClientService.sendMessage(user, "Книга не найдена");
|
||||
telegramClientService.sendMessage(user, Constants.ERROR_BOOK_NOT_FOUND);
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
telegramClientService.sendMessage(user, "Некорректный ID книги");
|
||||
telegramClientService.sendMessage(user, Constants.ERROR_INVALID_BOOK_ID);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -25,13 +25,11 @@ public class BookListingServiceImpl implements BookListingService {
|
||||
private final TelegramClientService telegramClientService;
|
||||
private final BookRepository bookRepository;
|
||||
|
||||
private static final String FULL_STAR = "⭐"; // Эмодзи U+2B50
|
||||
private static final String EMPTY_STAR = "☆"; // Символ U+2606
|
||||
|
||||
@Override
|
||||
public void showPaginatedBookList(BotUser botUser, int page, Integer messageId, Boolean forEdit) {
|
||||
try {
|
||||
Pageable pageable = PageRequest.of(page, 5);
|
||||
Pageable pageable = PageRequest.of(page, Constants.PAGE_SIZE);
|
||||
Page<Book> books = bookRepository.findByBotUserOrderByIdAsc(botUser, pageable);
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
@@ -57,8 +55,8 @@ public class BookListingServiceImpl implements BookListingService {
|
||||
bookCacheService.updateBookEditCache(botUser.getId(), BookEditStateCache.builder().bookEditState(BookEditStateCache.BookEditState.CHOOSING_FIELD).build());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Error loading book list", e);
|
||||
telegramClientService.sendMessage(botUser, "❌ Ошибка при загрузке списка книг");
|
||||
log.error("Error loading book list for user {}", botUser.getId(), e);
|
||||
telegramClientService.sendMessage(botUser, Constants.ERROR_LOADING_BOOKS);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,8 +70,7 @@ public class BookListingServiceImpl implements BookListingService {
|
||||
}
|
||||
|
||||
private String getBookIcon(int index) {
|
||||
String[] icons = {"📘", "📙", "📕", "📗", "📓"};
|
||||
return icons[index % icons.length];
|
||||
return Constants.BOOK_ICONS[index % Constants.BOOK_ICONS.length];
|
||||
}
|
||||
|
||||
private String escapeMarkdown(String text) {
|
||||
|
||||
@@ -25,8 +25,6 @@ import static ru.cathub.telegabot.utils.Constants.*;
|
||||
@Slf4j
|
||||
public class BookManageServiceImpl implements BookManageService {
|
||||
|
||||
private static final String FULL_STAR = "⭐"; // Эмодзи U+2B50
|
||||
private static final String EMPTY_STAR = "☆"; // Символ U+2606
|
||||
private final BookCreationService bookCreationService;
|
||||
private final BookEditingService bookEditingService;
|
||||
private final BookListingService bookListingService;
|
||||
@@ -38,7 +36,7 @@ public class BookManageServiceImpl implements BookManageService {
|
||||
|
||||
public static String formatRating(int rating) {
|
||||
// Ограничиваем рейтинг в диапазоне 0-10
|
||||
int validatedRating = Math.min(10, Math.max(0, rating));
|
||||
int validatedRating = Math.min(Constants.MAX_RATING, Math.max(Constants.MIN_RATING, rating));
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
@@ -83,7 +81,7 @@ public class BookManageServiceImpl implements BookManageService {
|
||||
break;
|
||||
default:
|
||||
if (editCache == null || editCache.getBookEditState() == BookEditStateCache.BookEditState.NONE) {
|
||||
telegramClientService.sendMessage(user, "❌ Нет текущего действия. Выберете действие из меню или перейдите в начальное меню командой /start");
|
||||
telegramClientService.sendMessage(user, Constants.ERROR_NO_ACTION);
|
||||
return;
|
||||
}
|
||||
switch (editCache.getBookEditState()) {
|
||||
|
||||
@@ -1,14 +1,35 @@
|
||||
package ru.cathub.telegabot.utils;
|
||||
|
||||
public class Constants{
|
||||
public class Constants {
|
||||
// Commands
|
||||
public static final String START = "/start";
|
||||
|
||||
public static final String BOOK_CACHE_NAME = "bookCache";
|
||||
|
||||
// Menu options
|
||||
public static final String ADD_BOOK = "Добавить книгу";
|
||||
public static final String LIST_BOOKS = "Список книг";
|
||||
public static final String BACK = "Назад";
|
||||
public static final String EDIT_BOOKS = "Редактировать книги";
|
||||
|
||||
// Cache names
|
||||
public static final String BOOK_CACHE_NAME = "bookCache";
|
||||
public static final String BOOK_CACHE_LIST = "bookListCache";
|
||||
public static final String BOOK_CACHE_EDIT = "bookEditCache";
|
||||
|
||||
|
||||
// Rating constants
|
||||
public static final String FULL_STAR = "⭐";
|
||||
public static final String EMPTY_STAR = "☆";
|
||||
public static final int MAX_RATING = 10;
|
||||
public static final int MIN_RATING = 0;
|
||||
|
||||
// Pagination
|
||||
public static final int PAGE_SIZE = 5;
|
||||
|
||||
// Book icons
|
||||
public static final String[] BOOK_ICONS = {"📘", "📙", "📕", "📗", "📓"};
|
||||
|
||||
// Error messages
|
||||
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_NO_ACTION = "❌ Нет текущего действия. Выберите действие из меню или перейдите в начальное меню командой /start";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user