forked from Sanders/TelegaBot
refactor: extract formatting logic to utility class and improve code consistency
This commit is contained in:
@@ -38,12 +38,13 @@ public class BookEditingServiceImpl implements BookEditingService {
|
||||
bookCache = BookEditStateCache.builder().bookEditState(BookEditStateCache.BookEditState.NONE).bookId(bookId).build();
|
||||
}
|
||||
bookCacheService.updateBookEditCache(user.getId(), bookCache);
|
||||
if (book.isPresent()) {
|
||||
log.info("Found book with id {}", bookId);
|
||||
telegramClientService.sendMessage(user, "Выберите поле для редактирования:", getEditOptions(user));
|
||||
} else {
|
||||
telegramClientService.sendMessage(user, Constants.ERROR_BOOK_NOT_FOUND);
|
||||
}
|
||||
book.ifPresentOrElse(
|
||||
b -> {
|
||||
log.info("Found book with id {}", bookId);
|
||||
telegramClientService.sendMessage(user, "Выберите поле для редактирования:", getEditOptions(user));
|
||||
},
|
||||
() -> telegramClientService.sendMessage(user, Constants.ERROR_BOOK_NOT_FOUND)
|
||||
);
|
||||
} catch (NumberFormatException e) {
|
||||
telegramClientService.sendMessage(user, Constants.ERROR_INVALID_BOOK_ID);
|
||||
}
|
||||
|
||||
@@ -65,37 +65,6 @@ public class BookListingServiceImpl implements BookListingService {
|
||||
}
|
||||
|
||||
private String formatBookEntry(Book book, int index, boolean forEdit) {
|
||||
if (forEdit) {
|
||||
return String.format("%s %s *%s*\n" + "✍️ _%s_\n" + "🏆 %s\n" +
|
||||
"▔▔▔▔▔▔▔▔▔▔▔▔▔▔\n", String.valueOf((char) ('0' + index + 1)) + "\uFE0F\u20E3", getBookIcon(index), escapeMarkdown(book.getTitle()), escapeMarkdown(book.getAuthor()), escapeMarkdown(formatRating(book.getRating())));
|
||||
}
|
||||
return String.format("%s *%s*\n" + "✍️ _%s_\n" + "🏆 %s\n" +
|
||||
"▔▔▔▔▔▔▔▔▔▔▔▔▔▔\n", getBookIcon(index), escapeMarkdown(book.getTitle()), escapeMarkdown(book.getAuthor()), escapeMarkdown(formatRating(book.getRating())));
|
||||
}
|
||||
|
||||
private String getBookIcon(int index) {
|
||||
return Constants.BOOK_ICONS[index % Constants.BOOK_ICONS.length];
|
||||
}
|
||||
|
||||
private String escapeMarkdown(String text) {
|
||||
return text.replaceAll("([_*\\[\\]()~`>#+=|{}.!-])", "\\\\$1");
|
||||
}
|
||||
|
||||
public static String formatRating(int rating) {
|
||||
int validatedRating = Math.min(10, Math.max(0, rating));
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < validatedRating; i++) {
|
||||
sb.append(FULL_STAR);
|
||||
}
|
||||
|
||||
for (int i = validatedRating; i < 10; i++) {
|
||||
sb.append(EMPTY_STAR);
|
||||
}
|
||||
|
||||
sb.append(" (").append(validatedRating).append("/10)");
|
||||
|
||||
return sb.toString();
|
||||
return BookFormatUtils.formatBookEntry(book, index, forEdit);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,27 +35,6 @@ public class BookManageServiceImpl implements BookManageService {
|
||||
private final TelegramClientService telegramClientService;
|
||||
|
||||
|
||||
public static String formatRating(int rating) {
|
||||
// Ограничиваем рейтинг в диапазоне 0-10
|
||||
int validatedRating = Math.min(Constants.MAX_RATING, Math.max(Constants.MIN_RATING, rating));
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
// Добавляем заполненные звезды
|
||||
for (int i = 0; i < validatedRating; i++) {
|
||||
sb.append(FULL_STAR);
|
||||
}
|
||||
|
||||
// Добавляем пустые звезды
|
||||
for (int i = validatedRating; i < 10; i++) {
|
||||
sb.append(EMPTY_STAR);
|
||||
}
|
||||
|
||||
// Добавляем текстовое представление
|
||||
sb.append(" (").append(validatedRating).append("/10)");
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processBookInput(BotUser user, String text) {
|
||||
@@ -86,30 +65,17 @@ public class BookManageServiceImpl implements BookManageService {
|
||||
return;
|
||||
}
|
||||
switch (editCache.getBookEditState()) {
|
||||
case ADDING_TITLE:
|
||||
case ADDING_TITLE, ADDING_AUTHOR, ADDING_RATING ->
|
||||
bookCreationService.handleExistingAddCache(user, text);
|
||||
break;
|
||||
case ADDING_AUTHOR:
|
||||
bookCreationService.handleExistingAddCache(user, text);
|
||||
break;
|
||||
case ADDING_RATING:
|
||||
bookCreationService.handleExistingAddCache(user, text);
|
||||
break;
|
||||
case SELECTING_BOOK:
|
||||
case SELECTING_BOOK ->
|
||||
bookCreationService.handleNewAddRequest(user, text);
|
||||
break;
|
||||
case CHOOSING_FIELD:
|
||||
//handleExistingAddCache(user, text);
|
||||
break;
|
||||
case EDITING_TITLE:
|
||||
case EDITING_TITLE ->
|
||||
bookEditingService.updateBookTitle(user, text);
|
||||
break;
|
||||
case EDITING_AUTHOR:
|
||||
case EDITING_AUTHOR ->
|
||||
bookEditingService.updateBookAuthor(user, text);
|
||||
break;
|
||||
case EDITING_RATING:
|
||||
case EDITING_RATING ->
|
||||
bookEditingService.updateBookRating(user, text);
|
||||
break;
|
||||
case CHOOSING_FIELD -> {} // No action needed
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package ru.cathub.telegabot.utils;
|
||||
|
||||
import ru.cathub.telegabot.model.Book;
|
||||
import static ru.cathub.telegabot.utils.Constants.*;
|
||||
|
||||
public class BookFormatUtils {
|
||||
|
||||
public static String formatBookEntry(Book book, int index, boolean forEdit) {
|
||||
String prefix = forEdit ?
|
||||
String.valueOf((char) ('0' + index + 1)) + "\uFE0F\u20E3 " : "";
|
||||
|
||||
return String.format("%s%s *%s*\n✍️ _%s_\n🏆 %s\n▔▔▔▔▔▔▔▔▔▔▔▔▔▔\n",
|
||||
prefix,
|
||||
getBookIcon(index),
|
||||
escapeMarkdown(book.getTitle()),
|
||||
escapeMarkdown(book.getAuthor()),
|
||||
escapeMarkdown(formatRating(book.getRating())));
|
||||
}
|
||||
|
||||
public static String getBookIcon(int index) {
|
||||
return BOOK_ICONS[index % BOOK_ICONS.length];
|
||||
}
|
||||
|
||||
public static String escapeMarkdown(String text) {
|
||||
return text.replaceAll("([_*\\[\\]()~`>#+=|{}.!-])", "\\\\$1");
|
||||
}
|
||||
|
||||
public static String formatRating(int rating) {
|
||||
int validatedRating = Math.min(MAX_RATING, Math.max(MIN_RATING, rating));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
sb.append(FULL_STAR.repeat(validatedRating))
|
||||
.append(EMPTY_STAR.repeat(MAX_RATING - validatedRating))
|
||||
.append(String.format(" (%d/%d)", validatedRating, MAX_RATING));
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user