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 15, 2024
1 parent 86b2f3b commit 66f8d45
Show file tree
Hide file tree
Showing 61 changed files with 782 additions and 310 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
secrets

### STS ###
.apt_generated
Expand Down
14 changes: 8 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
FROM tomcat:10.1.11
FROM maven:3.9.7-eclipse-temurin-17 AS build
COPY src /home/app/src
COPY pom.xml /home/app
RUN mvn -f /home/app/pom.xml clean package

COPY ./target/RepCounterBot.war /usr/local/tomcat/webapps-javaee/ROOT.war

EXPOSE 8080

CMD ["catalina.sh", "run"]
FROM openjdk:17-jdk-alpine
LABEL maintainer="MykhailoTiutiun <[email protected]>"
COPY --from=build /home/app/target/RepCounterBot.jar RepCounterBot.jar
ENTRYPOINT ["java", "-jar", "/RepCounterBot.jar"]
46 changes: 26 additions & 20 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
version: "3.3"
services:
# 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
repcounterbot:
container_name: repcounterbot
build:
dockerfile: Dockerfile
ports:
- "8080:8080"
environment:
DATASOURCE_URL: ""
DATASOURCE_USER: ""
DATASOURCE_PASSWORD: ""
BOT_TOKEN: ""
restart: unless-stopped
deploy:
resources:
limits:
cpus: '1'
memory: 300M
networks:
- local

postgres:
container_name: postgres
image: postgres
restart: always
shm_size: 128mb
ports:
- 5432:5432
environment:
POSTGRES_DB: orders
POSTGRES_USER: order-service
POSTGRES_PASSWORD: order
POSTGRES_DB: repcounterbot
POSTGRES_USER: ""
POSTGRES_PASSWORD: ""
volumes:
- repcounterbot-db:/var/lib/postgresql/data
networks:
- repcounterbot
- local

networks:
repcounterbot:
local:
driver: bridge

volumes:
Expand Down
13 changes: 9 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<groupId>com.mykhailotiutiun</groupId>
<artifactId>RepCounterBot</artifactId>
<version>1.0.1</version>
<packaging>war</packaging>
<packaging>jar</packaging>
<name>RepCounterBot</name>
<description>RepCounterBot</description>
<properties>
Expand Down Expand Up @@ -79,9 +79,14 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
<version>2.22.0</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit4</artifactId>
<version>2.22.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
<finalName>RepCounterBot</finalName>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@
import lombok.extern.slf4j.Slf4j;
import org.telegram.telegrambots.bots.TelegramWebhookBot;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Getter
Expand All @@ -33,59 +26,8 @@ public RepCounterBot(RepCounterBotFacade repCounterBotFacade) {
this.repCounterBotFacade = repCounterBotFacade;
}


@Override
public BotApiMethod<?> onWebhookUpdateReceived(Update update) {
return repCounterBotFacade.handleUpdate(update);
}

public void sendMessage(String chatId, String textMessage) {
SendMessage sendMessage = new SendMessage();
sendMessage.setChatId(chatId);
sendMessage.setText(textMessage);

try {
execute(sendMessage);
} catch (TelegramApiException e) {
e.printStackTrace();
}
}

public void sendMessage(SendMessage sendMessage) {
try {
execute(sendMessage);
} catch (TelegramApiException e) {
e.printStackTrace();
}
}

