Skip to content

Commit db2044b

Browse files
committed
refactor project
1 parent 7d21e12 commit db2044b

22 files changed

Lines changed: 229 additions & 210 deletions

.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ public class OrganizationController {
1818
private final OrganizationService organizationService;
1919

2020
@GetMapping("/organization")
21-
Page<OrganizationDto> getAllOrganizations(Pageable pageable) {
21+
Page<OrganizationDto> findAll(Pageable pageable) {
2222
return organizationService.getAllOrganizations(pageable);
2323
}
2424

2525
@GetMapping("/organization/type/{type}")
26-
Page<OrganizationDto> getAllOrganizationsByType(@PathVariable TypeOrganization type,
26+
Page<OrganizationDto> findAllByType(@PathVariable TypeOrganization type,
2727
Pageable pageable) {
2828
return organizationService.getOrganizationsByType(pageable, type);
2929
}
3030

3131
@GetMapping("/organization/type")
32-
List<TypeOrganization> getAllTypesOrganizations() {
32+
List<TypeOrganization> findAllTypes() {
3333
return organizationService.getAllTypesOrganizations();
3434
}
3535

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

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

3-
import com.booking.bot.service.ChatService;
3+
import com.booking.bot.service.ChatEditMessageTextFactory;
4+
import com.booking.bot.service.ChatSendMessageFactory;
45
import com.booking.bot.state.Command;
56
import com.booking.bot.state.Context;
67
import com.booking.bot.state.Stage;
@@ -24,7 +25,8 @@
2425
@RequiredArgsConstructor
2526
public class TelegramBot extends TelegramLongPollingBot {
2627

27-
private final ChatService chatService;
28+
private final ChatEditMessageTextFactory chatEditMessageTextFactory;
29+
private final ChatSendMessageFactory chatSendMessageFactory;
2830
private final Map<Long, Context> contextMap = new ConcurrentHashMap<>();
2931

3032
@Value("${bot.username}")
@@ -63,25 +65,31 @@ public void onUpdateReceived(Update update) {
6365
}
6466
}
6567

68+
private Context getContext(Message message) {
69+
return contextMap.get(message.getChatId());
70+
}
71+
6672
private void handleCallback(CallbackQuery callbackQuery) throws TelegramApiException, JsonProcessingException {
67-
Context context = contextMap.get(callbackQuery.getMessage().getChatId());
73+
74+
Context context = getContext(callbackQuery.getMessage());
6875
if (!callbackQuery.getData().isEmpty()) {
6976
context.setCallbackData(callbackQuery.getData().split(":")[1]);
7077
if (Enums.getIfPresent(Stage.class, callbackQuery.getData().split(":")[0]).isPresent()) {
7178
context.setStage(Stage.valueOf(callbackQuery.getData().split(":")[0]));
7279
}
7380
execute(
74-
chatService.editMessageText(context, callbackQuery.getMessage())
81+
chatEditMessageTextFactory.createEditMessageText(context, callbackQuery.getMessage())
7582
);
7683
} else {
7784
System.out.println("callbackQuery is empty");
7885
}
79-
System.out.println(callbackQuery.getData());
8086
}
8187

8288
private void handleMessage(Message message) throws TelegramApiException, JsonProcessingException {
8389
contextMap.putIfAbsent(message.getFrom().getId(), new Context(message.getFrom().getId()));
84-
Context context = contextMap.get(message.getFrom().getId());
90+
91+
Context context = getContext(message);
92+
8593
if (message.isCommand()) {
8694
Optional<MessageEntity> commandEntity =
8795
message.getEntities().stream().filter(e -> "bot_command".equals(e.getType())).findFirst();
@@ -91,10 +99,9 @@ private void handleMessage(Message message) throws TelegramApiException, JsonPro
9199
if (command.equals(Command.START.getValue())) {
92100
context.setStage(Stage.MAIN);
93101
context.setMessageId(
94-
execute(chatService.sendMessage(contextMap
102+
execute(chatSendMessageFactory.createSendMessage(contextMap
95103
.get(message.getFrom().getId()), message))
96104
.getMessageId());
97-
System.out.println(context);
98105
}
99106
}
100107
}

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

Lines changed: 0 additions & 40 deletions
This file was deleted.

bot/src/main/java/com/booking/bot/client/BookingClient.java renamed to bot/src/main/java/com/booking/bot/infrastructure/BookingClient.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
package com.booking.bot.client;
1+
package com.booking.bot.infrastructure;
22

3-
import com.booking.bot.dto.BookingDto;
3+
import com.booking.bot.view.BookingDto;
44
import org.springframework.cloud.openfeign.FeignClient;
55
import org.springframework.web.bind.annotation.PostMapping;
66
import org.springframework.web.bind.annotation.RequestBody;
77

8-
@FeignClient(url = "localhost:8080", name = "booking-client")
8+
@FeignClient(url = "localhost:8080", name = "booking-controller")
99
public interface BookingClient {
1010

1111
@PostMapping("/bookings")
12-
void addNewBooking(@RequestBody BookingDto bookingDto);
12+
void addBooking(@RequestBody BookingDto bookingDto);
1313

1414
}
1515

bot/src/main/java/com/booking/bot/client/OrganizationClient.java renamed to bot/src/main/java/com/booking/bot/infrastructure/OrganizationClient.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package com.booking.bot.client;
1+
package com.booking.bot.infrastructure;
22

3-
import com.booking.bot.dto.OrganizationDto;
3+
import com.booking.bot.view.OrganizationDto;
44
import org.springframework.cloud.openfeign.FeignClient;
55
import org.springframework.data.domain.Page;
66
import org.springframework.data.domain.Pageable;
@@ -12,19 +12,19 @@
1212
import java.util.List;
1313

1414
// TODO: url убрать в application.properties
15-
@FeignClient(url = "localhost:8080", name = "organization-client")
15+
@FeignClient(url = "localhost:8080", name = "organization-controller")
1616
public interface OrganizationClient {
1717

1818
@GetMapping("/organization")
19-
Page<OrganizationDto> getAllOrganizations(Pageable pageable);
19+
Page<OrganizationDto> findAll(Pageable pageable);
2020

2121
@GetMapping("/organization/type/{type}?size=10&page={page}")
22-
Page<OrganizationDto> getAllOrganizationsByType(@PathVariable String type,
22+
Page<OrganizationDto> findAllByType(@PathVariable String type,
2323
@PathVariable Integer page,
2424
Pageable pageable);
2525

2626
@GetMapping("/organization/type")
27-
List<String> getAllTypesOrganizations();
27+
List<String> findAllTypes();
2828

2929
@GetMapping("/organization/{id}")
3030
OrganizationDto getById(@PathVariable Long id);

bot/src/main/java/com/booking/bot/client/PersonClient.java renamed to bot/src/main/java/com/booking/bot/infrastructure/PersonClient.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
package com.booking.bot.client;
1+
package com.booking.bot.infrastructure;
22

3-
import com.booking.bot.dto.PersonDto;
3+
import com.booking.bot.view.PersonDto;
44
import org.springframework.cloud.openfeign.FeignClient;
55
import org.springframework.web.bind.annotation.*;
66

7-
@FeignClient(url = "localhost:8080", name = "person-client")
7+
@FeignClient(url = "localhost:8080", name = "person-controller")
88
public interface PersonClient {
99

1010
@RequestMapping(value = "/person", params = {"id"}, method = RequestMethod.GET)
11-
PersonDto getPersonById(@RequestParam Long id);
11+
PersonDto getById(@RequestParam Long id);
1212

1313
@PostMapping("/person")
1414
void addNewBooking(@RequestBody PersonDto personDto);

bot/src/main/java/com/booking/bot/client/ReservationClient.java renamed to bot/src/main/java/com/booking/bot/infrastructure/ReservationClient.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
package com.booking.bot.client;
1+
package com.booking.bot.infrastructure;
22

3-
import com.booking.bot.dto.ReservationDto;
3+
import com.booking.bot.view.ReservationDto;
4+
import org.springframework.cloud.openfeign.FeignClient;
45
import org.springframework.web.bind.annotation.PostMapping;
56
import org.springframework.web.bind.annotation.RequestBody;
6-
7+
@FeignClient(url = "localhost:8080", name = "reservation-controller")
78
public interface ReservationClient {
89
@PostMapping("/reservation")
910
void updateReservation(@RequestBody ReservationDto reservationDto);
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.booking.bot.service;
2+
3+
import com.booking.bot.state.Context;
4+
import com.booking.bot.view.OrganizationDto;
5+
import com.fasterxml.jackson.core.JsonProcessingException;
6+
import lombok.RequiredArgsConstructor;
7+
import org.springframework.stereotype.Service;
8+
import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageText;
9+
import org.telegram.telegrambots.meta.api.objects.Message;
10+
11+
@Service
12+
@RequiredArgsConstructor
13+
public class ChatEditMessageTextFactory {
14+
private final MenuKeyboardFactory menuKeyboardFactory;
15+
private final OrganizationService organizationService;
16+
17+
18+
public EditMessageText createEditMessageText(Context context, Message message) throws JsonProcessingException {
19+
String userName = message.getFrom().getUserName();
20+
switch (context.getStage()) {
21+
case MAIN -> {
22+
return EditMessageText.builder()
23+
.messageId(context.getMessageId())
24+
.text(String.format("Hi, %s! Сервис по бронированию.", userName))
25+
.chatId(message.getChatId().toString())
26+
.replyMarkup(menuKeyboardFactory.createMainKeyboard())
27+
.build();
28+
}
29+
case TYPE -> {
30+
context.setPage(0);
31+
return EditMessageText.builder()
32+
.messageId(context.getMessageId())
33+
.text(CHOOSE_TYPE_TEXT)
34+
.chatId(message.getChatId().toString())
35+
.replyMarkup(menuKeyboardFactory.createOrganizationTypeKeyboard())
36+
.build();
37+
}
38+
case ORGANIZATIONS -> {
39+
context.setType(context.getCallbackData());
40+
return EditMessageText.builder()
41+
.messageId(context.getMessageId())
42+
.text(CHOOSE_TYPE_TEXT)
43+
.chatId(message.getChatId().toString())
44+
.replyMarkup(menuKeyboardFactory.createChoiceOrganizationKeyboard(context))
45+
.build();
46+
}
47+
case PAGE -> {
48+
context.setPage(Integer.parseInt(context.getCallbackData()));
49+
return EditMessageText.builder()
50+
.messageId(context.getMessageId())
51+
.text(CHOOSE_TYPE_TEXT)
52+
.chatId(message.getChatId().toString())
53+
.replyMarkup(menuKeyboardFactory.createChoiceOrganizationKeyboard(context))
54+
.build();
55+
}
56+
case DESCRIPTION -> {
57+
OrganizationDto organization = organizationService.getById(context.getCallbackData());
58+
return EditMessageText.builder()
59+
.messageId(context.getMessageId())
60+
.text(String.format("%s:" +
61+
"\nРейтинг = %.1f" +
62+
"\nСредний чек = %.2f" +
63+
"\nРасписание = %s",
64+
organization.name(),
65+
organization.rating(),
66+
organization.averageCheck(),
67+
organization.schedule()
68+
))
69+
.chatId(message.getChatId().toString())
70+
.replyMarkup(menuKeyboardFactory.createDescriptionOrganizationKeyboard(context))
71+
.build();
72+
}
73+
default -> {
74+
return null;
75+
}
76+
}
77+
}
78+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.booking.bot.service;
2+
3+
import com.booking.bot.state.Context;
4+
import com.booking.bot.state.Stage;
5+
import com.booking.bot.view.OrganizationDto;
6+
import com.booking.bot.view.PersonDto;
7+
import com.fasterxml.jackson.core.JsonProcessingException;
8+
import lombok.RequiredArgsConstructor;
9+
import org.springframework.stereotype.Service;
10+
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
11+
import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageText;
12+
import org.telegram.telegrambots.meta.api.objects.Message;
13+
14+
@Service
15+
@RequiredArgsConstructor
16+
public class ChatSendMessageFactory {
17+
private final MenuKeyboardFactory menuKeyboardFactory;
18+
private final PersonService personService;
19+
20+
public SendMessage createSendMessage(Context context, Message message) {
21+
Long userId = message.getFrom().getId();
22+
String userName = message.getFrom().getUserName();
23+
if (context.getStage().equals(Stage.MAIN)) {
24+
personService.addPerson(new PersonDto(userId, userName));
25+
return SendMessage.builder()
26+
.text(String.format("Hi, %s! Сервис по бронированию.", userName))
27+
.chatId(message.getChatId().toString())
28+
.replyMarkup(menuKeyboardFactory.createMainKeyboard())
29+
.build();
30+
}
31+
return SendMessage.builder()
32+
.text("Неизвестная команда.")
33+
.chatId(message.getChatId().toString())
34+
.build();
35+
}
36+
}

0 commit comments

Comments
 (0)