Skip to content

Commit 7747a38

Browse files
committed
добавил выбор страниц для организаций
1 parent 5e3d75f commit 7747a38

8 files changed

Lines changed: 64 additions & 54 deletions

File tree

bot/src/main/java/com/booking/bot/TelegramBot.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,14 @@ public void onUpdateReceived(Update update) {
6565
}
6666

6767
private void handleCallback(CallbackQuery callbackQuery) throws TelegramApiException, JsonProcessingException {
68+
Context context = contextMap.get(callbackQuery.getMessage().getChatId());
6869
if (!callbackQuery.getData().isEmpty()) {
69-
if(Enums.getIfPresent(Stage.class, callbackQuery.getData()).isPresent()){
70-
contextMap.get(callbackQuery.getMessage().getChatId()).setStage(Stage.valueOf(callbackQuery.getData()));
70+
context.setCallbackData(callbackQuery.getData().split(":")[1]);
71+
if(Enums.getIfPresent(Stage.class, callbackQuery.getData().split(":")[0]).isPresent()){
72+
context.setStage(Stage.valueOf(callbackQuery.getData().split(":")[0]));
7173
}
72-
contextMap.get(callbackQuery.getMessage().getChatId()).setCallbackData(callbackQuery.getData());
7374
execute(
74-
chatService.editMessageText(contextMap.get(callbackQuery.getMessage().getChatId()),
75-
callbackQuery.getMessage()
76-
)
75+
chatService.editMessageText(context, callbackQuery.getMessage())
7776
);
7877
} else {
7978
System.out.println("callbackQuery is empty");
@@ -83,20 +82,20 @@ private void handleCallback(CallbackQuery callbackQuery) throws TelegramApiExcep
8382

8483
private void handleMessage(Message message) throws TelegramApiException, JsonProcessingException {
8584
contextMap.putIfAbsent(message.getFrom().getId(), new Context(message.getFrom().getId()));
85+
Context context = contextMap.get(message.getFrom().getId());
8686
if (message.isCommand()) {
8787
Optional<MessageEntity> commandEntity =
8888
message.getEntities().stream().filter(e -> "bot_command".equals(e.getType())).findFirst();
8989
if (commandEntity.isPresent()) {
9090
String command =
9191
message.getText().substring(commandEntity.get().getOffset(), commandEntity.get().getLength());
9292
if (command.equals(Command.START.getValue())) {
93-
contextMap.get(message.getFrom().getId()).setStage(Stage.MAIN);
94-
contextMap.get(message.getFrom().getId())
95-
.setMessageId(
93+
context.setStage(Stage.MAIN);
94+
context.setMessageId(
9695
execute(chatService.sendMessage(contextMap
9796
.get(message.getFrom().getId()), message))
9897
.getMessageId());
99-
System.out.println(contextMap.get(message.getFrom().getId()).toString());
98+
System.out.println(context);
10099
}
101100
}
102101
}

bot/src/main/java/com/booking/bot/adapter/BotAdapter.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,19 @@ public class BotAdapter {
2121
public void addPerson(PersonDto personDto) {
2222
personClient.addNewBooking(personDto);
2323
}
24+
2425
public List<String> getAllTypesOrganizations() {
2526
return organizationClient.getAllTypesOrganizations();
2627
}
27-
public List<OrganizationDto> getAllOrganizations(){
28+
29+
public List<OrganizationDto> getAllOrganizations() {
2830
return organizationClient.getAllOrganizations(Pageable.unpaged()).getContent();
2931
}
30-
public Page<OrganizationDto> getAllOrganizationsByType(String type) {
31-
return organizationClient.getAllOrganizationsByType(type,Pageable.unpaged());
32+
33+
public Page<OrganizationDto> getAllOrganizationsByType(String type, Integer page) {
34+
return organizationClient.getAllOrganizationsByType(type, page, Pageable.unpaged());
3235
}
36+
3337
public OrganizationDto getOrganizationById(String id) {
3438
return organizationClient.getById(Long.parseLong(id));
3539
}

bot/src/main/java/com/booking/bot/client/OrganizationClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ public interface OrganizationClient {
1515
@GetMapping("/organization")
1616
Page<OrganizationDto> getAllOrganizations(Pageable pageable);
1717

18-
@GetMapping("/organization/type/{type}?size=5")
18+
@GetMapping("/organization/type/{type}?size=10&page={page}")
1919
Page<OrganizationDto> getAllOrganizationsByType(@PathVariable String type,
20+
@PathVariable Integer page,
2021
Pageable pageable);
2122
@GetMapping("/organization/type")
2223
List<String> getAllTypesOrganizations();

bot/src/main/java/com/booking/bot/service/ChatServiceImpl.java

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.booking.bot.state.Context;
77
import com.booking.bot.state.Stage;
88
import com.fasterxml.jackson.core.JsonProcessingException;
9-
import com.fasterxml.jackson.databind.ObjectMapper;
109
import lombok.RequiredArgsConstructor;
1110
import org.springframework.stereotype.Service;
1211
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
@@ -18,7 +17,8 @@
1817
public class ChatServiceImpl implements ChatService {
1918
private final MenuService menuService;
2019
private final BotAdapter botAdapter;
21-
public SendMessage sendMessage(Context context,Message message) throws JsonProcessingException {
20+
21+
public SendMessage sendMessage(Context context, Message message) throws JsonProcessingException {
2222
Long userId = message.getFrom().getId();
2323
String userName = message.getFrom().getUserName();
2424
if (context.getStage().equals(Stage.MAIN)) {
@@ -55,35 +55,33 @@ public EditMessageText editMessageText(Context context, Message message) throws
5555
.build();
5656
}
5757
case ORGANIZATIONS -> {
58+
if (context.getBeforeStage() == Stage.TYPE) {
59+
context.setType(context.getCallbackData());
60+
context.setPage(0);
61+
context.setBeforeStage(Stage.ORGANIZATIONS);
62+
} else {
63+
context.setPage(Integer.parseInt(context.getCallbackData()));
64+
}
65+
return EditMessageText.builder()
66+
.messageId(context.getMessageId())
67+
.text("Выбери организацию:")
68+
.chatId(message.getChatId().toString())
69+
.replyMarkup(menuService.getChoiceOrganizationKeyboard(context))
70+
.build();
71+
}
72+
case DESCRIPTION -> {
73+
OrganizationDto organization = botAdapter.getOrganizationById(context.getCallbackData());
5874
return EditMessageText.builder()
5975
.messageId(context.getMessageId())
60-
.text("Выбери организацию:")
76+
.text(organization.name() + ":" +
77+
"\nРейтинг = " + organization.rating() +
78+
"\nСредний чек = " + organization.averageCheck() +
79+
"\nРасписание = " + organization.schedule())
6180
.chatId(message.getChatId().toString())
62-
.replyMarkup(menuService.getChoiceOrganizationKeyboard(context))
81+
.replyMarkup(menuService.getDescriptionOrganizationKeyboard(context))
6382
.build();
6483
}
6584
default -> {
66-
// if ("choice of organizations".equals(chatState.get(userId))) {
67-
// return EditMessageText.builder()
68-
// .messageId(lastMessageId)
69-
// .text("Выбери организацию:")
70-
// .chatId(message.getChatId().toString())
71-
// .replyMarkup(menuService.getKeyboard(chatState, message, command))
72-
// .build();
73-
// }
74-
// if ("description of the organization".equals(chatState.get(userId))) {
75-
// OrganizationDto organization = botAdapter.getOrganizationById(command);
76-
// chatData.put(userId, organization.typeOrganization());
77-
// return EditMessageText.builder()
78-
// .messageId(lastMessageId)
79-
// .text(organization.name() + ":" +
80-
// "\nРейтинг = " + organization.rating() +
81-
// "\nСредний чек = " + organization.averageCheck() +
82-
// "\nРасписание = " + organization.schedule())
83-
// .chatId(message.getChatId().toString())
84-
// .replyMarkup(menuService.getKeyboard(chatState, message, command))
85-
// .build();
86-
// }
8785
return null;
8886
}
8987
}

bot/src/main/java/com/booking/bot/service/MenuService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ public interface MenuService {
88
InlineKeyboardMarkup getMainKeyboard(Context context) throws JsonProcessingException;
99
InlineKeyboardMarkup getOrganizationTypeKeyboard(Context context);
1010
InlineKeyboardMarkup getChoiceOrganizationKeyboard(Context context);
11+
InlineKeyboardMarkup getDescriptionOrganizationKeyboard(Context context);
1112
}

bot/src/main/java/com/booking/bot/service/MenuServiceImpl.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public InlineKeyboardMarkup getMainKeyboard(Context context) {
2424
List<InlineKeyboardButton> keyboardButtonsRow = new ArrayList<>();
2525
keyboardButtonsRow.add(InlineKeyboardButton.builder()
2626
.text("Поиск")
27-
.callbackData("TYPE")
27+
.callbackData("TYPE:null")
2828
.build());
2929
List<List<InlineKeyboardButton>> rowList = new ArrayList<>();
3030
rowList.add(keyboardButtonsRow);
@@ -33,54 +33,57 @@ public InlineKeyboardMarkup getMainKeyboard(Context context) {
3333
}
3434

3535
public InlineKeyboardMarkup getOrganizationTypeKeyboard(Context context) {
36+
context.setBeforeStage(Stage.TYPE);
3637
context.setStage(Stage.ORGANIZATIONS);
3738
List<List<InlineKeyboardButton>> rowList = new ArrayList<>();
3839
botAdapter.getAllTypesOrganizations().forEach(typeOrganization -> rowList.add(List.of(InlineKeyboardButton.builder()
3940
.text(typeOrganization)
40-
.callbackData(typeOrganization)
41+
.callbackData("ORGANIZATIONS:"+typeOrganization)
4142
.build())));
4243
rowList.add(List.of(InlineKeyboardButton.builder()
4344
.text("<< Главное меню")
44-
.callbackData("MAIN")
45+
.callbackData("MAIN:null")
4546
.build()));
4647
return InlineKeyboardMarkup.builder().keyboard(rowList).build();
4748
}
4849

4950
public InlineKeyboardMarkup getChoiceOrganizationKeyboard(Context context) {
50-
//TODO: сделать кнопки страниц
5151
List<List<InlineKeyboardButton>> rowList = new ArrayList<>();
52-
System.out.println(context.getCallbackData());
53-
Page<OrganizationDto> organizationsByTypePage = botAdapter.getAllOrganizationsByType(context.getCallbackData());
52+
Page<OrganizationDto> organizationsByTypePage = botAdapter.getAllOrganizationsByType(context.getType(),context.getPage());
5453
List<InlineKeyboardButton> pagesButtonRow = new ArrayList<>();
55-
for (int i = 1; i <= organizationsByTypePage.getTotalPages(); i++) {
56-
pagesButtonRow.add(InlineKeyboardButton.builder().text(String.valueOf(i)).callbackData(String.valueOf(i)).build());
54+
for (int i = 0; i < organizationsByTypePage.getTotalPages() && organizationsByTypePage.getTotalPages() !=1; i++) {
55+
pagesButtonRow.add(InlineKeyboardButton.builder()
56+
.text(String.valueOf(i+1))
57+
.callbackData("PAGE:"+i)
58+
.build());
5759
}
5860
organizationsByTypePage.getContent().forEach(organizationDto -> rowList.add(
5961
List.of(InlineKeyboardButton.builder()
6062
.text(organizationDto.name())
61-
.callbackData(organizationDto.id().toString())
63+
.callbackData("DESCRIPTION:"+organizationDto.id().toString())
6264
.build())));
6365
rowList.add(pagesButtonRow);
6466
rowList.add(List.of(InlineKeyboardButton.builder()
6567
.text("<< Назад")
66-
.callbackData("TYPE")
68+
.callbackData("TYPE:null")
6769
.build()));
6870
rowList.add(List.of(InlineKeyboardButton.builder()
6971
.text("<< Главное меню")
70-
.callbackData("MENU")
72+
.callbackData("MAIN:null")
7173
.build()));
7274
return InlineKeyboardMarkup.builder().keyboard(rowList).build();
7375
}
7476

75-
private InlineKeyboardMarkup getDescriptionOrganizationKeyboard() {
77+
//TODO: Починить кнопку Назад
78+
public InlineKeyboardMarkup getDescriptionOrganizationKeyboard(Context context) {
7679
List<List<InlineKeyboardButton>> rowList = new ArrayList<>();
7780
rowList.add(List.of(InlineKeyboardButton.builder()
7881
.text("<< Назад")
79-
.callbackData("back from description")
82+
.callbackData("ORGANIZATIONS:"+context.getType())
8083
.build()));
8184
rowList.add(List.of(InlineKeyboardButton.builder()
8285
.text("<< Главное меню")
83-
.callbackData("/start")
86+
.callbackData("MAIN:null")
8487
.build()));
8588
return InlineKeyboardMarkup.builder().keyboard(rowList).build();
8689
}

bot/src/main/java/com/booking/bot/state/Context.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ public class Context {
1111
private Integer messageId;
1212
private Long userId;
1313
private Stage stage;
14+
private Stage beforeStage;
1415
private String callbackData;
16+
private String type;
17+
private Integer page;
1518
public Context(Long userId) {
1619
this.userId = userId;
1720
}

bot/src/main/java/com/booking/bot/state/Stage.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
public enum Stage {
44
MAIN,
55
TYPE,
6-
ORGANIZATIONS
6+
ORGANIZATIONS,
7+
DESCRIPTION
78
}

0 commit comments

Comments
 (0)