public void sendInlineKeyBoardMessage(String chatId, String text, String buttonText, String callbackData) {
InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup();
InlineKeyboardButton keyboardButton = new InlineKeyboardButton();

keyboardButton.setText(buttonText);

if (callbackData != null) {
keyboardButton.setCallbackData(callbackData);
}

List<InlineKeyboardButton> keyboardButtonsRow1 = new ArrayList<>();
keyboardButtonsRow1.add(keyboardButton);

List<List<InlineKeyboardButton>> rowList = new ArrayList<>();
rowList.add(keyboardButtonsRow1);

inlineKeyboardMarkup.setKeyboard(rowList);

SendMessage sendMessage = new SendMessage();
sendMessage.setChatId(chatId);
sendMessage.setText(text);
sendMessage.setReplyMarkup(inlineKeyboardMarkup);

try {
execute(sendMessage);
} catch (TelegramApiException e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ public BotApiMethod<?> handleUpdate(Update update) {
} else {
return handleMessage(update.getMessage());
}


}

private BotApiMethod<?> handleCallbackQuery(CallbackQuery callbackQuery) {
Expand Down Expand Up @@ -100,8 +98,8 @@ private MessageHandler choseMessageHandler(Message message) {
currentBotStateCache.setChatDataCurrentBotState(message.getChatId().toString(), ChatState.MAIN_MENU);
return messageHandlers.get(MessageHandlerType.MAIN_MENU_HANDLER);
}
case ("Поточний тиждень тренувань"):
case ("Current workout week"): {
case ("Поточний тиждень"):
case ("Current week"): {
currentBotStateCache.setChatDataCurrentBotState(message.getChatId().toString(), ChatState.MAIN_MENU);
return messageHandlers.get(MessageHandlerType.CURRENT_WEEK_HANDLER);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.mykhailotiutiun.repcounterbot.constants.CallbackHandlerType;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.CallbackQuery;

public interface CallbackQueryHandler {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import com.mykhailotiutiun.repcounterbot.botapi.handler.MessageHandler;
import com.mykhailotiutiun.repcounterbot.constants.MessageHandlerType;
import com.mykhailotiutiun.repcounterbot.exception.EntityAlreadyExistsException;
import com.mykhailotiutiun.repcounterbot.model.User;
import com.mykhailotiutiun.repcounterbot.util.LocaleMessageUtil;
import com.mykhailotiutiun.repcounterbot.message.MainMenuMessageGenerator;
import com.mykhailotiutiun.repcounterbot.model.User;
import com.mykhailotiutiun.repcounterbot.service.UserService;
import com.mykhailotiutiun.repcounterbot.util.LocaleMessageUtil;
import org.springframework.stereotype.Component;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
Expand Down Expand Up @@ -47,6 +47,7 @@ private SendMessage handleStart(Message message) {
userService.create(User.builder()
.id(message.getFrom().getId())
.username(message.getFrom().getFirstName())
.localTag("en_EN")
.build());
} catch (EntityAlreadyExistsException ignored) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import com.mykhailotiutiun.repcounterbot.cache.SelectedWorkoutDayCache;
import com.mykhailotiutiun.repcounterbot.constants.CallbackHandlerType;
import com.mykhailotiutiun.repcounterbot.constants.ChatState;
import com.mykhailotiutiun.repcounterbot.message.MainMenuMessageGenerator;
import com.mykhailotiutiun.repcounterbot.message.WorkoutDayMessageGenerator;
import com.mykhailotiutiun.repcounterbot.message.WorkoutWeekMessageGenerator;
import com.mykhailotiutiun.repcounterbot.util.LocaleMessageUtil;
import com.mykhailotiutiun.repcounterbot.message.MainMenuMessageGenerator;
import com.mykhailotiutiun.repcounterbot.service.WorkoutDayService;
import com.mykhailotiutiun.repcounterbot.service.WorkoutWeekService;
import com.mykhailotiutiun.repcounterbot.util.LocaleMessageUtil;
import org.springframework.stereotype.Component;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageText;
Expand All @@ -20,17 +20,15 @@
public class WorkoutDayCallbackQueryHandler implements CallbackQueryHandler {

private final WorkoutDayService workoutDayService;
private final WorkoutWeekService workoutWeekService;
private final MainMenuMessageGenerator mainMenuMessageGenerator;
private final LocaleMessageUtil localeMessageUtil;
private final CurrentBotStateCache currentBotStateCache;
private final SelectedWorkoutDayCache selectedWorkoutDayCache;
private final WorkoutDayMessageGenerator workoutDayMessageGenerator;
private final WorkoutWeekMessageGenerator workoutWeekMessageGenerator;

public WorkoutDayCallbackQueryHandler(WorkoutDayService workoutDayService, WorkoutWeekService workoutWeekService, MainMenuMessageGenerator mainMenuMessageGenerator, LocaleMessageUtil localeMessageUtil, CurrentBotStateCache currentBotStateCache, SelectedWorkoutDayCache selectedWorkoutDayCache, WorkoutDayMessageGenerator workoutDayMessageGenerator, WorkoutWeekMessageGenerator workoutWeekMessageGenerator) {
public WorkoutDayCallbackQueryHandler(WorkoutDayService workoutDayService, MainMenuMessageGenerator mainMenuMessageGenerator, LocaleMessageUtil localeMessageUtil, CurrentBotStateCache currentBotStateCache, SelectedWorkoutDayCache selectedWorkoutDayCache, WorkoutDayMessageGenerator workoutDayMessageGenerator, WorkoutWeekMessageGenerator workoutWeekMessageGenerator) {
this.workoutDayService = workoutDayService;
this.workoutWeekService = workoutWeekService;
this.mainMenuMessageGenerator = mainMenuMessageGenerator;
this.localeMessageUtil = localeMessageUtil;
this.currentBotStateCache = currentBotStateCache;
Expand All @@ -42,7 +40,7 @@ public WorkoutDayCallbackQueryHandler(WorkoutDayService workoutDayService, Worko
@Override
public BotApiMethod<?> handleCallbackQuery(CallbackQuery callbackQuery) {
if (callbackQuery.getData().startsWith("/select")) {
return workoutDayMessageGenerator.getSelectWorkoutDayEditMessage(callbackQuery.getFrom().getId().toString(), callbackQuery.getMessage().getMessageId(), callbackQuery.getData().split(":")[1]);
return workoutDayMessageGenerator.getSelectWorkoutDayEditMessage(callbackQuery.getFrom().getId().toString(), callbackQuery.getMessage().getMessageId(), Long.valueOf(callbackQuery.getData().split(":")[1]));
} else if (callbackQuery.getData().startsWith("/set-name-request")) {
return handelSetNameRequest(callbackQuery);
} else if (callbackQuery.getData().startsWith("/set-rest-request")) {
Expand Down Expand Up @@ -79,7 +77,7 @@ private EditMessageText handelSetRestRequest(CallbackQuery callbackQuery){
}

private EditMessageText handelSetRest(CallbackQuery callbackQuery) {
workoutDayService.setRestWorkoutDay(callbackQuery.getData().split(":")[1]);
workoutDayService.setRestWorkoutDay(Long.valueOf(callbackQuery.getData().split(":")[1]));
return workoutWeekMessageGenerator.getCurrentWorkoutWeekEditMessage(callbackQuery.getFrom().getId().toString(), callbackQuery.getMessage().getMessageId());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,20 @@
public class WorkoutDayMessageHandler implements MessageHandler {

private final WorkoutDayService workoutDayService;
private final WorkoutWeekService workoutWeekService;
private final CurrentBotStateCache currentBotStateCache;
private final SelectedWorkoutDayCache selectedWorkoutDayCache;
private final WorkoutWeekMessageGenerator workoutWeekMessageGenerator;

public WorkoutDayMessageHandler(WorkoutDayService workoutDayService, WorkoutWeekService workoutWeekService, CurrentBotStateCache currentBotStateCache, SelectedWorkoutDayCache selectedWorkoutDayCache, WorkoutWeekMessageGenerator workoutWeekMessageGenerator) {
public WorkoutDayMessageHandler(WorkoutDayService workoutDayService, CurrentBotStateCache currentBotStateCache, SelectedWorkoutDayCache selectedWorkoutDayCache, WorkoutWeekMessageGenerator workoutWeekMessageGenerator) {
this.workoutDayService = workoutDayService;
this.workoutWeekService = workoutWeekService;
this.currentBotStateCache = currentBotStateCache;
this.selectedWorkoutDayCache = selectedWorkoutDayCache;
this.workoutWeekMessageGenerator = workoutWeekMessageGenerator;
}

@Override
public BotApiMethod<?> handleMessage(Message message) {
workoutDayService.setName(selectedWorkoutDayCache.getSelectedWorkoutDay(message.getChatId().toString()), message.getText());
workoutDayService.setName(Long.valueOf(selectedWorkoutDayCache.getSelectedWorkoutDay(message.getChatId().toString())), message.getText());

currentBotStateCache.setChatDataCurrentBotState(message.getChatId().toString(), ChatState.MAIN_MENU);
return workoutWeekMessageGenerator.getCurrentWorkoutWeekSendMessage(message.getChatId().toString());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package com.mykhailotiutiun.repcounterbot.botapi.handler.Impl;

import com.mykhailotiutiun.repcounterbot.botapi.handler.CallbackQueryHandler;
import com.mykhailotiutiun.repcounterbot.cache.SelectedWorkoutExerciseCache;
import com.mykhailotiutiun.repcounterbot.cache.CurrentBotStateCache;
import com.mykhailotiutiun.repcounterbot.cache.SelectedWorkoutDayCache;
import com.mykhailotiutiun.repcounterbot.cache.SelectedWorkoutExerciseCache;
import com.mykhailotiutiun.repcounterbot.constants.CallbackHandlerType;
import com.mykhailotiutiun.repcounterbot.constants.ChatState;
import com.mykhailotiutiun.repcounterbot.message.MainMenuMessageGenerator;
import com.mykhailotiutiun.repcounterbot.message.WorkoutDayMessageGenerator;
import com.mykhailotiutiun.repcounterbot.message.WorkoutExerciseMessageGenerator;
import com.mykhailotiutiun.repcounterbot.model.WorkoutExercise;
import com.mykhailotiutiun.repcounterbot.util.LocaleMessageUtil;
import com.mykhailotiutiun.repcounterbot.message.MainMenuMessageGenerator;
import com.mykhailotiutiun.repcounterbot.service.WorkoutExerciseService;
import com.mykhailotiutiun.repcounterbot.util.LocaleMessageUtil;
import org.springframework.stereotype.Component;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageText;
Expand Down Expand Up @@ -85,22 +85,22 @@ private EditMessageText handleCreateRequest(CallbackQuery callbackQuery) {


private EditMessageText handleSelect(CallbackQuery callbackQuery) {
return workoutExerciseMessageGenerator.getWorkoutExerciseEditMessage(callbackQuery.getFrom().getId().toString(), callbackQuery.getMessage().getMessageId(), callbackQuery.getData().split(":")[1], false);
return workoutExerciseMessageGenerator.getWorkoutExerciseEditMessage(callbackQuery.getFrom().getId().toString(), callbackQuery.getMessage().getMessageId(), Long.valueOf(callbackQuery.getData().split(":")[1]), false);
}

private EditMessageText handleEdit(CallbackQuery callbackQuery) {
return workoutExerciseMessageGenerator.getWorkoutExerciseEditMessage(callbackQuery.getFrom().getId().toString(), callbackQuery.getMessage().getMessageId(), callbackQuery.getData().split(":")[1], true);
return workoutExerciseMessageGenerator.getWorkoutExerciseEditMessage(callbackQuery.getFrom().getId().toString(), callbackQuery.getMessage().getMessageId(), Long.valueOf(callbackQuery.getData().split(":")[1]), true);
}

private EditMessageText moveUp(CallbackQuery callbackQuery){
WorkoutExercise workoutExercise = workoutExerciseService.getById(callbackQuery.getData().split(":")[1]);
workoutExerciseService.moveUp(callbackQuery.getData().split(":")[1]);
WorkoutExercise workoutExercise = workoutExerciseService.getById(Long.valueOf(callbackQuery.getData().split(":")[1]));
workoutExerciseService.moveUp(Long.valueOf(callbackQuery.getData().split(":")[1]));
return workoutDayMessageGenerator.getSelectWorkoutDayEditMessage(callbackQuery.getFrom().getId().toString(), callbackQuery.getMessage().getMessageId(), workoutExercise.getWorkoutDay().getId());
}

private EditMessageText moveDown(CallbackQuery callbackQuery){
workoutExerciseService.moveDown(callbackQuery.getData().split(":")[1]);
WorkoutExercise workoutExercise = workoutExerciseService.getById(callbackQuery.getData().split(":")[1]);
workoutExerciseService.moveDown(Long.valueOf(callbackQuery.getData().split(":")[1]));
WorkoutExercise workoutExercise = workoutExerciseService.getById(Long.valueOf(callbackQuery.getData().split(":")[1]));
return workoutDayMessageGenerator.getSelectWorkoutDayEditMessage(callbackQuery.getFrom().getId().toString(), callbackQuery.getMessage().getMessageId(), workoutExercise.getWorkoutDay().getId());
}

Expand All @@ -123,8 +123,8 @@ private EditMessageText handleDeleteRequest(CallbackQuery callbackQuery){
}

private EditMessageText handleDelete(CallbackQuery callbackQuery) {
WorkoutExercise workoutExercise = workoutExerciseService.getById(callbackQuery.getData().split(":")[1]);
String workoutDayId = workoutExercise.getWorkoutDay().getId();
WorkoutExercise workoutExercise = workoutExerciseService.getById(Long.valueOf(callbackQuery.getData().split(":")[1]));
Long workoutDayId = workoutExercise.getWorkoutDay().getId();
workoutExerciseService.deleteById(workoutExercise.getId());

return workoutDayMessageGenerator.getSelectWorkoutDayEditMessage(callbackQuery.getFrom().getId().toString(), callbackQuery.getMessage().getMessageId(), workoutDayId);
Expand Down
Loading

0 comments on commit 66f8d45

Please sign in to comment.