Skip to content

Commit

Permalink
Update project
Browse files Browse the repository at this point in the history
  • Loading branch information
Mykhailo Tiutiun committed Aug 14, 2024
1 parent 45cdd2f commit 86b2f3b
Show file tree
Hide file tree
Showing 64 changed files with 1,355 additions and 918 deletions.
41 changes: 29 additions & 12 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
version: "3.3"
services:
rep-counter-bot:
container_name: rep-counter-bot
image: rep-counter-bot
ports:
- "8080:8080"
# rep-counter-bot:
# container_name: rep-counter-bot
# image: rep-counter-bot
# ports:
# - "8080:8080"
# environment:
# DATASOURCE_URI: ""
# restart: unless-stopped
# deploy:
# resources:
# limits:
# cpus: '1'
# memory: 300M
# networks:
# - repcounterbot

postgres:
container_name: postgres
image: postgres
restart: always
shm_size: 128mb
environment:
DATASOURCE_URI: "mongodb+srv://repcounterbot-app:[email protected]/?retryWrites=true&w=majority&appName=RepCounterBotCluster"
restart: unless-stopped
deploy:
resources:
limits:
cpus: '1'
memory: 300M
POSTGRES_DB: orders
POSTGRES_USER: order-service
POSTGRES_PASSWORD: order
volumes:
- repcounterbot-db:/var/lib/postgresql/data
networks:
- repcounterbot

networks:
repcounterbot:
driver: bridge

volumes:
repcounterbot-db:
6 changes: 5 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

