Fix paginated book list

This commit is contained in:
2025-02-07 16:07:21 +03:00
parent 61137438c2
commit 3e8a43807c
11 changed files with 307 additions and 40 deletions
+2 -2
View File
@@ -36,9 +36,9 @@ dependencies {
// implementation 'org.telegram:telegrambots-meta:8.2.0' // И эту
// https://mvnrepository.com/artifact/org.telegram/telegrambots-springboot-longpolling-starter
implementation group: 'org.telegram', name: 'telegrambots-springboot-longpolling-starter', version: '7.2.0'
implementation group: 'org.telegram', name: 'telegrambots-springboot-longpolling-starter', version: '8.2.0'
// https://mvnrepository.com/artifact/org.telegram/telegrambots-client
implementation group: 'org.telegram', name: 'telegrambots-client', version: '7.2.0'
implementation group: 'org.telegram', name: 'telegrambots-client', version: '8.2.0'
implementation 'org.hibernate:hibernate-core:6.4.4.Final'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
+75
View File
@@ -962,3 +962,78 @@ create table bot_user (chat_id bigint, created_date_time timestamp(6), id bigint
create table messages (bot_user_id bigint, created_at timestamp(6), id bigint generated by default as identity, content TEXT, type varchar(255) check (type in ('TEXT','FILE','COMMAND')), primary key (id));
alter table if exists books add constraint FKa9x6empm1nnnicld7s6v8yipr foreign key (bot_user_id) references bot_user;
alter table if exists messages add constraint FKbaqitbc8w0203kqcxcj88wfn9 foreign key (bot_user_id) references bot_user;
create table books (rating integer, bot_user_id bigint, id bigint generated by default as identity, author varchar(255), title varchar(255), primary key (id));
create table bot_user (chat_id bigint, created_date_time timestamp(6), id bigint not null, user_type varchar(255) check (user_type in ('USER','ADMIN')), working_mode varchar(255) check (working_mode in ('BOOK','TORRENT','AI','NO_MODE')), primary key (id));
create table messages (bot_user_id bigint, created_at timestamp(6), id bigint generated by default as identity, content TEXT, type varchar(255) check (type in ('TEXT','FILE','COMMAND')), primary key (id));
alter table if exists books add constraint FKa9x6empm1nnnicld7s6v8yipr foreign key (bot_user_id) references bot_user;
alter table if exists messages add constraint FKbaqitbc8w0203kqcxcj88wfn9 foreign key (bot_user_id) references bot_user;
create table books (rating integer, bot_user_id bigint, id bigint generated by default as identity, author varchar(255), title varchar(255), primary key (id));
create table bot_user (chat_id bigint, created_date_time timestamp(6), id bigint not null, user_type varchar(255) check (user_type in ('USER','ADMIN')), working_mode varchar(255) check (working_mode in ('BOOK','TORRENT','AI','NO_MODE')), primary key (id));
create table messages (bot_user_id bigint, created_at timestamp(6), id bigint generated by default as identity, content TEXT, type varchar(255) check (type in ('TEXT','FILE','COMMAND')), primary key (id));
alter table if exists books add constraint FKa9x6empm1nnnicld7s6v8yipr foreign key (bot_user_id) references bot_user;
alter table if exists messages add constraint FKbaqitbc8w0203kqcxcj88wfn9 foreign key (bot_user_id) references bot_user;
create table books (rating integer, bot_user_id bigint, id bigint generated by default as identity, author varchar(255), title varchar(255), primary key (id));
create table bot_user (chat_id bigint, created_date_time timestamp(6), id bigint not null, user_type varchar(255) check (user_type in ('USER','ADMIN')), working_mode varchar(255) check (working_mode in ('BOOK','TORRENT','AI','NO_MODE')), primary key (id));
create table messages (bot_user_id bigint, created_at timestamp(6), id bigint generated by default as identity, content TEXT, type varchar(255) check (type in ('TEXT','FILE','COMMAND')), primary key (id));
alter table if exists books add constraint FKa9x6empm1nnnicld7s6v8yipr foreign key (bot_user_id) references bot_user;
alter table if exists messages add constraint FKbaqitbc8w0203kqcxcj88wfn9 foreign key (bot_user_id) references bot_user;
create table books (rating integer, bot_user_id bigint, id bigint generated by default as identity, author varchar(255), title varchar(255), primary key (id));
create table bot_user (chat_id bigint, created_date_time timestamp(6), id bigint not null, user_type varchar(255) check (user_type in ('USER','ADMIN')), working_mode varchar(255) check (working_mode in ('BOOK','TORRENT','AI','NO_MODE')), primary key (id));
create table messages (bot_user_id bigint, created_at timestamp(6), id bigint generated by default as identity, content TEXT, type varchar(255) check (type in ('TEXT','FILE','COMMAND')), primary key (id));
alter table if exists books add constraint FKa9x6empm1nnnicld7s6v8yipr foreign key (bot_user_id) references bot_user;
alter table if exists messages add constraint FKbaqitbc8w0203kqcxcj88wfn9 foreign key (bot_user_id) references bot_user;
create table books (rating integer, bot_user_id bigint, id bigint generated by default as identity, author varchar(255), title varchar(255), primary key (id));
create table bot_user (chat_id bigint, created_date_time timestamp(6), id bigint not null, user_type varchar(255) check (user_type in ('USER','ADMIN')), working_mode varchar(255) check (working_mode in ('BOOK','TORRENT','AI','NO_MODE')), primary key (id));
create table messages (bot_user_id bigint, created_at timestamp(6), id bigint generated by default as identity, content TEXT, type varchar(255) check (type in ('TEXT','FILE','COMMAND')), primary key (id));
alter table if exists books add constraint FKa9x6empm1nnnicld7s6v8yipr foreign key (bot_user_id) references bot_user;
alter table if exists messages add constraint FKbaqitbc8w0203kqcxcj88wfn9 foreign key (bot_user_id) references bot_user;
create table books (rating integer, bot_user_id bigint, id bigint generated by default as identity, author varchar(255), title varchar(255), primary key (id));
create table bot_user (chat_id bigint, created_date_time timestamp(6), id bigint not null, user_type varchar(255) check (user_type in ('USER','ADMIN')), working_mode varchar(255) check (working_mode in ('BOOK','TORRENT','AI','NO_MODE')), primary key (id));
create table messages (bot_user_id bigint, created_at timestamp(6), id bigint generated by default as identity, content TEXT, type varchar(255) check (type in ('TEXT','FILE','COMMAND')), primary key (id));
alter table if exists books add constraint FKa9x6empm1nnnicld7s6v8yipr foreign key (bot_user_id) references bot_user;
alter table if exists messages add constraint FKbaqitbc8w0203kqcxcj88wfn9 foreign key (bot_user_id) references bot_user;
create table books (rating integer, bot_user_id bigint, id bigint generated by default as identity, author varchar(255), title varchar(255), primary key (id));
create table bot_user (chat_id bigint, created_date_time timestamp(6), id bigint not null, user_type varchar(255) check (user_type in ('USER','ADMIN')), working_mode varchar(255) check (working_mode in ('BOOK','TORRENT','AI','NO_MODE')), primary key (id));
create table messages (bot_user_id bigint, created_at timestamp(6), id bigint generated by default as identity, content TEXT, type varchar(255) check (type in ('TEXT','FILE','COMMAND')), primary key (id));
alter table if exists books add constraint FKa9x6empm1nnnicld7s6v8yipr foreign key (bot_user_id) references bot_user;
alter table if exists messages add constraint FKbaqitbc8w0203kqcxcj88wfn9 foreign key (bot_user_id) references bot_user;
create table books (rating integer, bot_user_id bigint, id bigint generated by default as identity, author varchar(255), title varchar(255), primary key (id));
create table bot_user (chat_id bigint, created_date_time timestamp(6), id bigint not null, user_type varchar(255) check (user_type in ('USER','ADMIN')), working_mode varchar(255) check (working_mode in ('BOOK','TORRENT','AI','NO_MODE')), primary key (id));
create table messages (bot_user_id bigint, created_at timestamp(6), id bigint generated by default as identity, content TEXT, type varchar(255) check (type in ('TEXT','FILE','COMMAND')), primary key (id));
alter table if exists books add constraint FKa9x6empm1nnnicld7s6v8yipr foreign key (bot_user_id) references bot_user;
alter table if exists messages add constraint FKbaqitbc8w0203kqcxcj88wfn9 foreign key (bot_user_id) references bot_user;
create table books (rating integer, bot_user_id bigint, id bigint generated by default as identity, author varchar(255), title varchar(255), primary key (id));
create table bot_user (chat_id bigint, created_date_time timestamp(6), id bigint not null, user_type varchar(255) check (user_type in ('USER','ADMIN')), working_mode varchar(255) check (working_mode in ('BOOK','TORRENT','AI','NO_MODE')), primary key (id));
create table messages (bot_user_id bigint, created_at timestamp(6), id bigint generated by default as identity, content TEXT, type varchar(255) check (type in ('TEXT','FILE','COMMAND')), primary key (id));
alter table if exists books add constraint FKa9x6empm1nnnicld7s6v8yipr foreign key (bot_user_id) references bot_user;
alter table if exists messages add constraint FKbaqitbc8w0203kqcxcj88wfn9 foreign key (bot_user_id) references bot_user;
create table books (rating integer, bot_user_id bigint, id bigint generated by default as identity, author varchar(255), title varchar(255), primary key (id));
create table bot_user (chat_id bigint, created_date_time timestamp(6), id bigint not null, user_type varchar(255) check (user_type in ('USER','ADMIN')), working_mode varchar(255) check (working_mode in ('BOOK','TORRENT','AI','NO_MODE')), primary key (id));
create table messages (bot_user_id bigint, created_at timestamp(6), id bigint generated by default as identity, content TEXT, type varchar(255) check (type in ('TEXT','FILE','COMMAND')), primary key (id));
alter table if exists books add constraint FKa9x6empm1nnnicld7s6v8yipr foreign key (bot_user_id) references bot_user;
alter table if exists messages add constraint FKbaqitbc8w0203kqcxcj88wfn9 foreign key (bot_user_id) references bot_user;
create table books (rating integer, bot_user_id bigint, id bigint generated by default as identity, author varchar(255), title varchar(255), primary key (id));
create table bot_user (chat_id bigint, created_date_time timestamp(6), id bigint not null, user_type varchar(255) check (user_type in ('USER','ADMIN')), working_mode varchar(255) check (working_mode in ('BOOK','TORRENT','AI','NO_MODE')), primary key (id));
create table messages (bot_user_id bigint, created_at timestamp(6), id bigint generated by default as identity, content TEXT, type varchar(255) check (type in ('TEXT','FILE','COMMAND')), primary key (id));
alter table if exists books add constraint FKa9x6empm1nnnicld7s6v8yipr foreign key (bot_user_id) references bot_user;
alter table if exists messages add constraint FKbaqitbc8w0203kqcxcj88wfn9 foreign key (bot_user_id) references bot_user;
create table books (rating integer, bot_user_id bigint, id bigint generated by default as identity, author varchar(255), title varchar(255), primary key (id));
create table bot_user (chat_id bigint, created_date_time timestamp(6), id bigint not null, user_type varchar(255) check (user_type in ('USER','ADMIN')), working_mode varchar(255) check (working_mode in ('BOOK','TORRENT','AI','NO_MODE')), primary key (id));
create table messages (bot_user_id bigint, created_at timestamp(6), id bigint generated by default as identity, content TEXT, type varchar(255) check (type in ('TEXT','FILE','COMMAND')), primary key (id));
alter table if exists books add constraint FKa9x6empm1nnnicld7s6v8yipr foreign key (bot_user_id) references bot_user;
alter table if exists messages add constraint FKbaqitbc8w0203kqcxcj88wfn9 foreign key (bot_user_id) references bot_user;
create table books (rating integer, bot_user_id bigint, id bigint generated by default as identity, author varchar(255), title varchar(255), primary key (id));
create table bot_user (chat_id bigint, created_date_time timestamp(6), id bigint not null, user_type varchar(255) check (user_type in ('USER','ADMIN')), working_mode varchar(255) check (working_mode in ('BOOK','TORRENT','AI','NO_MODE')), primary key (id));
create table messages (bot_user_id bigint, created_at timestamp(6), id bigint generated by default as identity, content TEXT, type varchar(255) check (type in ('TEXT','FILE','COMMAND')), primary key (id));
alter table if exists books add constraint FKa9x6empm1nnnicld7s6v8yipr foreign key (bot_user_id) references bot_user;
alter table if exists messages add constraint FKbaqitbc8w0203kqcxcj88wfn9 foreign key (bot_user_id) references bot_user;
create table books (rating integer, bot_user_id bigint, id bigint generated by default as identity, author varchar(255), title varchar(255), primary key (id));
create table bot_user (chat_id bigint, created_date_time timestamp(6), id bigint not null, user_type varchar(255) check (user_type in ('USER','ADMIN')), working_mode varchar(255) check (working_mode in ('BOOK','TORRENT','AI','NO_MODE')), primary key (id));
create table messages (bot_user_id bigint, created_at timestamp(6), id bigint generated by default as identity, content TEXT, type varchar(255) check (type in ('TEXT','FILE','COMMAND')), primary key (id));
alter table if exists books add constraint FKa9x6empm1nnnicld7s6v8yipr foreign key (bot_user_id) references bot_user;
alter table if exists messages add constraint FKbaqitbc8w0203kqcxcj88wfn9 foreign key (bot_user_id) references bot_user;
create table books (rating integer, bot_user_id bigint, id bigint generated by default as identity, author varchar(255), title varchar(255), primary key (id));
create table bot_user (chat_id bigint, created_date_time timestamp(6), id bigint not null, user_type varchar(255) check (user_type in ('USER','ADMIN')), working_mode varchar(255) check (working_mode in ('BOOK','TORRENT','AI','NO_MODE')), primary key (id));
create table messages (bot_user_id bigint, created_at timestamp(6), id bigint generated by default as identity, content TEXT, type varchar(255) check (type in ('TEXT','FILE','COMMAND')), primary key (id));
alter table if exists books add constraint FKa9x6empm1nnnicld7s6v8yipr foreign key (bot_user_id) references bot_user;
alter table if exists messages add constraint FKbaqitbc8w0203kqcxcj88wfn9 foreign key (bot_user_id) references bot_user;
@@ -0,0 +1,27 @@
package ru.cathub.telegabot.model;
import lombok.*;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Setter
public class BookEditStateCache {
public enum BookEditState {
ADDING_TITLE,
ADDING_AUTHOR,
ADDING_RATING,
SELECTING_BOOK,
CHOOSING_FIELD,
EDITING_TITLE,
EDITING_AUTHOR,
EDITING_RATING,
NONE
}
private BookEditState bookEditState = BookEditState.NONE;
private Long bookId;
}
@@ -0,0 +1,14 @@
package ru.cathub.telegabot.model;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.RequiredArgsConstructor;
@Data
@Builder
@AllArgsConstructor
public class BookListCache {
private int currentPage;
private Integer listMessageId;
}
@@ -20,4 +20,5 @@ public interface BookRepository extends JpaRepository<Book, Long> {
List<Book> findPaginatedByBotUser(@Param("user") BotUser user, Pageable pageable);
Optional<Book> findByIdAndBotUser(Long bookId, BotUser user);
}
@@ -3,10 +3,11 @@ package ru.cathub.telegabot.service;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import ru.cathub.telegabot.model.BookCreationCache;
import ru.cathub.telegabot.model.BookEditStateCache;
import ru.cathub.telegabot.model.BookListCache;
import static ru.cathub.telegabot.utils.Constants.BOOK_CACHE_NAME;
import static ru.cathub.telegabot.utils.Constants.*;
public interface BookCacheService {
@@ -18,4 +19,22 @@ public interface BookCacheService {
@CacheEvict(value = BOOK_CACHE_NAME, key = "#userId")
void clearCache(Long userId);
@Cacheable(value = BOOK_CACHE_LIST, key = "#userId")
BookListCache getBookListState(Long userId);
@CachePut(value = BOOK_CACHE_LIST, key = "#userId")
BookListCache updateBookListState(Long userId, BookListCache state);
@Cacheable(value = BOOK_CACHE_EDIT, key = "#userId")
BookEditStateCache getBookEditCache(Long userId);
@CachePut(value = BOOK_CACHE_EDIT, key = "#userId")
BookEditStateCache updateBookEditCache(Long userId, BookEditStateCache cache);
@CacheEvict(value = BOOK_CACHE_EDIT, key = "#userId")
void clearBookEditCache(Long userId);
@CacheEvict(value = BOOK_CACHE_LIST, key = "#userId")
void clearBookListCache(Long userId);
}
@@ -14,5 +14,5 @@ public interface TelegramClientService {
Message sendMessageWithMarkdown(BotUser botUser, String text, InlineKeyboardMarkup bookListKeyboard);
void editMessage(BotUser botUser, int messageId, String s);
void editMessageWithMarkdown(BotUser botUser, int messageId, String s, InlineKeyboardMarkup bookListKeyboard);
}
@@ -6,9 +6,11 @@ import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import ru.cathub.telegabot.model.BookCreationCache;
import ru.cathub.telegabot.model.BookEditStateCache;
import ru.cathub.telegabot.model.BookListCache;
import ru.cathub.telegabot.service.BookCacheService;
import static ru.cathub.telegabot.utils.Constants.BOOK_CACHE_NAME;
import static ru.cathub.telegabot.utils.Constants.*;
@Service
@RequiredArgsConstructor
@@ -30,4 +32,38 @@ public class BookCacheServiceImpl implements BookCacheService {
@CacheEvict(value = BOOK_CACHE_NAME, key = "#userId")
public void clearCache(Long userId) {
}
@Override
@Cacheable(value = BOOK_CACHE_LIST, key = "#userId")
public BookListCache getBookListState(Long userId) {
return null;
}
@Override
@CachePut(value = BOOK_CACHE_LIST, key = "#userId")
public BookListCache updateBookListState(Long userId, BookListCache state) {
return state;
}
@Override
@Cacheable(value = BOOK_CACHE_EDIT, key = "#userId")
public BookEditStateCache getBookEditCache(Long userId) {
return null;
}
@Override
@CachePut(value = BOOK_CACHE_EDIT, key = "#userId")
public BookEditStateCache updateBookEditCache(Long userId, BookEditStateCache cache) {
return cache;
}
@Override
@CacheEvict(value = BOOK_CACHE_EDIT, key = "#userId")
public void clearBookEditCache(Long userId) {
}
@Override
@CacheEvict(value = BOOK_CACHE_LIST, key = "#userId")
public void clearBookListCache(Long userId) {
}
}
@@ -8,9 +8,7 @@ import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import ru.cathub.telegabot.bot.KeyboardHelper;
import ru.cathub.telegabot.exception.ServiceException;
import ru.cathub.telegabot.model.Book;
import ru.cathub.telegabot.model.BookCreationCache;
import ru.cathub.telegabot.model.BotUser;
import ru.cathub.telegabot.model.*;
import ru.cathub.telegabot.repository.BookRepository;
import ru.cathub.telegabot.repository.UserRepository;
import ru.cathub.telegabot.service.BookCacheService;
@@ -33,28 +31,56 @@ public class BookManageServiceImpl implements BookManageService {
private final BookCacheService bookCacheService;
private final BookRepository bookRepository;
@Override
public void processBookInput(BotUser user, String text) {
Long userId = user.getId();
BookCreationCache cache = bookCacheService.getCache(userId);
BookCreationCache creationCache = bookCacheService.getCache(userId);
BookEditStateCache editCache = bookCacheService.getBookEditCache(userId);
switch (text) {
case LIST_BOOKS:
showPaginatedBookList(user, 0,null);
bookCacheService.clearBookListCache(userId);
showPaginatedBookList(user, 0, null);
break;
case EDIT_BOOKS:
startBookEditing(user);
break;
default:
if (cache == null) {
handleNewAddRequest(user, text);
} else {
handleExistingAddCache(user, cache, text);
if (editCache == null || editCache.getBookEditState() == BookEditStateCache.BookEditState.NONE) {
telegramClientService.sendMessage(user, "❌ Нет текущего действия. Выберете действие из меню или перейдите в начальное меню командой /start");
return;
}
switch (editCache.getBookEditState()) {
case ADDING_TITLE:
handleNewAddRequest(user, text);
break;
case ADDING_AUTHOR:
handleExistingAddCache(user, creationCache, text);
break;
case ADDING_RATING:
handleExistingAddCache(user, creationCache, text);
break;
case SELECTING_BOOK:
handleNewAddRequest(user, text);
break;
case CHOOSING_FIELD:
//handleExistingAddCache(user, text);
break;
case EDITING_TITLE:
updateBookTitle(user, text);
break;
case EDITING_AUTHOR:
updateBookAuthor(user, text);
break;
case EDITING_RATING:
updateBookRating(user, text);
break;
}
}
}
@Override
public void handleStartMessage(BotUser botUser) throws ServiceException {
botUser.setWorkingMode(BotUser.WorkingMode.BOOK);
@@ -66,8 +92,12 @@ public class BookManageServiceImpl implements BookManageService {
public void processBookQuery(BotUser user, String text) {
if (text.startsWith("BOOK_PAGE_")) {
int page = Integer.parseInt(text.split("_")[2]);
Integer messageId = Integer.parseInt(text.split("_")[3]);
showPaginatedBookList(user, page,messageId);
var bookListCache = bookCacheService.getBookListState(user.getId());
if (bookListCache.getCurrentPage() != page) {
bookListCache.setCurrentPage(page);
bookCacheService.updateBookListState(user.getId(), bookListCache);
}
showPaginatedBookList(user, page, bookListCache.getListMessageId());
} else if (text.startsWith("BOOK_EDIT_ID_")) {
Long bookId = Long.parseLong(text.split("_")[3]);
processBookSelection(user, bookId);
@@ -77,10 +107,10 @@ public class BookManageServiceImpl implements BookManageService {
handleEditTitle(user);
break;
case "BOOK_EDIT_AUTHOR":
//handleEditAuthor(user);
handleEditAuthor(user);
break;
case "BOOK_EDIT_RATING":
//handleEditRating(user);
handleEditRating(user);
break;
case "BOOK_EDIT_CANCEL":
// cancelEditing(user);
@@ -90,10 +120,6 @@ public class BookManageServiceImpl implements BookManageService {
}
}
private void handleEditTitle(BotUser user) {
// user.setBookEditState(BookEditState.EDITING_TITLE);
telegramClientService.sendMessage(user, "Введите новое название:");
}
private void handleNewAddRequest(BotUser user, String text) {
if (ADD_BOOK.equals(text)) {
@@ -141,7 +167,7 @@ public class BookManageServiceImpl implements BookManageService {
try {
Pageable pageable = PageRequest.of(page, 5);
Page<Book> books = bookRepository.findByBotUser(botUser,pageable);
Page<Book> books = bookRepository.findByBotUser(botUser, pageable);
StringBuilder sb = new StringBuilder();
@@ -150,17 +176,24 @@ public class BookManageServiceImpl implements BookManageService {
int counter = 1;
for (Book book : books) {
sb.append(formatBookEntry(book, (counter-1)%5));
sb.append(formatBookEntry(book, (counter - 1) % 5));
counter++;
}
telegramClientService.editMessage(botUser, messageId, escapeMarkdown(sb.toString()));
telegramClientService.sendMessageWithMarkdown(
botUser,
escapeMarkdown(sb.toString()),
getBookListKeyboard(page, books.hasNext())
);
var bookListCache = bookCacheService.getBookListState(botUser.getId());
if (bookListCache != null) {
bookListCache.setCurrentPage(page);
bookCacheService.updateBookListState(botUser.getId(), bookListCache);
telegramClientService.editMessageWithMarkdown(botUser, messageId, sb.toString(),getBookListKeyboard(page, books.hasNext()));
} else {
var message = telegramClientService.sendMessageWithMarkdown(
botUser,
sb.toString(),
getBookListKeyboard(page, books.hasNext())
);
bookCacheService.updateBookListState(botUser.getId(), BookListCache.builder().currentPage(page).listMessageId(message.getMessageId()).build());
}
} catch (Exception e) {
// 6. Обработка ошибок
log.error("Error loading book list", e);
@@ -173,19 +206,19 @@ public class BookManageServiceImpl implements BookManageService {
}
public void startBookEditing(BotUser user) {
// user.setBookEditState(BookEditState.SELECTING_BOOK);
userRepository.save(user);
telegramClientService.sendMessage(user, "Выберите книгу для редактирования:",
bookCacheService.updateBookEditCache(user.getId(), BookEditStateCache.builder().bookEditState(BookEditStateCache.BookEditState.SELECTING_BOOK).build());
var message = telegramClientService.sendMessage(user, "Выберите книгу для редактирования:",
getBookEditListKeyboard(user, bookRepository.findByBotUser(user)));
bookCacheService.updateBookListState(user.getId(), BookListCache.builder().currentPage(0).listMessageId(message.getMessageId()).build());
}
private void processBookSelection(BotUser user, Long bookId) {
private void processBookSelection(BotUser user, Long bookId) {
try {
Optional<Book> book = bookRepository.findByIdAndBotUser(bookId, user);
if (book.isPresent()) {
log.info("Found book with id {}", bookId);
telegramClientService.sendMessage(user, "Выберите поле для редактирования:", getEditOptions(user));
} else {
telegramClientService.sendMessage(user, "Книга не найдена");
@@ -224,19 +257,77 @@ public class BookManageServiceImpl implements BookManageService {
String[] icons = {"📘", "📙", "📕", "📗", "📓"};
return icons[index % icons.length];
}
private String formatBookEntry(Book book, int index) {
return String.format(
"%s *%s*\n" +
"✍️ _%s_\n" +
"🏆 %s\n" +
// "\n" +
// "\n" +
"▔▔▔▔▔▔▔▔▔▔▔▔▔▔\n",
getBookIcon(index),
escapeMarkdown(book.getTitle()),
escapeMarkdown(book.getAuthor()),
formatRating(book.getRating())
escapeMarkdown(formatRating(book.getRating()))
);
}
private void handleEditTitle(BotUser user) {
var bookEditState = bookCacheService.getBookEditCache(user.getId());
bookEditState.setBookEditState(BookEditStateCache.BookEditState.EDITING_TITLE);
bookCacheService.updateBookEditCache(user.getId(), BookEditStateCache.builder().bookEditState(BookEditStateCache.BookEditState.EDITING_TITLE).build());
telegramClientService.sendMessage(user,"Введите новое название:");
}
private void handleEditAuthor(BotUser user) {
var bookEditState = bookCacheService.getBookEditCache(user.getId());
bookEditState.setBookEditState(BookEditStateCache.BookEditState.EDITING_AUTHOR);
bookCacheService.updateBookEditCache(user.getId(), BookEditStateCache.builder().bookEditState(BookEditStateCache.BookEditState.EDITING_TITLE).build());
telegramClientService.sendMessage(user,"Введите нового автора:");
}
private void handleEditRating(BotUser user) {
var bookEditState = bookCacheService.getBookEditCache(user.getId());
bookEditState.setBookEditState(BookEditStateCache.BookEditState.EDITING_RATING);
bookCacheService.updateBookEditCache(user.getId(), BookEditStateCache.builder().bookEditState(BookEditStateCache.BookEditState.EDITING_TITLE).build());
telegramClientService.sendMessage(user,"Введите рейтинг:");
}
private void updateBookTitle(BotUser user, String newTitle) {
var bookEditState = bookCacheService.getBookEditCache(user.getId());
Book book = bookRepository.findById(bookEditState.getBookId()).orElseThrow();
book.setTitle(newTitle);
bookRepository.save(book);
bookEditState.setBookEditState(BookEditStateCache.BookEditState.NONE);
bookCacheService.updateBookEditCache(user.getId(), bookEditState);
telegramClientService.sendMessage(user, "Название успешно обновлено!");
}
private void updateBookAuthor(BotUser user, String newAuthor) {
var bookEditState = bookCacheService.getBookEditCache(user.getId());
Book book = bookRepository.findById(bookEditState.getBookId()).orElseThrow();
book.setAuthor(newAuthor);
bookRepository.save(book);
bookEditState.setBookEditState(BookEditStateCache.BookEditState.NONE);
bookCacheService.updateBookEditCache(user.getId(), bookEditState);
telegramClientService.sendMessage(user,"Автор успешно обновлен");
}
private void updateBookRating(BotUser user, String newRating) {
var bookEditState = bookCacheService.getBookEditCache(user.getId());
Book book = bookRepository.findById(bookEditState.getBookId()).orElseThrow();
book.setRating(Integer.parseInt(newRating));
bookRepository.save(book);
bookEditState.setBookEditState(BookEditStateCache.BookEditState.NONE);
bookCacheService.updateBookEditCache(user.getId(), bookEditState);
telegramClientService.sendMessage(user,"Рейтинг успешно обновлен");
}
}
@@ -59,13 +59,15 @@ public class TelegramClientServiceImps implements TelegramClientService {
}
@Override
public void editMessage(BotUser botUser, int messageId, String s) {
public void editMessageWithMarkdown(BotUser botUser, int messageId, String s, InlineKeyboardMarkup bookListKeyboard) {
try {
telegramClient.execute(EditMessageText.builder()
.chatId(botUser.getChatId().toString())
.messageId(messageId)
.parseMode("MarkdownV2")
.text(s)
.replyMarkup(bookListKeyboard)
.build());
} catch (TelegramApiException e) {
log.error("Ошибка отправки сообщения", e);
@@ -8,5 +8,7 @@ public class Constants{
public static final String LIST_BOOKS = "Список книг";
public static final String BACK = "Назад";
public static final String EDIT_BOOKS = "Редактировать книги";
public static final String BOOK_CACHE_LIST = "bookListCache";
public static final String BOOK_CACHE_EDIT = "bookEditCache";
}