Skip to content

Commit f44ecda

Browse files
committed
Сделал вывод описания организаций.
1 parent 7ba1124 commit f44ecda

8 files changed

Lines changed: 118 additions & 115 deletions

File tree

backend/src/main/java/com/booking/backend/controller/OrganizationController.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ public List<OrganizationDto> getAll(@RequestParam String name) {
3030
return organizationService.findAllByName(name);
3131
}
3232

33+
@GetMapping("/organization")
34+
@RequestMapping(value = "/organization", params = "id")
35+
public OrganizationDto getById(@RequestParam Long id) {
36+
return organizationService.getById(id);
37+
}
38+
3339
@GetMapping("/organization")
3440
@RequestMapping(value = "/organization")
3541
public List<OrganizationDto> getAllOrganization() {

backend/src/main/java/com/booking/backend/service/OrganizationService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ public interface OrganizationService {
1313

1414
List<OrganizationDto> getAll();
1515

16+
OrganizationDto getById(Long id);
17+
1618
void updateOrganization(OrganizationDto organizationDto);
1719
}

backend/src/main/java/com/booking/backend/service/OrganizationServiceImpl.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
@Service
1818
public class OrganizationServiceImpl implements OrganizationService {
1919

20-
private OrganizationRepository organizationRepository;
21-
private OrganizationMapper organizationMapper;
20+
private final OrganizationRepository organizationRepository;
21+
private final OrganizationMapper organizationMapper;
2222

2323
public OrganizationServiceImpl(OrganizationRepository organizationRepository, OrganizationMapper organizationMapper) {
2424
this.organizationRepository = organizationRepository;
@@ -70,5 +70,10 @@ public List<OrganizationDto> getAll() {
7070
public void updateOrganization(OrganizationDto organizationDto) {
7171
organizationRepository.save(organizationMapper.convertFromOrganizationDtoToOrganization(organizationDto));
7272
}
73+
74+
@Override
75+
public OrganizationDto getById(Long id) {
76+
return organizationMapper.convertFromOrganizationToOrganizationDto(organizationRepository.getById(id));
77+
}
7378
}
7479

backend/src/main/resources/data.sql

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
INSERT INTO "organization" ("id", "average_check", "name", "rating", "schedule","type_organization")
2-
VALUES (1, 3000.5, 'Россичъ', 9.9, '10:00-22:00','RESTAURANT'),
3-
(2, 2000.5, 'Alma', 10, '12:00-23:00','BAR'),
4-
(3, 3000.5, 'Uhvat', 9.6, '10:00-22:00','RESTAURANT'),
5-
(4, 3000.5, 'Гусятникоff', 9.2, '10:00-22:00','RESTAURANT'),
6-
(5, 3000.5, 'Tinta', 10.0, '10:00-22:00','RESTAURANT'),
7-
(6, 2000.5, 'Кнорозов бар', 9.8, '12:00-23:00','BAR'),
8-
(7, 2000.5, 'Киану', 9.8, '12:00-23:00','BAR'),
9-
(8, 2000.5, 'Happy', 9.5, '12:00-23:00','BAR');
1+
INSERT INTO "organization" ("id", "average_check", "name", "rating", "schedule", "type_organization")
2+
VALUES (1, 3000.5, 'Россичъ', 9.9, '10:00-22:00', 'RESTAURANT'),
3+
(2, 2000.5, 'Alma', 10, '12:00-23:00', 'BAR'),
4+
(3, 3000.5, 'Uhvat', 9.6, '10:00-22:00', 'RESTAURANT'),
5+
(4, 3000.5, 'Гусятникоff', 9.2, '10:00-22:00', 'RESTAURANT'),
6+
(5, 3000.5, 'Tinta', 10.0, '10:00-22:00', 'RESTAURANT'),
7+
(6, 2000.5, 'Кнорозов бар', 9.8, '12:00-23:00', 'BAR'),
8+
(7, 2000.5, 'Киану', 9.8, '12:00-23:00', 'BAR'),
9+
(8, 2000.5, 'Happy', 9.5, '12:00-23:00', 'BAR'),
10+
(9, 1564.20, 'Alchemist', 9.10, '12:00-23:00', 'RESTAURANT'),
11+
(10, 3320.99, 'У дяди Васи', 3.4, '12:00-23:00', 'RESTAURANT'),
12+
(11, 2000.51, 'Direkte', 9.10, '12:00-23:00', 'RESTAURANT'),
13+
(12, 1564.21, 'Sun', 7.4, '12:00-23:00', 'RESTAURANT'),
14+
(13, 3320.10, 'Star', 9.7, '12:00-23:00', 'RESTAURANT'),
15+
(14, 2000.52, 'Beer', 9.3, '12:00-23:00', 'BAR'),
16+
(15, 1564.22, 'Vodka', 10.1, '12:00-23:00', 'BAR');
1017

1118
INSERT INTO "some_object" (id, description, organization_id)
1219
VALUES (1, 'Столик у окна на 8 мест', 1),

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

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import org.springframework.beans.factory.annotation.Value;
88
import org.springframework.stereotype.Component;
99
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
10-
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
1110
import org.telegram.telegrambots.meta.api.objects.CallbackQuery;
1211
import org.telegram.telegrambots.meta.api.objects.Message;
1312
import org.telegram.telegrambots.meta.api.objects.MessageEntity;
@@ -35,18 +34,15 @@ public String getBotToken() {
3534
return token;
3635
}
3736

38-
@Autowired
39-
private final BookingService bookingService;
4037
@Autowired
4138
private final ChatService chatService;
4239

43-
private final Map<Long, Integer> lastMessageIdMap = new HashMap<>();
44-
45-
public TelegramBot(BookingService bookingService, ChatService chatService) {
46-
this.bookingService = bookingService;
40+
public TelegramBot(ChatService chatService) {
4741
this.chatService = chatService;
4842
}
4943

44+
private final Map<Long, Integer> lastMessageIdMap = new HashMap<>();
45+
5046
@Override
5147
public void onUpdateReceived(Update update) {
5248
if (update.hasMessage()) {
@@ -88,26 +84,8 @@ private void handleMessage(Message message) throws TelegramApiException, JsonPro
8884
if (commandEntity.isPresent()) {
8985
String command =
9086
message.getText().substring(commandEntity.get().getOffset(), commandEntity.get().getLength());
91-
lastMessageIdMap.put(message.getChatId(), execute(chatService.commandSwitch(message.getFrom().getId(), command, message)).getMessageId());
87+
lastMessageIdMap.put(message.getFrom().getId(), execute(chatService.commandSwitch(message.getFrom().getId(), command, message)).getMessageId());
9288
}
93-
return;
9489
}
95-
96-
if (message.hasText()) {
97-
Optional<String> messageString = bookingService.parseString(message.getText());
98-
// String mapValue = chatService.get(message.getFrom().getId());
99-
// if (messageString.isPresent()) {
100-
// executeString(bookingService.getValueFromChat(mapValue, messageString.get(), message, chatService), message);
101-
// }
102-
}
103-
}
104-
105-
private void executeString(String executeStr, Message message) throws TelegramApiException {
106-
execute(
107-
SendMessage.builder()
108-
.text(executeStr)
109-
.chatId(message.getChatId().toString())
110-
.build()
111-
);
11290
}
11391
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public List<OrganizationDto> getOrganizations(String uri, String sortBy) {
4848
.bodyToMono(OrganizationDto[].class);
4949
return Arrays.stream(Objects.requireNonNull(organizationDtoMono.share().block())).collect(Collectors.toList());
5050
}
51+
5152
public List<OrganizationDto> getOrganizations(String uri) {
5253
Mono<OrganizationDto[]> organizationDtoMono
5354
= client.get()
@@ -56,4 +57,13 @@ public List<OrganizationDto> getOrganizations(String uri) {
5657
.bodyToMono(OrganizationDto[].class);
5758
return Arrays.stream(Objects.requireNonNull(organizationDtoMono.share().block())).collect(Collectors.toList());
5859
}
60+
61+
public OrganizationDto getOrganizationById(String uri, String id) {
62+
Mono<OrganizationDto> organizationDtoMono
63+
= client.get()
64+
.uri(uri, id)
65+
.retrieve()
66+
.bodyToMono(OrganizationDto.class);
67+
return organizationDtoMono.share().block();
68+
}
5969
}

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

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.booking.bot.service;
22

33
import com.booking.bot.adapter.BotAdapter;
4+
import com.booking.bot.dto.OrganizationDto;
45
import com.booking.bot.dto.PersonDto;
56
import lombok.RequiredArgsConstructor;
67
import org.springframework.beans.factory.annotation.Autowired;
@@ -27,64 +28,75 @@ public ChatServiceImpl(BotAdapter botAdapter, MenuService menuService) {
2728
}
2829

2930
Map<Long, String> chatState = new HashMap<>();
31+
Map<Long, String> chatData = new HashMap<>();
3032

3133
public SendMessage commandSwitch(Long userId, String command, Message message) {
3234
String userName = message.getFrom().getUserName();
33-
switch (command) {
34-
case "/start" -> {
35-
botAdapter.addPerson(new PersonDto(userId, userName), "/person");
36-
chatState.put(userId, "/start");
37-
return SendMessage.builder()
38-
.text("Hi, " + userName + "! Сервис по бронированию.")
39-
.chatId(message.getChatId().toString())
40-
.replyMarkup(menuService.getKeyboard(chatState, message,command))
41-
.build();
42-
}
43-
case "/find" -> {
44-
chatState.put(userId, "/types");
45-
return SendMessage.builder()
46-
.text("Выбери тип организации:")
47-
.chatId(message.getChatId().toString())
48-
.replyMarkup(menuService.getKeyboard(chatState, message,command))
49-
.build();
50-
}
51-
default -> {
52-
return SendMessage.builder()
53-
.text("Я не понимаю что ты хочешь от меня")
54-
.chatId(message.getChatId().toString())
55-
.build();
56-
}
35+
if ("/start".equals(command)) {
36+
botAdapter.addPerson(new PersonDto(userId, userName), "/person");
37+
chatState.put(userId, "main menu");
38+
return SendMessage.builder()
39+
.text("Hi, " + userName + "! Сервис по бронированию.")
40+
.chatId(message.getChatId().toString())
41+
.replyMarkup(menuService.getKeyboard(chatState, message, command))
42+
.build();
5743
}
44+
return SendMessage.builder()
45+
.text("Я не понимаю что ты хочешь от меня")
46+
.chatId(message.getChatId().toString())
47+
.build();
5848
}
5949

6050
public EditMessageText commandSwitch(Long userId, String command, Message message, Integer lastMessageId) {
6151
String userName = message.getFrom().getUserName();
6252
switch (command) {
6353
case "/start" -> {
64-
chatState.put(userId, "/start");
54+
chatState.put(userId, "main menu");
6555
return EditMessageText.builder()
6656
.messageId(lastMessageId)
6757
.text("Hi, " + userName + "! Сервис по бронированию.")
6858
.chatId(message.getChatId().toString())
69-
.replyMarkup(menuService.getKeyboard(chatState, message,command))
59+
.replyMarkup(menuService.getKeyboard(chatState, message, command))
7060
.build();
7161
}
7262
case "/find" -> {
73-
chatState.put(userId, "start > types");
63+
chatState.put(userId, "choice of organization type");
7464
return EditMessageText.builder()
7565
.messageId(lastMessageId)
7666
.text("Выбери тип организации:")
7767
.chatId(message.getChatId().toString())
78-
.replyMarkup(menuService.getKeyboard(chatState, message,command))
68+
.replyMarkup(menuService.getKeyboard(chatState, message, command))
69+
.build();
70+
}
71+
case "back from description" -> {
72+
chatState.put(userId,"choice of organizations");
73+
return EditMessageText.builder()
74+
.messageId(lastMessageId)
75+
.text("Выбери организацию:")
76+
.chatId(message.getChatId().toString())
77+
.replyMarkup(menuService.getKeyboard(chatState, message, chatData.get(userId)))
7978
.build();
8079
}
8180
default -> {
82-
if ("types > organizations".equals(chatState.get(userId))) {
81+
if ("choice of organizations".equals(chatState.get(userId))) {
8382
return EditMessageText.builder()
8483
.messageId(lastMessageId)
8584
.text("Выбери организацию:")
8685
.chatId(message.getChatId().toString())
87-
.replyMarkup(menuService.getKeyboard(chatState, message,command))
86+
.replyMarkup(menuService.getKeyboard(chatState, message, command))
87+
.build();
88+
}
89+
if ("description of the organization".equals(chatState.get(userId))) {
90+
OrganizationDto organization = botAdapter.getOrganizationById("/organization?id={id}", command);
91+
chatData.put(userId,organization.typeOrganization());
92+
return EditMessageText.builder()
93+
.messageId(lastMessageId)
94+
.text(organization.name() + ":" +
95+
"\nРейтинг = " + organization.rating() +
96+
"\nСредний чек = " + organization.averageCheck() +
97+
"\nРасписание = " + organization.schedule())
98+
.chatId(message.getChatId().toString())
99+
.replyMarkup(menuService.getKeyboard(chatState, message, command))
88100
.build();
89101
}
90102
return null;

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

Lines changed: 31 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@
55
import lombok.RequiredArgsConstructor;
66
import org.springframework.beans.factory.annotation.Autowired;
77
import org.springframework.stereotype.Service;
8-
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
98
import org.telegram.telegrambots.meta.api.objects.Message;
109
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
11-
import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboardMarkup;
12-
import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboardRemove;
1310
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton;
14-
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardRow;
1511

1612
import java.util.ArrayList;
1713
import java.util.List;
@@ -31,15 +27,19 @@ public MenuServiceImpl(BotAdapter botAdapter) {
3127
@Override
3228
public InlineKeyboardMarkup getKeyboard(Map<Long, String> chatState, Message message, String command) {
3329
switch (chatState.get(message.getFrom().getId())) {
34-
case "/start" -> {
30+
case "main menu" -> {
3531
return mainKeyboard();
3632
}
37-
case "start > types" -> {
38-
chatState.put(message.getFrom().getId(), "types > organizations");
39-
return organisationTypesKeyboard();
33+
case "choice of organization type" -> {
34+
chatState.put(message.getFrom().getId(), "choice of organizations");
35+
return organizationTypeKeyboard();
4036
}
41-
case "types > organizations" -> {
42-
return organizationKeyboard(command);
37+
case "choice of organizations" -> {
38+
chatState.put(message.getFrom().getId(), "description of the organization");
39+
return choiceOrganizationKeyboard(command);
40+
}
41+
case "description of the organization" -> {
42+
return descriptionOrganizationKeyboard(command);
4343
}
4444
default -> {
4545
return null;
@@ -57,47 +57,30 @@ private InlineKeyboardMarkup mainKeyboard() {
5757
return inlineKeyboardMarkup;
5858
}
5959

60-
private InlineKeyboardMarkup organisationTypesKeyboard() {
61-
InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup();
62-
List<InlineKeyboardButton> keyboardButtonsRow = new ArrayList<>();
63-
List<OrganizationDto> organizationDtoList = botAdapter.getOrganizations("/organization");
64-
List<String> typesList = new ArrayList<>();
65-
for (OrganizationDto oD : organizationDtoList) {
66-
if (!typesList.contains(oD.typeOrganization())) {
67-
typesList.add(oD.typeOrganization());
68-
}
69-
}
70-
for(String type : typesList) {
71-
keyboardButtonsRow.add(InlineKeyboardButton.builder().text(type).callbackData(type).build());
72-
}
60+
private InlineKeyboardMarkup organizationTypeKeyboard() {
7361
List<List<InlineKeyboardButton>> rowList = new ArrayList<>();
74-
rowList.add(keyboardButtonsRow);
75-
keyboardButtonsRow = new ArrayList<>();
76-
keyboardButtonsRow.add(InlineKeyboardButton.builder().text("<< Главное меню").callbackData("/start").build());
77-
rowList.add(keyboardButtonsRow);
78-
inlineKeyboardMarkup.setKeyboard(rowList);
79-
return inlineKeyboardMarkup;
62+
botAdapter.getOrganizations("/organization").stream()
63+
.map(OrganizationDto::typeOrganization)
64+
.distinct()
65+
.forEach( typeOrganization->rowList.add(List.of(InlineKeyboardButton.builder().text(typeOrganization).callbackData(typeOrganization).build())));
66+
rowList.add(List.of(InlineKeyboardButton.builder().text("<< Главное меню").callbackData("/start").build()));
67+
return InlineKeyboardMarkup.builder().keyboard(rowList).build();
8068
}
8169

82-
private InlineKeyboardMarkup organizationKeyboard(String command) {
83-
List<InlineKeyboardButton> keyboardButtonsRow;
84-
InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup();
85-
List<OrganizationDto> organizationDtoList = botAdapter.getOrganizations("/organization");
70+
private InlineKeyboardMarkup choiceOrganizationKeyboard(String type) {
8671
List<List<InlineKeyboardButton>> rowList = new ArrayList<>();
87-
for (OrganizationDto oD : organizationDtoList) {
88-
if (oD.typeOrganization().equals(command)) {
89-
keyboardButtonsRow = new ArrayList<>();
90-
keyboardButtonsRow.add(InlineKeyboardButton.builder().text(oD.name()).callbackData(oD.id().toString()).build());
91-
rowList.add(keyboardButtonsRow);
92-
}
93-
}
94-
keyboardButtonsRow = new ArrayList<>();
95-
keyboardButtonsRow.add(InlineKeyboardButton.builder().text("<< Назад").callbackData("/find").build());
96-
rowList.add(keyboardButtonsRow);
97-
keyboardButtonsRow = new ArrayList<>();
98-
keyboardButtonsRow.add(InlineKeyboardButton.builder().text("<< Главное меню").callbackData("/start").build());
99-
rowList.add(keyboardButtonsRow);
100-
inlineKeyboardMarkup.setKeyboard(rowList);
101-
return inlineKeyboardMarkup;
72+
botAdapter.getOrganizations("/organization").stream()
73+
.filter(o -> o.typeOrganization().equals(type))
74+
.forEach(organizationDto -> rowList.add(List.of(InlineKeyboardButton.builder().text(organizationDto.name()).callbackData(organizationDto.id().toString()).build())));
75+
rowList.add(List.of(InlineKeyboardButton.builder().text("<< Назад").callbackData("/find").build()));
76+
rowList.add(List.of(InlineKeyboardButton.builder().text("<< Главное меню").callbackData("/start").build()));
77+
return InlineKeyboardMarkup.builder().keyboard(rowList).build();
78+
}
79+
80+
private InlineKeyboardMarkup descriptionOrganizationKeyboard(String id) {
81+
List<List<InlineKeyboardButton>> rowList = new ArrayList<>();
82+
rowList.add(List.of(InlineKeyboardButton.builder().text("<< Назад").callbackData("back from description").build()));
83+
rowList.add(List.of(InlineKeyboardButton.builder().text("<< Главное меню").callbackData("/start").build()));
84+
return InlineKeyboardMarkup.builder().keyboard(rowList).build();
10285
}
10386
}

0 commit comments

Comments
 (0)