import com.mykhailotiutiun.repcounterbot.botapi.handler.CallbackQueryHandler;
import com.mykhailotiutiun.repcounterbot.botapi.handler.MessageHandler;
import com.mykhailotiutiun.repcounterbot.cache.ChatDataCache;
import com.mykhailotiutiun.repcounterbot.cache.CurrentBotStateCache;
import com.mykhailotiutiun.repcounterbot.constants.CallbackHandlerType;
import com.mykhailotiutiun.repcounterbot.constants.ChatState;
import com.mykhailotiutiun.repcounterbot.constants.MessageHandlerType;
import com.mykhailotiutiun.repcounterbot.service.LocaleMessageService;
import com.mykhailotiutiun.repcounterbot.util.LocaleMessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageText;
import org.telegram.telegrambots.meta.api.objects.CallbackQuery;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update;
Expand All @@ -27,14 +26,14 @@ public class RepCounterBotFacade {
private final Map<MessageHandlerType, MessageHandler> messageHandlers = new HashMap<>();
private final Map<CallbackHandlerType, CallbackQueryHandler> callbackQueryHandlers = new HashMap<>();

private final LocaleMessageService localeMessageService;
private final ChatDataCache chatDataCache;
private final LocaleMessageUtil localeMessageUtil;
private final CurrentBotStateCache currentBotStateCache;

public RepCounterBotFacade(List<MessageHandler> messageHandlers, List<CallbackQueryHandler> callbackQueryHandlers, LocaleMessageService localeMessageService, ChatDataCache chatDataCache) {
this.localeMessageService = localeMessageService;
public RepCounterBotFacade(List<MessageHandler> messageHandlers, List<CallbackQueryHandler> callbackQueryHandlers, LocaleMessageUtil localeMessageUtil, CurrentBotStateCache currentBotStateCache) {
this.localeMessageUtil = localeMessageUtil;
messageHandlers.forEach(handler -> this.messageHandlers.put(handler.getHandlerType(), handler));
callbackQueryHandlers.forEach(handler -> this.callbackQueryHandlers.put(handler.getHandlerType(), handler));
this.chatDataCache = chatDataCache;
this.currentBotStateCache = currentBotStateCache;
}

public BotApiMethod<?> handleUpdate(Update update) {
Expand All @@ -50,7 +49,7 @@ public BotApiMethod<?> handleUpdate(Update update) {
private BotApiMethod<?> handleCallbackQuery(CallbackQuery callbackQuery) {
CallbackQueryHandler callbackQueryHandler = choseCallbackQueryHandler(callbackQuery);
if (callbackQueryHandler == null) {
return new SendMessage(callbackQuery.getFrom().getId().toString(), localeMessageService.getMessage("reply.error", callbackQuery.getFrom().getId().toString()));
return new SendMessage(callbackQuery.getFrom().getId().toString(), localeMessageUtil.getMessage("reply.error", callbackQuery.getFrom().getId().toString()));
}

log.trace("CallbackQuery request from @{}, with text {}", callbackQuery.getFrom().getUserName(), callbackQuery.getData());
Expand All @@ -59,19 +58,19 @@ private BotApiMethod<?> handleCallbackQuery(CallbackQuery callbackQuery) {

private CallbackQueryHandler choseCallbackQueryHandler(CallbackQuery callbackQuery) {
if (callbackQuery.getData().contains("Main")) {
chatDataCache.setChatDataCurrentBotState(callbackQuery.getFrom().getId().toString(), ChatState.MAIN_MENU);
currentBotStateCache.setChatDataCurrentBotState(callbackQuery.getFrom().getId().toString(), ChatState.MAIN_MENU);
return callbackQueryHandlers.get(CallbackHandlerType.MAIN_MENU_HANDLER);
} else if(callbackQuery.getData().contains("WorkoutWeek")) {
chatDataCache.setChatDataCurrentBotState(callbackQuery.getFrom().getId().toString(), ChatState.MAIN_MENU);
currentBotStateCache.setChatDataCurrentBotState(callbackQuery.getFrom().getId().toString(), ChatState.MAIN_MENU);
return callbackQueryHandlers.get(CallbackHandlerType.WORKOUT_WEEK_HANDLER);
} else if (callbackQuery.getData().contains("WorkoutDay")) {
chatDataCache.setChatDataCurrentBotState(callbackQuery.getFrom().getId().toString(), ChatState.MAIN_MENU);
currentBotStateCache.setChatDataCurrentBotState(callbackQuery.getFrom().getId().toString(), ChatState.MAIN_MENU);
return callbackQueryHandlers.get(CallbackHandlerType.WORKOUT_DAY_HANDLER);
} else if (callbackQuery.getData().contains("WorkoutExercise")) {
chatDataCache.setChatDataCurrentBotState(callbackQuery.getFrom().getId().toString(), ChatState.MAIN_MENU);
currentBotStateCache.setChatDataCurrentBotState(callbackQuery.getFrom().getId().toString(), ChatState.MAIN_MENU);
return callbackQueryHandlers.get(CallbackHandlerType.WORKOUT_EXERCISE_HANDLER);
} else if (callbackQuery.getData().contains("WorkoutSet")) {
chatDataCache.setChatDataCurrentBotState(callbackQuery.getFrom().getId().toString(), ChatState.MAIN_MENU);
currentBotStateCache.setChatDataCurrentBotState(callbackQuery.getFrom().getId().toString(), ChatState.MAIN_MENU);
return callbackQueryHandlers.get(CallbackHandlerType.WORKOUT_SET_HANDLER);
}

Expand All @@ -82,7 +81,7 @@ private CallbackQueryHandler choseCallbackQueryHandler(CallbackQuery callbackQue
private BotApiMethod<?> handleMessage(Message message) {
MessageHandler messageHandler = choseMessageHandler(message);
if (messageHandler == null) {
return new SendMessage(message.getChatId().toString(), localeMessageService.getMessage("reply.invalid-message", message.getChatId().toString()));
return new SendMessage(message.getChatId().toString(), localeMessageUtil.getMessage("reply.invalid-message", message.getChatId().toString()));
}

log.trace("Message request from @{}, with text {}", message.getFrom().getUserName(), message.getText());
Expand All @@ -98,19 +97,19 @@ private MessageHandler choseMessageHandler(Message message) {
case ("/start"):
case ("Choose a language"):
case ("Обрати мову"): {
chatDataCache.setChatDataCurrentBotState(message.getChatId().toString(), ChatState.MAIN_MENU);
currentBotStateCache.setChatDataCurrentBotState(message.getChatId().toString(), ChatState.MAIN_MENU);
return messageHandlers.get(MessageHandlerType.MAIN_MENU_HANDLER);
}
case ("Поточний тиждень тренувань"):
case ("Current workout week"): {
chatDataCache.setChatDataCurrentBotState(message.getChatId().toString(), ChatState.MAIN_MENU);
currentBotStateCache.setChatDataCurrentBotState(message.getChatId().toString(), ChatState.MAIN_MENU);
return messageHandlers.get(MessageHandlerType.CURRENT_WEEK_HANDLER);
}
default:
break;
}

switch (chatDataCache.getChatDataCurrentBotState(message.getChatId().toString())) {
switch (currentBotStateCache.getChatDataCurrentBotState(message.getChatId().toString())) {
case SET_NAME_FOR_WORKOUT_DAY:
return messageHandlers.get(MessageHandlerType.WORKOUT_DAY_HANDLER);
case CREATE_WORKOUT_EXERCISE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.mykhailotiutiun.repcounterbot.botapi.handler.CallbackQueryHandler;
import com.mykhailotiutiun.repcounterbot.constants.CallbackHandlerType;
import com.mykhailotiutiun.repcounterbot.service.MainMenuService;
import com.mykhailotiutiun.repcounterbot.message.MainMenuMessageGenerator;
import com.mykhailotiutiun.repcounterbot.service.UserService;
import org.springframework.stereotype.Component;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
Expand All @@ -11,20 +11,20 @@
@Component
public class MainMenuCallbackQueryHandler implements CallbackQueryHandler {

private final MainMenuService mainMenuService;
private final MainMenuMessageGenerator mainMenuMessageGenerator;
private final UserService userService;

public MainMenuCallbackQueryHandler(MainMenuService mainMenuService, UserService userService) {
this.mainMenuService = mainMenuService;
public MainMenuCallbackQueryHandler(MainMenuMessageGenerator mainMenuMessageGenerator, UserService userService) {
this.mainMenuMessageGenerator = mainMenuMessageGenerator;
this.userService = userService;
}

@Override
public SendMessage handleCallbackQuery(CallbackQuery callbackQuery) {
// 0 - command, 1 - userId, 2 - lang
String[] splintedMessage = callbackQuery.getData().split(":");
userService.setUserLang(splintedMessage[1], splintedMessage[2]);
return mainMenuService.getMainMenuMessage(callbackQuery.getFrom().getId().toString(), callbackQuery.getFrom().getFirstName());
userService.setUserLang(Long.valueOf(splintedMessage[1]), splintedMessage[2]);
return mainMenuMessageGenerator.getMainMenuMessage(callbackQuery.getFrom().getId().toString(), callbackQuery.getFrom().getFirstName());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import com.mykhailotiutiun.repcounterbot.constants.MessageHandlerType;
import com.mykhailotiutiun.repcounterbot.exception.EntityAlreadyExistsException;
import com.mykhailotiutiun.repcounterbot.model.User;
import com.mykhailotiutiun.repcounterbot.service.LocaleMessageService;
import com.mykhailotiutiun.repcounterbot.service.MainMenuService;
import com.mykhailotiutiun.repcounterbot.util.LocaleMessageUtil;
import com.mykhailotiutiun.repcounterbot.message.MainMenuMessageGenerator;
import com.mykhailotiutiun.repcounterbot.service.UserService;
import org.springframework.stereotype.Component;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
Expand All @@ -20,14 +20,14 @@
@Component
public class MainMenuMessageHandler implements MessageHandler {

private final MainMenuService mainMenuService;
private final MainMenuMessageGenerator mainMenuMessageGenerator;
private final UserService userService;
private final LocaleMessageService localeMessageService;
private final LocaleMessageUtil localeMessageUtil;

public MainMenuMessageHandler(MainMenuService mainMenuService, UserService userService, LocaleMessageService localeMessageService) {
this.mainMenuService = mainMenuService;
public MainMenuMessageHandler(MainMenuMessageGenerator mainMenuMessageGenerator, UserService userService, LocaleMessageUtil localeMessageUtil) {
this.mainMenuMessageGenerator = mainMenuMessageGenerator;
this.userService = userService;
this.localeMessageService = localeMessageService;
this.localeMessageUtil = localeMessageUtil;
}

@Override
Expand All @@ -44,14 +44,17 @@ public BotApiMethod<?> handleMessage(Message message) {

private SendMessage handleStart(Message message) {
try {
userService.create(new User(message.getFrom().getId(), message.getFrom().getFirstName()));
userService.create(User.builder()
.id(message.getFrom().getId())
.username(message.getFrom().getFirstName())
.build());
} catch (EntityAlreadyExistsException ignored) {
}
return mainMenuService.getMainMenuMessage(message.getChatId().toString(), message.getFrom().getFirstName());
return mainMenuMessageGenerator.getMainMenuMessage(message.getChatId().toString(), message.getFrom().getFirstName());
}

private SendMessage handleChooseLang(Message message) {
SendMessage sendMessage = new SendMessage(message.getChatId().toString(), localeMessageService.getMessage("reply.main-menu.keyboard.change-lang", message.getChatId().toString()));
SendMessage sendMessage = new SendMessage(message.getChatId().toString(), localeMessageUtil.getMessage("reply.main-menu.keyboard.change-lang", message.getChatId().toString()));
sendMessage.setReplyMarkup(getKeyboardForLang(message.getChatId().toString()));
return sendMessage;
}
Expand Down
Loading

0 comments on commit 86b2f3b

Please sign in to comment.