From 66f8d457976d174c966651ba20576038fbf8f5de Mon Sep 17 00:00:00 2001 From: Mykhailo Tiutiun Date: Thu, 15 Aug 2024 20:18:53 +0200 Subject: [PATCH] Update project --- .gitignore | 1 + Dockerfile | 14 ++- docker-compose.yml | 46 ++++--- pom.xml | 13 +- .../repcounterbot/ServletInitializer.java | 13 -- .../repcounterbot/botapi/RepCounterBot.java | 58 --------- .../botapi/RepCounterBotFacade.java | 6 +- .../botapi/handler/CallbackQueryHandler.java | 1 - .../handler/Impl/MainMenuMessageHandler.java | 5 +- .../Impl/WorkoutDayCallbackQueryHandler.java | 12 +- .../Impl/WorkoutDayMessageHandler.java | 6 +- .../WorkoutExerciseCallbackQueryHandler.java | 22 ++-- .../Impl/WorkoutExerciseMessageHandler.java | 10 +- .../Impl/WorkoutSetCallbackQueryHandler.java | 4 +- .../Impl/WorkoutSetMessageHandler.java | 6 +- .../Impl/WorkoutWeekCallbackQueryHandler.java | 1 - .../Impl/WorkoutWeekMessageHandler.java | 1 - .../repcounterbot/config/JdbcConfig.java | 16 +++ ...ookController.java => MainController.java} | 4 +- .../language/SelectedLanguageProvider.java | 5 + .../repcounterbot/mapper/UserMapper.java | 18 +++ .../mapper/WorkoutDayMapper.java | 21 ++++ .../mapper/WorkoutExerciseMapper.java | 23 ++++ .../mapper/WorkoutSetMapper.java | 19 +++ .../mapper/WorkoutWeekMapper.java | 21 ++++ .../message/WorkoutDayMessageGenerator.java | 4 +- .../WorkoutExerciseMessageGenerator.java | 4 +- .../impl/MainMenuMessageGeneratorImpl.java | 2 - .../impl/WorkoutDayMessageGeneratorImpl.java | 4 +- .../WorkoutExerciseMessageGeneratorImpl.java | 4 +- .../repcounterbot/model/WorkoutDay.java | 4 +- .../repcounterbot/model/WorkoutExercise.java | 2 +- .../repcounterbot/model/WorkoutSet.java | 2 +- .../repcounterbot/model/WorkoutWeek.java | 2 +- .../repository/WorkoutDayRepository.java | 4 +- .../repository/WorkoutExerciseRepository.java | 5 +- .../repository/WorkoutSetRepository.java | 5 + .../repository/WorkoutWeekRepository.java | 1 + .../repository/impl/UserRepositoryImpl.java | 39 ++++++ .../impl/WorkoutDayRepositoryImpl.java | 73 +++++++++++ .../impl/WorkoutExerciseRepositoryImpl.java | 119 ++++++++++++++++++ .../impl/WorkoutSetRepositoryImpl.java | 56 +++++++++ .../impl/WorkoutWeekRepositoryImpl.java | 66 ++++++++++ .../service/Impl/UserServiceImpl.java | 18 ++- .../service/Impl/WorkoutDayServiceImpl.java | 26 ++-- .../Impl/WorkoutExerciseServiceImpl.java | 24 ++-- .../service/Impl/WorkoutSetServiceImpl.java | 1 + .../service/Impl/WorkoutWeekServiceImpl.java | 13 +- .../service/WorkoutDayService.java | 8 +- .../service/WorkoutExerciseService.java | 14 +-- .../service/WorkoutWeekService.java | 2 - .../util/impl/LocaleMessageUtilImpl.java | 10 +- src/main/resources/application.properties | 7 +- src/main/resources/messages_en_US.properties | 6 +- src/main/resources/messages_uk_UA.properties | 8 +- src/main/resources/schema.sql | 63 ++++++++++ .../services/WorkoutDayServiceImplTest.java | 48 ++++--- .../WorkoutExerciseServiceImplTest.java | 83 ++++++------ .../services/WorkoutSetsServiceImplTest.java | 2 +- .../services/WorkoutWeekServiceImplTest.java | 10 +- .../util/LocaleMessageUtilImplTest.java | 7 +- 61 files changed, 782 insertions(+), 310 deletions(-) delete mode 100644 src/main/java/com/mykhailotiutiun/repcounterbot/ServletInitializer.java create mode 100644 src/main/java/com/mykhailotiutiun/repcounterbot/config/JdbcConfig.java rename src/main/java/com/mykhailotiutiun/repcounterbot/controller/{WebhookController.java => MainController.java} (88%) create mode 100644 src/main/java/com/mykhailotiutiun/repcounterbot/language/SelectedLanguageProvider.java create mode 100644 src/main/java/com/mykhailotiutiun/repcounterbot/mapper/UserMapper.java create mode 100644 src/main/java/com/mykhailotiutiun/repcounterbot/mapper/WorkoutDayMapper.java create mode 100644 src/main/java/com/mykhailotiutiun/repcounterbot/mapper/WorkoutExerciseMapper.java create mode 100644 src/main/java/com/mykhailotiutiun/repcounterbot/mapper/WorkoutSetMapper.java create mode 100644 src/main/java/com/mykhailotiutiun/repcounterbot/mapper/WorkoutWeekMapper.java create mode 100644 src/main/java/com/mykhailotiutiun/repcounterbot/repository/impl/UserRepositoryImpl.java create mode 100644 src/main/java/com/mykhailotiutiun/repcounterbot/repository/impl/WorkoutDayRepositoryImpl.java create mode 100644 src/main/java/com/mykhailotiutiun/repcounterbot/repository/impl/WorkoutExerciseRepositoryImpl.java create mode 100644 src/main/java/com/mykhailotiutiun/repcounterbot/repository/impl/WorkoutSetRepositoryImpl.java create mode 100644 src/main/java/com/mykhailotiutiun/repcounterbot/repository/impl/WorkoutWeekRepositoryImpl.java create mode 100644 src/main/resources/schema.sql diff --git a/.gitignore b/.gitignore index f2d67c3..cfea594 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ target/ !.mvn/wrapper/maven-wrapper.jar !**/src/main/**/target/ !**/src/test/**/target/ +secrets ### STS ### .apt_generated diff --git a/Dockerfile b/Dockerfile index dcc4853..c2f6a33 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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"] \ No newline at end of file +FROM openjdk:17-jdk-alpine +LABEL maintainer="MykhailoTiutiun " +COPY --from=build /home/app/target/RepCounterBot.jar RepCounterBot.jar +ENTRYPOINT ["java", "-jar", "/RepCounterBot.jar"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 838a338..7d8534c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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: diff --git a/pom.xml b/pom.xml index 155d5bd..34f07d2 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ com.mykhailotiutiun RepCounterBot 1.0.1 - war + jar RepCounterBot RepCounterBot @@ -79,9 +79,14 @@ org.apache.maven.plugins maven-surefire-plugin - - true - + 2.22.0 + + + org.apache.maven.surefire + surefire-junit4 + 2.22.0 + + RepCounterBot diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/ServletInitializer.java b/src/main/java/com/mykhailotiutiun/repcounterbot/ServletInitializer.java deleted file mode 100644 index 49a7e97..0000000 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/ServletInitializer.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.mykhailotiutiun.repcounterbot; - -import org.springframework.boot.builder.SpringApplicationBuilder; -import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; - -public class ServletInitializer extends SpringBootServletInitializer { - - @Override - protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { - return application.sources(RepCounterBotApplication.class); - } - -} diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/RepCounterBot.java b/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/RepCounterBot.java index 446cb1f..78b7ead 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/RepCounterBot.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/RepCounterBot.java @@ -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 @@ -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 keyboardButtonsRow1 = new ArrayList<>(); - keyboardButtonsRow1.add(keyboardButton); - - List> 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(); - } - } } diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/RepCounterBotFacade.java b/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/RepCounterBotFacade.java index 075a024..81abe98 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/RepCounterBotFacade.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/RepCounterBotFacade.java @@ -42,8 +42,6 @@ public BotApiMethod handleUpdate(Update update) { } else { return handleMessage(update.getMessage()); } - - } private BotApiMethod handleCallbackQuery(CallbackQuery callbackQuery) { @@ -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); } diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/CallbackQueryHandler.java b/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/CallbackQueryHandler.java index a6f9373..d046bb8 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/CallbackQueryHandler.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/CallbackQueryHandler.java @@ -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 { diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/MainMenuMessageHandler.java b/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/MainMenuMessageHandler.java index a0bcd78..8d9e74a 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/MainMenuMessageHandler.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/MainMenuMessageHandler.java @@ -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; @@ -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) { } diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/WorkoutDayCallbackQueryHandler.java b/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/WorkoutDayCallbackQueryHandler.java index 3574751..f77b043 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/WorkoutDayCallbackQueryHandler.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/WorkoutDayCallbackQueryHandler.java @@ -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; @@ -20,7 +20,6 @@ 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; @@ -28,9 +27,8 @@ public class WorkoutDayCallbackQueryHandler implements CallbackQueryHandler { 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; @@ -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")) { @@ -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()); } diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/WorkoutDayMessageHandler.java b/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/WorkoutDayMessageHandler.java index c69731d..3ec3bdb 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/WorkoutDayMessageHandler.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/WorkoutDayMessageHandler.java @@ -16,14 +16,12 @@ 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; @@ -31,7 +29,7 @@ public WorkoutDayMessageHandler(WorkoutDayService workoutDayService, WorkoutWeek @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()); diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/WorkoutExerciseCallbackQueryHandler.java b/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/WorkoutExerciseCallbackQueryHandler.java index 186c884..7308819 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/WorkoutExerciseCallbackQueryHandler.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/WorkoutExerciseCallbackQueryHandler.java @@ -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; @@ -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()); } @@ -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); diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/WorkoutExerciseMessageHandler.java b/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/WorkoutExerciseMessageHandler.java index 05fd91a..4d62af2 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/WorkoutExerciseMessageHandler.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/WorkoutExerciseMessageHandler.java @@ -1,9 +1,9 @@ package com.mykhailotiutiun.repcounterbot.botapi.handler.Impl; import com.mykhailotiutiun.repcounterbot.botapi.handler.MessageHandler; -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.ChatState; import com.mykhailotiutiun.repcounterbot.constants.MessageHandlerType; import com.mykhailotiutiun.repcounterbot.message.WorkoutDayMessageGenerator; @@ -54,17 +54,17 @@ private SendMessage handleCreateWorkoutExercise(Message message) { workoutExerciseService.create(WorkoutExercise.builder() .name(message.getText()) - .workoutDay(workoutDayService.getById(selectedWorkoutDayCache.getSelectedWorkoutDay(message.getChatId().toString()))) + .workoutDay(workoutDayService.getById(Long.valueOf(selectedWorkoutDayCache.getSelectedWorkoutDay(message.getChatId().toString())))) .build()); - return workoutDayMessageGenerator.getSelectWorkoutDaySendMessage(message.getChatId().toString(), selectedWorkoutDayCache.getSelectedWorkoutDay(message.getChatId().toString())); + return workoutDayMessageGenerator.getSelectWorkoutDaySendMessage(message.getChatId().toString(), Long.valueOf(selectedWorkoutDayCache.getSelectedWorkoutDay(message.getChatId().toString()))); } private SendMessage handleChangeName(Message message){ currentBotStateCache.setChatDataCurrentBotState(message.getChatId().toString(), ChatState.MAIN_MENU); - workoutExerciseService.setName(selectedWorkoutExerciseCache.getSelectedWorkoutExercise(message.getChatId().toString()), message.getText()); + workoutExerciseService.setName(Long.valueOf(selectedWorkoutExerciseCache.getSelectedWorkoutExercise(message.getChatId().toString())), message.getText()); - return workoutDayMessageGenerator.getSelectWorkoutDaySendMessage(message.getChatId().toString(), selectedWorkoutDayCache.getSelectedWorkoutDay(message.getChatId().toString())); + return workoutDayMessageGenerator.getSelectWorkoutDaySendMessage(message.getChatId().toString(), Long.valueOf(selectedWorkoutDayCache.getSelectedWorkoutDay(message.getChatId().toString()))); } } diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/WorkoutSetCallbackQueryHandler.java b/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/WorkoutSetCallbackQueryHandler.java index 7f174ab..ab10ce6 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/WorkoutSetCallbackQueryHandler.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/WorkoutSetCallbackQueryHandler.java @@ -1,12 +1,12 @@ 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.SelectedWorkoutExerciseCache; import com.mykhailotiutiun.repcounterbot.constants.CallbackHandlerType; import com.mykhailotiutiun.repcounterbot.constants.ChatState; -import com.mykhailotiutiun.repcounterbot.util.LocaleMessageUtil; import com.mykhailotiutiun.repcounterbot.message.MainMenuMessageGenerator; +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; diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/WorkoutSetMessageHandler.java b/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/WorkoutSetMessageHandler.java index cabd8d6..8af7789 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/WorkoutSetMessageHandler.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/WorkoutSetMessageHandler.java @@ -1,8 +1,8 @@ package com.mykhailotiutiun.repcounterbot.botapi.handler.Impl; import com.mykhailotiutiun.repcounterbot.botapi.handler.MessageHandler; -import com.mykhailotiutiun.repcounterbot.cache.SelectedWorkoutExerciseCache; import com.mykhailotiutiun.repcounterbot.cache.CurrentBotStateCache; +import com.mykhailotiutiun.repcounterbot.cache.SelectedWorkoutExerciseCache; import com.mykhailotiutiun.repcounterbot.constants.ChatState; import com.mykhailotiutiun.repcounterbot.constants.MessageHandlerType; import com.mykhailotiutiun.repcounterbot.message.WorkoutExerciseMessageGenerator; @@ -57,8 +57,8 @@ private SendMessage handleFastSetsSetRequest(Message message) { } String chatId = message.getChatId().toString(); - workoutExerciseService.addSets(selectedWorkoutExerciseCache.getSelectedWorkoutExercise(chatId), workoutSets); + workoutExerciseService.addSets(Long.valueOf(selectedWorkoutExerciseCache.getSelectedWorkoutExercise(chatId)), workoutSets); - return workoutExerciseMessageGenerator.getWorkoutExerciseSendMessage(chatId, selectedWorkoutExerciseCache.getSelectedWorkoutExercise(chatId)); + return workoutExerciseMessageGenerator.getWorkoutExerciseSendMessage(chatId, Long.valueOf(selectedWorkoutExerciseCache.getSelectedWorkoutExercise(chatId))); } } diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/WorkoutWeekCallbackQueryHandler.java b/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/WorkoutWeekCallbackQueryHandler.java index 127d308..df10f52 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/WorkoutWeekCallbackQueryHandler.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/WorkoutWeekCallbackQueryHandler.java @@ -3,7 +3,6 @@ import com.mykhailotiutiun.repcounterbot.botapi.handler.CallbackQueryHandler; import com.mykhailotiutiun.repcounterbot.constants.CallbackHandlerType; import com.mykhailotiutiun.repcounterbot.message.WorkoutWeekMessageGenerator; -import com.mykhailotiutiun.repcounterbot.service.WorkoutWeekService; import org.springframework.stereotype.Component; import org.telegram.telegrambots.meta.api.methods.BotApiMethod; import org.telegram.telegrambots.meta.api.objects.CallbackQuery; diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/WorkoutWeekMessageHandler.java b/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/WorkoutWeekMessageHandler.java index e05c929..e0cd290 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/WorkoutWeekMessageHandler.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/botapi/handler/Impl/WorkoutWeekMessageHandler.java @@ -3,7 +3,6 @@ import com.mykhailotiutiun.repcounterbot.botapi.handler.MessageHandler; import com.mykhailotiutiun.repcounterbot.constants.MessageHandlerType; import com.mykhailotiutiun.repcounterbot.message.WorkoutWeekMessageGenerator; -import com.mykhailotiutiun.repcounterbot.service.WorkoutWeekService; import org.springframework.stereotype.Component; import org.telegram.telegrambots.meta.api.methods.BotApiMethod; import org.telegram.telegrambots.meta.api.objects.Message; diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/config/JdbcConfig.java b/src/main/java/com/mykhailotiutiun/repcounterbot/config/JdbcConfig.java new file mode 100644 index 0000000..9693931 --- /dev/null +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/config/JdbcConfig.java @@ -0,0 +1,16 @@ +package com.mykhailotiutiun.repcounterbot.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.jdbc.core.JdbcTemplate; + +import javax.sql.DataSource; + +@Configuration +public class JdbcConfig { + + @Bean + public JdbcTemplate getJdbcTemplate(DataSource dataSource){ + return new JdbcTemplate(dataSource); + } +} diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/controller/WebhookController.java b/src/main/java/com/mykhailotiutiun/repcounterbot/controller/MainController.java similarity index 88% rename from src/main/java/com/mykhailotiutiun/repcounterbot/controller/WebhookController.java rename to src/main/java/com/mykhailotiutiun/repcounterbot/controller/MainController.java index f063277..7719041 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/controller/WebhookController.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/controller/MainController.java @@ -10,11 +10,11 @@ @Slf4j @RestController -public class WebhookController { +public class MainController { private final RepCounterBot repCounterBot; - public WebhookController(RepCounterBot repCounterBot) { + public MainController(RepCounterBot repCounterBot) { this.repCounterBot = repCounterBot; } diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/language/SelectedLanguageProvider.java b/src/main/java/com/mykhailotiutiun/repcounterbot/language/SelectedLanguageProvider.java new file mode 100644 index 0000000..78e4c1b --- /dev/null +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/language/SelectedLanguageProvider.java @@ -0,0 +1,5 @@ +package com.mykhailotiutiun.repcounterbot.language; + +public interface SelectedLanguageProvider { + String getLocaleTag(String chatId); +} diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/mapper/UserMapper.java b/src/main/java/com/mykhailotiutiun/repcounterbot/mapper/UserMapper.java new file mode 100644 index 0000000..faf79e8 --- /dev/null +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/mapper/UserMapper.java @@ -0,0 +1,18 @@ +package com.mykhailotiutiun.repcounterbot.mapper; + +import com.mykhailotiutiun.repcounterbot.model.User; +import org.springframework.jdbc.core.RowMapper; + +import java.sql.ResultSet; +import java.sql.SQLException; + +public class UserMapper implements RowMapper { + @Override + public User mapRow(ResultSet rs, int rowNum) throws SQLException { + return User.builder() + .id(rs.getLong("id")) + .username(rs.getString("username")) + .localTag(rs.getString("local_tag")) + .build(); + } +} diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/mapper/WorkoutDayMapper.java b/src/main/java/com/mykhailotiutiun/repcounterbot/mapper/WorkoutDayMapper.java new file mode 100644 index 0000000..e342164 --- /dev/null +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/mapper/WorkoutDayMapper.java @@ -0,0 +1,21 @@ +package com.mykhailotiutiun.repcounterbot.mapper; + +import com.mykhailotiutiun.repcounterbot.model.WorkoutDay; +import com.mykhailotiutiun.repcounterbot.model.WorkoutWeek; +import org.springframework.jdbc.core.RowMapper; + +import java.sql.ResultSet; +import java.sql.SQLException; + +public class WorkoutDayMapper implements RowMapper { + @Override + public WorkoutDay mapRow(ResultSet rs, int rowNum) throws SQLException { + return WorkoutDay.builder() + .id(rs.getLong("id")) + .name(rs.getString("name")) + .isWorkoutDay(rs.getBoolean("is_workout_day")) + .date(rs.getDate("date").toLocalDate()) + .workoutWeek(WorkoutWeek.builder().id(rs.getLong("workout_week_id")).build()) + .build(); + } +} diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/mapper/WorkoutExerciseMapper.java b/src/main/java/com/mykhailotiutiun/repcounterbot/mapper/WorkoutExerciseMapper.java new file mode 100644 index 0000000..642c2c7 --- /dev/null +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/mapper/WorkoutExerciseMapper.java @@ -0,0 +1,23 @@ +package com.mykhailotiutiun.repcounterbot.mapper; + +import com.mykhailotiutiun.repcounterbot.model.WorkoutDay; +import com.mykhailotiutiun.repcounterbot.model.WorkoutExercise; +import org.springframework.jdbc.core.RowMapper; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; + +public class WorkoutExerciseMapper implements RowMapper { + @Override + public WorkoutExercise mapRow(ResultSet rs, int rowNum) throws SQLException { + return WorkoutExercise.builder() + .id(rs.getLong("id")) + .name(rs.getString("name")) + .number(rs.getByte("number")) + .workoutDay(WorkoutDay.builder().id(rs.getLong("workout_day_id")).build()) + .workoutSets(new ArrayList<>()) + .prevWorkoutSets(new ArrayList<>()) + .build(); + } +} diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/mapper/WorkoutSetMapper.java b/src/main/java/com/mykhailotiutiun/repcounterbot/mapper/WorkoutSetMapper.java new file mode 100644 index 0000000..ed66585 --- /dev/null +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/mapper/WorkoutSetMapper.java @@ -0,0 +1,19 @@ +package com.mykhailotiutiun.repcounterbot.mapper; + +import com.mykhailotiutiun.repcounterbot.model.WorkoutSet; +import org.springframework.jdbc.core.RowMapper; + +import java.sql.ResultSet; +import java.sql.SQLException; + +public class WorkoutSetMapper implements RowMapper { + @Override + public WorkoutSet mapRow(ResultSet rs, int rowNum) throws SQLException { + return WorkoutSet.builder() + .id(rs.getLong("id")) + .number(rs.getInt("number")) + .weight(rs.getInt("weight")) + .reps(rs.getInt("reps")) + .build(); + } +} diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/mapper/WorkoutWeekMapper.java b/src/main/java/com/mykhailotiutiun/repcounterbot/mapper/WorkoutWeekMapper.java new file mode 100644 index 0000000..253fbf3 --- /dev/null +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/mapper/WorkoutWeekMapper.java @@ -0,0 +1,21 @@ +package com.mykhailotiutiun.repcounterbot.mapper; + +import com.mykhailotiutiun.repcounterbot.model.User; +import com.mykhailotiutiun.repcounterbot.model.WorkoutWeek; +import org.springframework.jdbc.core.RowMapper; + +import java.sql.ResultSet; +import java.sql.SQLException; + +public class WorkoutWeekMapper implements RowMapper { + @Override + public WorkoutWeek mapRow(ResultSet rs, int rowNum) throws SQLException { + return WorkoutWeek.builder() + .id(rs.getLong("id")) + .current(rs.getBoolean("current")) + .weekStartDate(rs.getDate("week_start_date").toLocalDate()) + .weekEndDate(rs.getDate("week_end_date").toLocalDate()) + .user(User.builder().id(rs.getLong("user_id")).build()) + .build(); + } +} diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/message/WorkoutDayMessageGenerator.java b/src/main/java/com/mykhailotiutiun/repcounterbot/message/WorkoutDayMessageGenerator.java index c02fcbb..f788095 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/message/WorkoutDayMessageGenerator.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/message/WorkoutDayMessageGenerator.java @@ -5,6 +5,6 @@ public interface WorkoutDayMessageGenerator { - SendMessage getSelectWorkoutDaySendMessage(String chatId, String workoutDayId); - EditMessageText getSelectWorkoutDayEditMessage(String chatId, Integer messageId, String workoutDayId); + SendMessage getSelectWorkoutDaySendMessage(String chatId, Long workoutDayId); + EditMessageText getSelectWorkoutDayEditMessage(String chatId, Integer messageId, Long workoutDayId); } diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/message/WorkoutExerciseMessageGenerator.java b/src/main/java/com/mykhailotiutiun/repcounterbot/message/WorkoutExerciseMessageGenerator.java index 37af420..94c56d8 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/message/WorkoutExerciseMessageGenerator.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/message/WorkoutExerciseMessageGenerator.java @@ -5,6 +5,6 @@ public interface WorkoutExerciseMessageGenerator { - SendMessage getWorkoutExerciseSendMessage(String chatId, String workoutExerciseId); - EditMessageText getWorkoutExerciseEditMessage(String chatId, Integer messageId, String workoutExerciseId, Boolean isOnEditPage); + SendMessage getWorkoutExerciseSendMessage(String chatId, Long workoutExerciseId); + EditMessageText getWorkoutExerciseEditMessage(String chatId, Integer messageId, Long workoutExerciseId, Boolean isOnEditPage); } diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/message/impl/MainMenuMessageGeneratorImpl.java b/src/main/java/com/mykhailotiutiun/repcounterbot/message/impl/MainMenuMessageGeneratorImpl.java index 95a93e0..fa99645 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/message/impl/MainMenuMessageGeneratorImpl.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/message/impl/MainMenuMessageGeneratorImpl.java @@ -2,9 +2,7 @@ import com.mykhailotiutiun.repcounterbot.message.MainMenuMessageGenerator; import com.mykhailotiutiun.repcounterbot.util.LocaleMessageUtil; -import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; -import org.springframework.stereotype.Service; 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.replykeyboard.InlineKeyboardMarkup; diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/message/impl/WorkoutDayMessageGeneratorImpl.java b/src/main/java/com/mykhailotiutiun/repcounterbot/message/impl/WorkoutDayMessageGeneratorImpl.java index 2bcfca1..c69b8e8 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/message/impl/WorkoutDayMessageGeneratorImpl.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/message/impl/WorkoutDayMessageGeneratorImpl.java @@ -30,7 +30,7 @@ public WorkoutDayMessageGeneratorImpl(WorkoutDayService workoutDayService, Local } @Override - public SendMessage getSelectWorkoutDaySendMessage(String chatId, String workoutDayId) { + public SendMessage getSelectWorkoutDaySendMessage(String chatId, Long workoutDayId) { WorkoutDay workoutDay = workoutDayService.getById(workoutDayId); SendMessage sendMessage = new SendMessage(chatId, workoutDay.print(localeMessageUtil.getMessage("print.workout-day.is-rest-day", chatId), localeMessageUtil.getMessage("print.workout-day.type-not-set", chatId), localeMessageUtil.getLocalTag(chatId))); sendMessage.setReplyMarkup(getInlineKeyboardMarkupForWorkoutDay(workoutDay, workoutExerciseService.getAllByWorkoutDay(workoutDay), chatId)); @@ -39,7 +39,7 @@ public SendMessage getSelectWorkoutDaySendMessage(String chatId, String workoutD } @Override - public EditMessageText getSelectWorkoutDayEditMessage(String chatId, Integer messageId, String workoutDayId) { + public EditMessageText getSelectWorkoutDayEditMessage(String chatId, Integer messageId, Long workoutDayId) { WorkoutDay workoutDay = workoutDayService.getById(workoutDayId); EditMessageText editMessageText = new EditMessageText(workoutDay.print(localeMessageUtil.getMessage("print.workout-day.is-rest-day", chatId), localeMessageUtil.getMessage("print.workout-day.type-not-set", chatId), localeMessageUtil.getLocalTag(chatId))); editMessageText.setChatId(chatId); diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/message/impl/WorkoutExerciseMessageGeneratorImpl.java b/src/main/java/com/mykhailotiutiun/repcounterbot/message/impl/WorkoutExerciseMessageGeneratorImpl.java index b15ea2e..3af3cf4 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/message/impl/WorkoutExerciseMessageGeneratorImpl.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/message/impl/WorkoutExerciseMessageGeneratorImpl.java @@ -25,7 +25,7 @@ public WorkoutExerciseMessageGeneratorImpl(LocaleMessageUtil localeMessageUtil, } @Override - public SendMessage getWorkoutExerciseSendMessage(String chatId, String workoutExerciseId) { + public SendMessage getWorkoutExerciseSendMessage(String chatId, Long workoutExerciseId) { WorkoutExercise workoutExercise = workoutExerciseService.getById(workoutExerciseId); SendMessage sendMessage = new SendMessage(chatId, workoutExercise.printForWorkoutExercise(localeMessageUtil.getMessage("print.workout-exercise.for-workout-exercise-reply", chatId), localeMessageUtil.getMessage("print.workout-set.pattern", chatId))); @@ -34,7 +34,7 @@ public SendMessage getWorkoutExerciseSendMessage(String chatId, String workoutEx } @Override - public EditMessageText getWorkoutExerciseEditMessage(String chatId, Integer messageId, String workoutExerciseId, Boolean isOnEditPage) { + public EditMessageText getWorkoutExerciseEditMessage(String chatId, Integer messageId, Long workoutExerciseId, Boolean isOnEditPage) { WorkoutExercise workoutExercise = workoutExerciseService.getById(workoutExerciseId); EditMessageText editMessageText = new EditMessageText(workoutExercise.printForWorkoutExercise(localeMessageUtil.getMessage("print.workout-exercise.for-workout-exercise-reply", chatId), localeMessageUtil.getMessage("print.workout-set.pattern", chatId))); diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/model/WorkoutDay.java b/src/main/java/com/mykhailotiutiun/repcounterbot/model/WorkoutDay.java index 4c602d6..7c56d5c 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/model/WorkoutDay.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/model/WorkoutDay.java @@ -17,11 +17,11 @@ @AllArgsConstructor public class WorkoutDay { - private String id; - private WorkoutWeek workoutWeek; + private Long id; private String name; private LocalDate date; private Boolean isWorkoutDay; + private WorkoutWeek workoutWeek; public Boolean isWorkoutDay() { return isWorkoutDay; diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/model/WorkoutExercise.java b/src/main/java/com/mykhailotiutiun/repcounterbot/model/WorkoutExercise.java index be3cd56..13b2442 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/model/WorkoutExercise.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/model/WorkoutExercise.java @@ -13,7 +13,7 @@ @AllArgsConstructor public class WorkoutExercise { - private String id; + private Long id; private Byte number; private String name; private WorkoutDay workoutDay; diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/model/WorkoutSet.java b/src/main/java/com/mykhailotiutiun/repcounterbot/model/WorkoutSet.java index 66314a9..dd3f265 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/model/WorkoutSet.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/model/WorkoutSet.java @@ -10,7 +10,7 @@ @AllArgsConstructor public class WorkoutSet { - private String id; + private Long id; private Integer number; private Integer reps; private Integer weight; diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/model/WorkoutWeek.java b/src/main/java/com/mykhailotiutiun/repcounterbot/model/WorkoutWeek.java index bf61e14..d40db7f 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/model/WorkoutWeek.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/model/WorkoutWeek.java @@ -13,7 +13,7 @@ @AllArgsConstructor public class WorkoutWeek { - private String id; + private Long id; private User user; private Boolean current = true; private LocalDate weekStartDate; diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/repository/WorkoutDayRepository.java b/src/main/java/com/mykhailotiutiun/repcounterbot/repository/WorkoutDayRepository.java index dc2bef3..3f66d52 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/repository/WorkoutDayRepository.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/repository/WorkoutDayRepository.java @@ -8,8 +8,8 @@ public interface WorkoutDayRepository { - Optional findById(String id); - List findAllByWorkoutWeek(WorkoutWeek WorkoutWeek); + Optional findById(Long id); + List findAllByWorkoutWeek(WorkoutWeek workoutWeek); WorkoutDay save(WorkoutDay workoutDay); } diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/repository/WorkoutExerciseRepository.java b/src/main/java/com/mykhailotiutiun/repcounterbot/repository/WorkoutExerciseRepository.java index 606503e..36f9137 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/repository/WorkoutExerciseRepository.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/repository/WorkoutExerciseRepository.java @@ -8,13 +8,12 @@ public interface WorkoutExerciseRepository { - Optional findById(String id); + Optional findById(Long id); Optional findByNumberAndWorkoutDay(Byte number, WorkoutDay workoutDay); List findAllByWorkoutDayOrderByNumberDesc(WorkoutDay workoutDay); - List findAllByWorkoutDay(WorkoutDay workoutDay); WorkoutExercise save(WorkoutExercise workoutExercise); - void deleteById(String id); + void deleteById(Long id); } diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/repository/WorkoutSetRepository.java b/src/main/java/com/mykhailotiutiun/repcounterbot/repository/WorkoutSetRepository.java index c09847f..5c8a37f 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/repository/WorkoutSetRepository.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/repository/WorkoutSetRepository.java @@ -1,8 +1,13 @@ package com.mykhailotiutiun.repcounterbot.repository; +import com.mykhailotiutiun.repcounterbot.model.WorkoutExercise; import com.mykhailotiutiun.repcounterbot.model.WorkoutSet; +import java.util.List; + public interface WorkoutSetRepository { + List findAllByWorkoutExercise(WorkoutExercise workoutExercise); + List findAllPrevSetsByWorkoutExercise(WorkoutExercise workoutExercise); WorkoutSet save(WorkoutSet workoutSet); } diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/repository/WorkoutWeekRepository.java b/src/main/java/com/mykhailotiutiun/repcounterbot/repository/WorkoutWeekRepository.java index cc6d87d..28c51af 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/repository/WorkoutWeekRepository.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/repository/WorkoutWeekRepository.java @@ -6,6 +6,7 @@ public interface WorkoutWeekRepository { + Optional findById(Long id); Optional findByUserIdAndCurrent(Long userId, Boolean isCurrent); WorkoutWeek save(WorkoutWeek workoutWeek); diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/repository/impl/UserRepositoryImpl.java b/src/main/java/com/mykhailotiutiun/repcounterbot/repository/impl/UserRepositoryImpl.java new file mode 100644 index 0000000..5fb07d0 --- /dev/null +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/repository/impl/UserRepositoryImpl.java @@ -0,0 +1,39 @@ +package com.mykhailotiutiun.repcounterbot.repository.impl; + +import com.mykhailotiutiun.repcounterbot.mapper.UserMapper; +import com.mykhailotiutiun.repcounterbot.model.User; +import com.mykhailotiutiun.repcounterbot.repository.UserRepository; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Repository; + +import java.util.Optional; + +@Repository +public class UserRepositoryImpl implements UserRepository { + + private final JdbcTemplate jdbcTemplate; + + public UserRepositoryImpl(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + @Override + public Optional findById(Long id) { + return Optional.ofNullable(jdbcTemplate.queryForObject("SELECT * FROM bot_users WHERE id = ?", new UserMapper(), id)); + } + + @Override + public boolean existsById(Long id) { + return Boolean.TRUE.equals(jdbcTemplate.queryForObject("SELECT EXISTS(SELECT id FROM bot_users WHERE id = ?)", Boolean.class, id)); + } + + @Override + public User save(User user) { + if (existsById(user.getId())) { + jdbcTemplate.update("UPDATE bot_users SET username = ?, local_tag = ? WHERE id = ?", user.getUsername(), user.getLocalTag(), user.getId()); + } else { + jdbcTemplate.update("INSERT INTO bot_users (id, username, local_tag) VALUES (?, ?, ?)", user.getId(), user.getUsername(), user.getLocalTag()); + } + return user; + } +} \ No newline at end of file diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/repository/impl/WorkoutDayRepositoryImpl.java b/src/main/java/com/mykhailotiutiun/repcounterbot/repository/impl/WorkoutDayRepositoryImpl.java new file mode 100644 index 0000000..fd88a4b --- /dev/null +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/repository/impl/WorkoutDayRepositoryImpl.java @@ -0,0 +1,73 @@ +package com.mykhailotiutiun.repcounterbot.repository.impl; + +import com.mykhailotiutiun.repcounterbot.exception.EntityNotFoundException; +import com.mykhailotiutiun.repcounterbot.mapper.WorkoutDayMapper; +import com.mykhailotiutiun.repcounterbot.mapper.WorkoutWeekMapper; +import com.mykhailotiutiun.repcounterbot.model.WorkoutDay; +import com.mykhailotiutiun.repcounterbot.model.WorkoutWeek; +import com.mykhailotiutiun.repcounterbot.repository.WorkoutDayRepository; +import com.mykhailotiutiun.repcounterbot.repository.WorkoutWeekRepository; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.simple.SimpleJdbcInsert; +import org.springframework.stereotype.Repository; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +@Repository +public class WorkoutDayRepositoryImpl implements WorkoutDayRepository { + + private final JdbcTemplate jdbcTemplate; + private final WorkoutWeekRepository workoutWeekRepository; + + public WorkoutDayRepositoryImpl(JdbcTemplate jdbcTemplate, WorkoutWeekRepository workoutWeekRepository) { + this.jdbcTemplate = jdbcTemplate; + this.workoutWeekRepository = workoutWeekRepository; + } + + private boolean existsById(Long id) { + return Boolean.TRUE.equals(jdbcTemplate.queryForObject("SELECT EXISTS(SELECT 1 FROM workout_days WHERE id = ?)", Boolean.class, id)); + } + + @Override + public Optional findById(Long id) { + WorkoutDay workoutDay = jdbcTemplate.queryForObject("SELECT * FROM workout_days WHERE id = ?", new WorkoutDayMapper(), id); + if (workoutDay != null) { + workoutDay.setWorkoutWeek(workoutWeekRepository.findById(workoutDay.getWorkoutWeek().getId()).orElseThrow(EntityNotFoundException::new)); + } + return Optional.ofNullable(workoutDay); + } + + @Override + public List findAllByWorkoutWeek(WorkoutWeek workoutWeek) { + List workoutDays = jdbcTemplate.query("SELECT * FROM workout_days WHERE workout_week_id = ?", new WorkoutDayMapper(), workoutWeek.getId()); + if (!workoutDays.isEmpty()){ + WorkoutWeek workoutWeekFromDb = jdbcTemplate.queryForObject("SELECT * FROM workout_weeks where id = ?", new WorkoutWeekMapper(), workoutWeek.getId()); + workoutDays.forEach(workoutDay -> workoutDay.setWorkoutWeek(workoutWeekFromDb)); + } + return workoutDays; + } + + @Override + public WorkoutDay save(WorkoutDay workoutDay) { + if(existsById(workoutDay.getId())){ + jdbcTemplate.update("UPDATE workout_days SET name = ?, is_workout_day = ?, date = ?, workout_week_id = ? WHERE id = ?", + workoutDay.getName(), + workoutDay.getIsWorkoutDay(), + workoutDay.getDate(), + workoutDay.getWorkoutWeek().getId(), + workoutDay.getId()); + } else { + SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(jdbcTemplate).withTableName("workout_days").usingGeneratedKeyColumns("id"); + Map parameters = new HashMap<>(4); + parameters.put("name", workoutDay.getName()); + parameters.put("date", workoutDay.getDate()); + parameters.put("is_workout_day", workoutDay.isWorkoutDay()); + parameters.put("workout_week_id", workoutDay.getWorkoutWeek().getId()); + workoutDay.setId((Long) simpleJdbcInsert.executeAndReturnKey(parameters)); + } + return workoutDay; + } +} diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/repository/impl/WorkoutExerciseRepositoryImpl.java b/src/main/java/com/mykhailotiutiun/repcounterbot/repository/impl/WorkoutExerciseRepositoryImpl.java new file mode 100644 index 0000000..11e7585 --- /dev/null +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/repository/impl/WorkoutExerciseRepositoryImpl.java @@ -0,0 +1,119 @@ +package com.mykhailotiutiun.repcounterbot.repository.impl; + +import com.mykhailotiutiun.repcounterbot.exception.EntityNotFoundException; +import com.mykhailotiutiun.repcounterbot.mapper.WorkoutExerciseMapper; +import com.mykhailotiutiun.repcounterbot.model.WorkoutDay; +import com.mykhailotiutiun.repcounterbot.model.WorkoutExercise; +import com.mykhailotiutiun.repcounterbot.model.WorkoutSet; +import com.mykhailotiutiun.repcounterbot.repository.WorkoutDayRepository; +import com.mykhailotiutiun.repcounterbot.repository.WorkoutExerciseRepository; +import com.mykhailotiutiun.repcounterbot.repository.WorkoutSetRepository; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.simple.SimpleJdbcInsert; +import org.springframework.stereotype.Repository; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Collectors; + +@Repository +public class WorkoutExerciseRepositoryImpl implements WorkoutExerciseRepository { + + private final JdbcTemplate jdbcTemplate; + private final WorkoutDayRepository workoutDayRepository; + private final WorkoutSetRepository workoutSetRepository; + + public WorkoutExerciseRepositoryImpl(JdbcTemplate jdbcTemplate, WorkoutDayRepository workoutDayRepository, WorkoutSetRepository workoutSetRepository) { + this.jdbcTemplate = jdbcTemplate; + this.workoutDayRepository = workoutDayRepository; + this.workoutSetRepository = workoutSetRepository; + } + + @Override + public Optional findById(Long id) { + WorkoutExercise workoutExercise = jdbcTemplate.queryForObject("SELECT * FROM workout_exercises WHERE id = ?", new WorkoutExerciseMapper(), id); + if (workoutExercise != null) { + workoutExercise.setWorkoutDay(workoutDayRepository.findById(workoutExercise.getWorkoutDay().getId()).orElseThrow(EntityNotFoundException::new)); + workoutExercise.setWorkoutSets(workoutSetRepository.findAllByWorkoutExercise(workoutExercise)); + } + return Optional.ofNullable(workoutExercise); + } + + @Override + public Optional findByNumberAndWorkoutDay(Byte number, WorkoutDay workoutDay) { + WorkoutExercise workoutExercise = jdbcTemplate.queryForObject("SELECT * FROM workout_exercises WHERE number = ? AND workout_day_id = ?", new WorkoutExerciseMapper(), number, workoutDay.getId()); + if (workoutExercise != null) { + workoutExercise.setWorkoutDay(workoutDayRepository.findById(workoutExercise.getWorkoutDay().getId()).orElseThrow(EntityNotFoundException::new)); + workoutExercise.setWorkoutSets(workoutSetRepository.findAllByWorkoutExercise(workoutExercise)); + workoutExercise.setPrevWorkoutSets(workoutSetRepository.findAllPrevSetsByWorkoutExercise(workoutExercise)); + } + return Optional.ofNullable(workoutExercise); + } + + @Override + public List findAllByWorkoutDayOrderByNumberDesc(WorkoutDay workoutDay) { + return jdbcTemplate.query("SELECT * FROM workout_exercises WHERE workout_day_id = ? ORDER BY number DESC", new WorkoutExerciseMapper(), workoutDay.getId()); + } + + private boolean existsById(Long id) { + return Boolean.TRUE.equals(jdbcTemplate.queryForObject("SELECT EXISTS(SELECT 1 FROM workout_exercises WHERE id = ?)", Boolean.class, id)); + } + + @Override + public WorkoutExercise save(WorkoutExercise workoutExercise) { + if(existsById(workoutExercise.getId())){ + jdbcTemplate.update("UPDATE workout_exercises SET number = ?, name = ?, workout_day_id = ? WHERE id = ?", + workoutExercise.getNumber(), + workoutExercise.getName(), + workoutExercise.getWorkoutDay().getId(), + workoutExercise.getId()); + } else { + SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(jdbcTemplate).withTableName("workout_exercises").usingGeneratedKeyColumns("id"); + Map parameters = new HashMap<>(3); + parameters.put("number", workoutExercise.getNumber()); + parameters.put("workout_day_id", workoutExercise.getWorkoutDay().getId()); + parameters.put("name", workoutExercise.getName()); + workoutExercise.setId((Long) simpleJdbcInsert.executeAndReturnKey(parameters)); + } + updateWorkoutSets(workoutExercise); + updatePrevWorkoutSets(workoutExercise); + return workoutExercise; + } + + private void updateWorkoutSets(WorkoutExercise workoutExercise){ + if (workoutExercise.getWorkoutSets() == null || workoutExercise.getWorkoutSets().isEmpty()){ + return; + } + List workoutSets = workoutExercise.getWorkoutSets(); + String sql = "UPDATE workout_sets SET workout_ex_id = ? WHERE id IN (" + + workoutSets.stream().map(id -> "?").collect(Collectors.joining(",")) + ")"; + Object[] params = new Object[workoutSets.size() + 1]; + params[0] = workoutExercise.getId(); + for (int i = 0; i < workoutSets.size(); i++){ + params[i + 1] = workoutSets.get(i).getId(); + } + jdbcTemplate.update(sql, params); + } + + private void updatePrevWorkoutSets(WorkoutExercise workoutExercise) { + if (workoutExercise.getPrevWorkoutSets() == null || workoutExercise.getPrevWorkoutSets().isEmpty()){ + return; + } + List workoutSets = workoutExercise.getPrevWorkoutSets(); + String sql = "UPDATE workout_sets SET prev_workout_ex_id = ? WHERE id IN (" + + workoutSets.stream().map(id -> "?").collect(Collectors.joining(",")) + ")"; + Object[] params = new Object[workoutSets.size() + 1]; + params[0] = workoutExercise.getId(); + for (int i = 0; i < workoutSets.size(); i++){ + params[i + 1] = workoutSets.get(i).getId(); + } + jdbcTemplate.update(sql, params); + } + + @Override + public void deleteById(Long id) { + jdbcTemplate.update("DELETE FROM workout_exercises WHERE id = ?", id); + } +} diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/repository/impl/WorkoutSetRepositoryImpl.java b/src/main/java/com/mykhailotiutiun/repcounterbot/repository/impl/WorkoutSetRepositoryImpl.java new file mode 100644 index 0000000..e81d944 --- /dev/null +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/repository/impl/WorkoutSetRepositoryImpl.java @@ -0,0 +1,56 @@ +package com.mykhailotiutiun.repcounterbot.repository.impl; + +import com.mykhailotiutiun.repcounterbot.mapper.WorkoutSetMapper; +import com.mykhailotiutiun.repcounterbot.model.WorkoutExercise; +import com.mykhailotiutiun.repcounterbot.model.WorkoutSet; +import com.mykhailotiutiun.repcounterbot.repository.WorkoutSetRepository; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.simple.SimpleJdbcInsert; +import org.springframework.stereotype.Repository; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Repository +public class WorkoutSetRepositoryImpl implements WorkoutSetRepository { + + private final JdbcTemplate jdbcTemplate; + + public WorkoutSetRepositoryImpl(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + private boolean existsById(Long id) { + return Boolean.TRUE.equals(jdbcTemplate.queryForObject("SELECT EXISTS(SELECT 1 FROM workout_sets WHERE id = ?)", Boolean.class, id)); + } + + @Override + public List findAllByWorkoutExercise(WorkoutExercise workoutExercise) { + return jdbcTemplate.query("SELECT * FROM workout_sets WHERE workout_ex_id = ?", new WorkoutSetMapper(), workoutExercise.getId()); + } + + @Override + public List findAllPrevSetsByWorkoutExercise(WorkoutExercise workoutExercise) { + return jdbcTemplate.query("SELECT * FROM workout_sets WHERE prev_workout_ex_id = ?", new WorkoutSetMapper(), workoutExercise.getId()); + } + + @Override + public WorkoutSet save(WorkoutSet workoutSet) { + if(existsById(workoutSet.getId())) { + jdbcTemplate.update("UPDATE workout_sets SET number = ?, weight = ?, reps = ? WHERE id = ?", + workoutSet.getNumber(), + workoutSet.getWeight(), + workoutSet.getReps(), + workoutSet.getId()); + } else { + SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(jdbcTemplate).withTableName("workout_sets").usingGeneratedKeyColumns("id"); + Map parameters = new HashMap<>(3); + parameters.put("number", workoutSet.getNumber()); + parameters.put("reps", workoutSet.getReps()); + parameters.put("weight", workoutSet.getWeight()); + workoutSet.setId((Long) simpleJdbcInsert.executeAndReturnKey(parameters)); + } + return workoutSet; + } +} diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/repository/impl/WorkoutWeekRepositoryImpl.java b/src/main/java/com/mykhailotiutiun/repcounterbot/repository/impl/WorkoutWeekRepositoryImpl.java new file mode 100644 index 0000000..0fe6634 --- /dev/null +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/repository/impl/WorkoutWeekRepositoryImpl.java @@ -0,0 +1,66 @@ +package com.mykhailotiutiun.repcounterbot.repository.impl; + +import com.mykhailotiutiun.repcounterbot.mapper.UserMapper; +import com.mykhailotiutiun.repcounterbot.mapper.WorkoutWeekMapper; +import com.mykhailotiutiun.repcounterbot.model.WorkoutWeek; +import com.mykhailotiutiun.repcounterbot.repository.WorkoutWeekRepository; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.simple.SimpleJdbcInsert; +import org.springframework.stereotype.Repository; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +@Repository +public class WorkoutWeekRepositoryImpl implements WorkoutWeekRepository { + + private final JdbcTemplate jdbcTemplate; + + public WorkoutWeekRepositoryImpl(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + private boolean existsById(Long id) { + return Boolean.TRUE.equals(jdbcTemplate.queryForObject("SELECT EXISTS(SELECT 1 FROM workout_weeks WHERE id = ?)", Boolean.class, id)); + } + + @Override + public Optional findById(Long id) { + WorkoutWeek workoutWeek = jdbcTemplate.queryForObject("SELECT * FROM workout_weeks WHERE id = ?", new WorkoutWeekMapper(), id); + if(workoutWeek != null){ + workoutWeek.setUser(jdbcTemplate.queryForObject("SELECT * FROM bot_users WHERE id = ?", new UserMapper(), workoutWeek.getUser().getId())); + } + return Optional.ofNullable(workoutWeek); + } + + @Override + public Optional findByUserIdAndCurrent(Long userId, Boolean isCurrent) { + WorkoutWeek workoutWeek = jdbcTemplate.queryForObject("SELECT * FROM workout_weeks WHERE user_id = ? AND current = ?", new WorkoutWeekMapper(), userId, isCurrent); + if(workoutWeek != null){ + workoutWeek.setUser(jdbcTemplate.queryForObject("SELECT * FROM bot_users WHERE id = ?", new UserMapper(), userId)); + } + return Optional.ofNullable(workoutWeek); + } + + @Override + public WorkoutWeek save(WorkoutWeek workoutWeek) { + if(existsById(workoutWeek.getId())){ + jdbcTemplate.update("UPDATE workout_weeks SET current = ?, week_start_date = ?, week_end_date = ?, user_id = ? WHERE id = ?", + workoutWeek.getCurrent(), + workoutWeek.getWeekStartDate(), + workoutWeek.getWeekEndDate(), + workoutWeek.getUser().getId(), + workoutWeek.getId()); + } else { + SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(jdbcTemplate).withTableName("workout_weeks").usingGeneratedKeyColumns("id"); + Map parameters = new HashMap<>(4); + parameters.put("current", workoutWeek.getCurrent()); + parameters.put("week_start_date", workoutWeek.getWeekStartDate()); + parameters.put("week_end_date", workoutWeek.getWeekEndDate()); + parameters.put("user_id", workoutWeek.getUser().getId()); + workoutWeek.setId((Long) simpleJdbcInsert.executeAndReturnKey(parameters)); + } + return workoutWeek; + } +} diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/service/Impl/UserServiceImpl.java b/src/main/java/com/mykhailotiutiun/repcounterbot/service/Impl/UserServiceImpl.java index 84f6e7b..c237014 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/service/Impl/UserServiceImpl.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/service/Impl/UserServiceImpl.java @@ -3,13 +3,13 @@ import com.mykhailotiutiun.repcounterbot.cache.SelectedLanguageCache; import com.mykhailotiutiun.repcounterbot.exception.EntityAlreadyExistsException; import com.mykhailotiutiun.repcounterbot.exception.EntityNotFoundException; +import com.mykhailotiutiun.repcounterbot.language.SelectedLanguageProvider; import com.mykhailotiutiun.repcounterbot.model.User; import com.mykhailotiutiun.repcounterbot.model.WorkoutWeek; import com.mykhailotiutiun.repcounterbot.repository.UserRepository; import com.mykhailotiutiun.repcounterbot.service.UserService; import com.mykhailotiutiun.repcounterbot.service.WorkoutWeekService; import com.mykhailotiutiun.repcounterbot.util.LocalDateWeekUtil; -import com.mykhailotiutiun.repcounterbot.util.LocaleMessageUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -18,14 +18,14 @@ @Slf4j @Service -public class UserServiceImpl implements UserService { +public class UserServiceImpl implements UserService, SelectedLanguageProvider { private final UserRepository userRepository; private final WorkoutWeekService workoutWeekService; private final LocalDateWeekUtil localDateWeekUtil; private final SelectedLanguageCache selectedLanguageCache; - public UserServiceImpl(UserRepository userRepository, WorkoutWeekService workoutWeekService, LocalDateWeekUtil localDateWeekUtil, LocaleMessageUtil localeMessageUtil, SelectedLanguageCache selectedLanguageCache) { + public UserServiceImpl(UserRepository userRepository, WorkoutWeekService workoutWeekService, LocalDateWeekUtil localDateWeekUtil, SelectedLanguageCache selectedLanguageCache) { this.userRepository = userRepository; this.workoutWeekService = workoutWeekService; this.localDateWeekUtil = localDateWeekUtil; @@ -35,7 +35,7 @@ public UserServiceImpl(UserRepository userRepository, WorkoutWeekService workout @Override public User getById(Long id) { - log.trace("Get User with id: {}", id); + log.trace("Get User by id: {}", id); return userRepository.findById(id).orElseThrow(EntityNotFoundException::new); } @@ -43,7 +43,7 @@ public User getById(Long id) { @Transactional public void create(User user) throws EntityAlreadyExistsException { if (userRepository.existsById(user.getId())) { - throw new EntityAlreadyExistsException(String.format("User with this id(%d) already exists", user.getId())); + throw new EntityAlreadyExistsException(String.format("User with id(%d) already exists", user.getId())); } log.trace("Create User: {}", user); @@ -51,6 +51,7 @@ public void create(User user) throws EntityAlreadyExistsException { workoutWeekService.create(WorkoutWeek.builder() .user(user) + .current(true) .weekStartDate(localDateWeekUtil.getFirstDateOfWeekFromDate(LocalDate.now())) .weekEndDate(localDateWeekUtil.getLastDateOfWeekFromDate(LocalDate.now())) .build()); @@ -66,11 +67,16 @@ private void save(User user) { public void setUserLang(Long userId, String localTag) { User user = getById(userId); - log.trace("Set localTag to User: {}", user); + log.trace("Set localTag for User: {}", user); user.setLocalTag(localTag); save(user); selectedLanguageCache.setSelectedLanguage(String.valueOf(userId), localTag); } + @Override + public String getLocaleTag(String chatId) { + log.trace("Get User's local-tag by id: {}", chatId); + return userRepository.findById(Long.valueOf(chatId)).orElseThrow(EntityNotFoundException::new).getLocalTag(); + } } diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/service/Impl/WorkoutDayServiceImpl.java b/src/main/java/com/mykhailotiutiun/repcounterbot/service/Impl/WorkoutDayServiceImpl.java index ac0de15..39bfad2 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/service/Impl/WorkoutDayServiceImpl.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/service/Impl/WorkoutDayServiceImpl.java @@ -2,21 +2,14 @@ import com.mykhailotiutiun.repcounterbot.exception.EntityNotFoundException; import com.mykhailotiutiun.repcounterbot.model.WorkoutDay; -import com.mykhailotiutiun.repcounterbot.model.WorkoutExercise; import com.mykhailotiutiun.repcounterbot.model.WorkoutWeek; import com.mykhailotiutiun.repcounterbot.repository.WorkoutDayRepository; import com.mykhailotiutiun.repcounterbot.service.WorkoutDayService; import com.mykhailotiutiun.repcounterbot.service.WorkoutExerciseService; -import com.mykhailotiutiun.repcounterbot.util.LocaleMessageUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -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.replykeyboard.InlineKeyboardMarkup; -import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton; -import java.util.ArrayList; import java.util.Comparator; import java.util.List; @@ -26,29 +19,30 @@ public class WorkoutDayServiceImpl implements WorkoutDayService { private final WorkoutDayRepository workoutDayRepository; private final WorkoutExerciseService workoutExerciseService; - private final LocaleMessageUtil localeMessageUtil; - public WorkoutDayServiceImpl(WorkoutDayRepository workoutDayRepository, WorkoutExerciseService workoutExerciseService, LocaleMessageUtil localeMessageUtil) { + public WorkoutDayServiceImpl(WorkoutDayRepository workoutDayRepository, WorkoutExerciseService workoutExerciseService) { this.workoutDayRepository = workoutDayRepository; this.workoutExerciseService = workoutExerciseService; - this.localeMessageUtil = localeMessageUtil; } @Override - public WorkoutDay getById(String id) { - log.trace("Get WorkoutDay with id: {}", id); + public WorkoutDay getById(Long id) { + log.trace("Get WorkoutDay by id: {}", id); return workoutDayRepository.findById(id).orElseThrow(EntityNotFoundException::new); } @Override public List getAllByWorkoutWeek(WorkoutWeek workoutWeek) { log.trace("Get WorkoutDays by WorkoutWeek: {}", workoutWeek); - return workoutDayRepository.findAllByWorkoutWeek(workoutWeek); + List workoutDays = workoutDayRepository.findAllByWorkoutWeek(workoutWeek); + workoutDays.sort(Comparator.comparing(WorkoutDay::getDate)); + return workoutDays; } @Override @Transactional public void createAllFromOldWorkoutWeek(WorkoutWeek oldWorkoutWeek, WorkoutWeek newWorkoutWeek) { + log.trace("Create all WorkoutDays from old WorkoutWeek: {}, {}", oldWorkoutWeek, newWorkoutWeek); List workoutDays = getAllByWorkoutWeek(oldWorkoutWeek); for (int i = 0; i < workoutDays.size(); i++) { @@ -75,8 +69,8 @@ public void save(WorkoutDay workoutWeek) { @Override @Transactional - public void setName(String workoutDayId, String name) { - log.trace("Set workout name {} to WorkoutDay by id: {}", name, workoutDayId); + public void setName(Long workoutDayId, String name) { + log.trace("Set workout name {} for WorkoutDay by id: {}", name, workoutDayId); WorkoutDay workoutDay = getById(workoutDayId); workoutDay.setName(name); @@ -87,7 +81,7 @@ public void setName(String workoutDayId, String name) { @Override @Transactional - public void setRestWorkoutDay(String workoutDayId) { + public void setRestWorkoutDay(Long workoutDayId) { log.trace("Set rest for WorkoutDay by id: {}", workoutDayId); WorkoutDay workoutDay = getById(workoutDayId); diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/service/Impl/WorkoutExerciseServiceImpl.java b/src/main/java/com/mykhailotiutiun/repcounterbot/service/Impl/WorkoutExerciseServiceImpl.java index 63ee087..ce22ec3 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/service/Impl/WorkoutExerciseServiceImpl.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/service/Impl/WorkoutExerciseServiceImpl.java @@ -7,16 +7,10 @@ import com.mykhailotiutiun.repcounterbot.repository.WorkoutExerciseRepository; import com.mykhailotiutiun.repcounterbot.service.WorkoutExerciseService; import com.mykhailotiutiun.repcounterbot.service.WorkoutSetService; -import com.mykhailotiutiun.repcounterbot.util.LocaleMessageUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -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.replykeyboard.InlineKeyboardMarkup; -import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton; -import java.util.ArrayList; import java.util.Comparator; import java.util.List; @@ -26,17 +20,15 @@ public class WorkoutExerciseServiceImpl implements WorkoutExerciseService { private final WorkoutExerciseRepository workoutExerciseRepository; private final WorkoutSetService workoutSetService; - private final LocaleMessageUtil localeMessageUtil; - public WorkoutExerciseServiceImpl(WorkoutExerciseRepository workoutExerciseRepository, WorkoutSetService workoutSetService, LocaleMessageUtil localeMessageUtil) { + public WorkoutExerciseServiceImpl(WorkoutExerciseRepository workoutExerciseRepository, WorkoutSetService workoutSetService) { this.workoutExerciseRepository = workoutExerciseRepository; this.workoutSetService = workoutSetService; - this.localeMessageUtil = localeMessageUtil; } @Override - public WorkoutExercise getById(String id) { + public WorkoutExercise getById(Long id) { log.trace("Get WorkoutExercise by id: {}", id); return sortWorkoutSetsInWorkoutExercise(workoutExerciseRepository.findById(id).orElseThrow(EntityNotFoundException::new)); } @@ -67,7 +59,7 @@ public WorkoutExercise getByNumberAndWorkoutDay(Byte number, WorkoutDay workoutD @Override public List getAllByWorkoutDay(WorkoutDay workoutDay) { log.trace("Get WorkoutExercises by WorkoutDay: {}", workoutDay); - return workoutExerciseRepository.findAllByWorkoutDay(workoutDay); + return workoutExerciseRepository.findAllByWorkoutDayOrderByNumberDesc(workoutDay); } @Override @@ -103,7 +95,7 @@ private void save(WorkoutExercise workoutExercise) { @Override @Transactional - public void setName(String workoutExerciseId, String name){ + public void setName(Long workoutExerciseId, String name){ WorkoutExercise workoutExercise = getById(workoutExerciseId); log.trace("Set name WorkoutExercise: {}, {}", workoutExercise, name); @@ -113,7 +105,7 @@ public void setName(String workoutExerciseId, String name){ @Override @Transactional - public void moveUp(String workoutExerciseId){ + public void moveUp(Long workoutExerciseId){ WorkoutExercise workoutExercise = getById(workoutExerciseId); log.trace("Move up WorkoutExercise: {}", workoutExercise); @@ -127,7 +119,7 @@ public void moveUp(String workoutExerciseId){ @Override @Transactional - public void moveDown(String workoutExerciseId){ + public void moveDown(Long workoutExerciseId){ WorkoutExercise workoutExercise = getById(workoutExerciseId); log.trace("Move down WorkoutExercise: {}", workoutExercise); @@ -140,7 +132,7 @@ public void moveDown(String workoutExerciseId){ @Override @Transactional - public void addSets(String workoutExerciseId, List workoutSets) { + public void addSets(Long workoutExerciseId, List workoutSets) { WorkoutExercise workoutExercise = getById(workoutExerciseId); log.trace("Add WorkoutSets to WorkoutExercise: {}, {}", workoutExercise, workoutSets); @@ -151,7 +143,7 @@ public void addSets(String workoutExerciseId, List workoutSets) { } @Override - public void deleteById(String id) { + public void deleteById(Long id) { log.trace("Delete WorkoutExercise with id: {}", id); workoutExerciseRepository.deleteById(id); } diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/service/Impl/WorkoutSetServiceImpl.java b/src/main/java/com/mykhailotiutiun/repcounterbot/service/Impl/WorkoutSetServiceImpl.java index 722a440..bc09efe 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/service/Impl/WorkoutSetServiceImpl.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/service/Impl/WorkoutSetServiceImpl.java @@ -18,6 +18,7 @@ public WorkoutSetServiceImpl(WorkoutSetRepository workoutSetRepository) { @Override public void save(WorkoutSet workoutSet) { + log.trace("Save WorkoutSet: {}", workoutSet); workoutSetRepository.save(workoutSet); } diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/service/Impl/WorkoutWeekServiceImpl.java b/src/main/java/com/mykhailotiutiun/repcounterbot/service/Impl/WorkoutWeekServiceImpl.java index b00c02b..261d9bd 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/service/Impl/WorkoutWeekServiceImpl.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/service/Impl/WorkoutWeekServiceImpl.java @@ -9,18 +9,11 @@ import com.mykhailotiutiun.repcounterbot.service.WorkoutDayService; import com.mykhailotiutiun.repcounterbot.service.WorkoutWeekService; import com.mykhailotiutiun.repcounterbot.util.LocalDateWeekUtil; -import com.mykhailotiutiun.repcounterbot.util.LocaleMessageUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -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.replykeyboard.InlineKeyboardMarkup; -import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton; import java.time.LocalDate; -import java.util.ArrayList; -import java.util.List; @Slf4j @Service @@ -29,14 +22,12 @@ public class WorkoutWeekServiceImpl implements WorkoutWeekService { private final WorkoutWeekRepository workoutWeekRepository; private final WorkoutDayService workoutDayService; private final LocalDateWeekUtil localDateWeekUtil; - private final LocaleMessageUtil localeMessageUtil; - public WorkoutWeekServiceImpl(WorkoutWeekRepository workoutWeekRepository, WorkoutDayService workoutDayService, LocalDateWeekUtil localDateWeekUtil, LocaleMessageUtil localeMessageUtil) { + public WorkoutWeekServiceImpl(WorkoutWeekRepository workoutWeekRepository, WorkoutDayService workoutDayService, LocalDateWeekUtil localDateWeekUtil) { this.workoutWeekRepository = workoutWeekRepository; this.workoutDayService = workoutDayService; this.localDateWeekUtil = localDateWeekUtil; - this.localeMessageUtil = localeMessageUtil; } @Override @@ -44,6 +35,7 @@ public WorkoutWeek getCurrentWorkoutWeekByUserId(Long userId) { log.trace("Get current WorkoutWeek by user-id {}", userId); WorkoutWeek workoutWeek = workoutWeekRepository.findByUserIdAndCurrent(userId, true).orElseThrow(EntityNotFoundException::new); + if (!localDateWeekUtil.isCurrentWeek(workoutWeek.getWeekStartDate(), workoutWeek.getWeekEndDate())) { createFromOldWorkoutWeek(workoutWeek); workoutWeek = workoutWeekRepository.findByUserIdAndCurrent(userId, true).orElseThrow(EntityNotFoundException::new); @@ -69,6 +61,7 @@ private void createFromOldWorkoutWeek(WorkoutWeek oldWorkoutWeek) { log.trace("Create from old WorkoutWeek: {}", oldWorkoutWeek); WorkoutWeek workoutWeek = WorkoutWeek.builder() .user(oldWorkoutWeek.getUser()) + .current(true) .weekStartDate(localDateWeekUtil.getFirstDateOfWeekFromDate(LocalDate.now())) .weekEndDate(localDateWeekUtil.getLastDateOfWeekFromDate(LocalDate.now())) .build(); diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/service/WorkoutDayService.java b/src/main/java/com/mykhailotiutiun/repcounterbot/service/WorkoutDayService.java index 0f6b26c..4aa1ece 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/service/WorkoutDayService.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/service/WorkoutDayService.java @@ -2,18 +2,16 @@ import com.mykhailotiutiun.repcounterbot.model.WorkoutDay; import com.mykhailotiutiun.repcounterbot.model.WorkoutWeek; -import org.telegram.telegrambots.meta.api.methods.send.SendMessage; -import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageText; import java.util.List; public interface WorkoutDayService { - WorkoutDay getById(String id); + WorkoutDay getById(Long id); List getAllByWorkoutWeek(WorkoutWeek workoutWeek); void createAllFromOldWorkoutWeek(WorkoutWeek oldWorkoutWeek, WorkoutWeek newWorkoutWeek); void save(WorkoutDay workoutDay); - void setName(String workoutDayId, String name); - void setRestWorkoutDay(String workoutDayId); + void setName(Long workoutDayId, String name); + void setRestWorkoutDay(Long workoutDayId); } diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/service/WorkoutExerciseService.java b/src/main/java/com/mykhailotiutiun/repcounterbot/service/WorkoutExerciseService.java index a7be01e..c1bfd3e 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/service/WorkoutExerciseService.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/service/WorkoutExerciseService.java @@ -3,13 +3,11 @@ import com.mykhailotiutiun.repcounterbot.model.WorkoutDay; import com.mykhailotiutiun.repcounterbot.model.WorkoutExercise; import com.mykhailotiutiun.repcounterbot.model.WorkoutSet; -import org.telegram.telegrambots.meta.api.methods.send.SendMessage; -import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageText; import java.util.List; public interface WorkoutExerciseService { - WorkoutExercise getById(String id); + WorkoutExercise getById(Long id); Byte getLatestNumberByWorkoutDay(WorkoutDay workoutDay); WorkoutExercise getByNumberAndWorkoutDay(Byte number, WorkoutDay workoutDay); List getAllByWorkoutDay(WorkoutDay workoutDay); @@ -17,10 +15,10 @@ public interface WorkoutExerciseService { void create(WorkoutExercise workoutExercise); void createAllFromOldWorkoutDay(WorkoutDay oldWorkoutDay, WorkoutDay newWorkoutDay); - void setName(String workoutExerciseId, String name); - void moveUp(String workoutExerciseId); - void moveDown(String workoutExerciseId); - void addSets(String workoutExerciseId, List workoutSets); - void deleteById(String id); + void setName(Long workoutExerciseId, String name); + void moveUp(Long workoutExerciseId); + void moveDown(Long workoutExerciseId); + void addSets(Long workoutExerciseId, List workoutSets); + void deleteById(Long id); } diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/service/WorkoutWeekService.java b/src/main/java/com/mykhailotiutiun/repcounterbot/service/WorkoutWeekService.java index 69e6569..6a4194b 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/service/WorkoutWeekService.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/service/WorkoutWeekService.java @@ -2,8 +2,6 @@ import com.mykhailotiutiun.repcounterbot.exception.EntityAlreadyExistsException; import com.mykhailotiutiun.repcounterbot.model.WorkoutWeek; -import org.telegram.telegrambots.meta.api.methods.send.SendMessage; -import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageText; public interface WorkoutWeekService { diff --git a/src/main/java/com/mykhailotiutiun/repcounterbot/util/impl/LocaleMessageUtilImpl.java b/src/main/java/com/mykhailotiutiun/repcounterbot/util/impl/LocaleMessageUtilImpl.java index 1171156..fcfd221 100644 --- a/src/main/java/com/mykhailotiutiun/repcounterbot/util/impl/LocaleMessageUtilImpl.java +++ b/src/main/java/com/mykhailotiutiun/repcounterbot/util/impl/LocaleMessageUtilImpl.java @@ -1,7 +1,7 @@ package com.mykhailotiutiun.repcounterbot.util.impl; import com.mykhailotiutiun.repcounterbot.cache.SelectedLanguageCache; -import com.mykhailotiutiun.repcounterbot.service.UserService; +import com.mykhailotiutiun.repcounterbot.language.SelectedLanguageProvider; import com.mykhailotiutiun.repcounterbot.util.LocaleMessageUtil; import org.springframework.context.MessageSource; import org.springframework.stereotype.Component; @@ -13,12 +13,12 @@ public class LocaleMessageUtilImpl implements LocaleMessageUtil { private final MessageSource messageSource; private final SelectedLanguageCache selectedLanguageCache; - private final UserService userService; + private final SelectedLanguageProvider selectedLanguageProvider; - public LocaleMessageUtilImpl(MessageSource messageSource, SelectedLanguageCache selectedLanguageCache, UserService userService) { + public LocaleMessageUtilImpl(MessageSource messageSource, SelectedLanguageCache selectedLanguageCache, SelectedLanguageProvider selectedLanguageProvider) { this.messageSource = messageSource; this.selectedLanguageCache = selectedLanguageCache; - this.userService = userService; + this.selectedLanguageProvider = selectedLanguageProvider; } @Override @@ -30,7 +30,7 @@ public String getMessage(String messageCode, String chatId) { public String getLocalTag(String chatId) { String localeTag = selectedLanguageCache.getSelectedLanguage(chatId); if (localeTag == null) { - localeTag = userService.getById(Long.valueOf(chatId)).getLocalTag(); + localeTag = selectedLanguageProvider.getLocaleTag(chatId); selectedLanguageCache.setSelectedLanguage(chatId, localeTag); } return localeTag; diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index f8d615a..2fcca77 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,6 +1,11 @@ server.port=8080 +spring.datasource.driver-class-name=org.postgresql.Driver +spring.datasource.url=jdbc:postgresql://${DATASOURCE_URL}/repcounterbot +spring.datasource.username=${DATASOURCE_USER} +spring.datasource.password=${DATASOURCE_PASSWORD} +spring.sql.init.mode=always logging.level.com.mykhailotiutiun.repcounterbot=trace logging.level.root=info telegram.webhook-path=https://repcounterbot.serveo.net telegram.bot-name=@RepCounterBot -telegram.bot-token=6030179095:AAE1j3SaiUiZYh4tkY9lj4KfAppPTJLcyvU +telegram.bot-token=${BOT_TOKEN} diff --git a/src/main/resources/messages_en_US.properties b/src/main/resources/messages_en_US.properties index 57b8c02..428cf8b 100644 --- a/src/main/resources/messages_en_US.properties +++ b/src/main/resources/messages_en_US.properties @@ -14,12 +14,12 @@ reply.workout-day.keyboard.set-as-rest-day=Make a day of rest reply.workout-exercise.enter-the-name=Enter the name of the exercise reply.workout-exercise.keyboard.set-sets-request=Specify sets reply.workout-exercise.keyboard.edit=Edit -reply.workout-set.set=Use the pattern: \nWeight : number of reps, weight : number of reps, etc. +reply.workout-set.set=Use the pattern: \nweight:number of reps,weight:number of reps, etc. reply.main-menu.greeting=Hello -reply.main-menu.keyboard.recent-week=Current workout week +reply.main-menu.keyboard.recent-week=Current week reply.main-menu.keyboard.change-lang=Choose a language reply.keyboard.back=<< Back -print.workout-week=Current training week with %s, by %s +print.workout-week=Current week from %s, to %s print.workout-day.is-rest-day=Rest day print.workout-day.type-not-set=Not specified print.workout-exercise.for-workout-exercise-reply=Previous workout: diff --git a/src/main/resources/messages_uk_UA.properties b/src/main/resources/messages_uk_UA.properties index d32f02d..6e631ab 100644 --- a/src/main/resources/messages_uk_UA.properties +++ b/src/main/resources/messages_uk_UA.properties @@ -12,14 +12,14 @@ reply.workout-day.keyboard.add-exercise=Додати вправу reply.workout-day.keyboard.create-workout-request=Створити тренування reply.workout-day.keyboard.set-as-rest-day=Зробити день відпочинку reply.workout-exercise.enter-the-name=Введіть назву вправи -reply.workout-exercise.keyboard.set-sets-request=Вкажіть підходи +reply.workout-exercise.keyboard.set-sets-request=Вказать підходи reply.workout-exercise.keyboard.edit=Редагувати -reply.workout-set.set=Використовуйте шаблон: \nВага: кількість повторень, вага: кількість повторень, i т.д. +reply.workout-set.set=Використовуйте шаблон: \nвага:кількість повторень,вага:кількість повторень, i т.д. reply.main-menu.greeting=Привіт -reply.main-menu.keyboard.recent-week=Поточний тиждень тренувань +reply.main-menu.keyboard.recent-week=Поточний тиждень reply.main-menu.keyboard.change-lang=Обрати мову reply.keyboard.back=<< Назад -print.workout-week=Поточний тиждень тренувань з %s, по %s +print.workout-week=Поточний тиждень з %s, по %s print.workout-day.is-rest-day=День відпочинку print.workout-day.type-not-set=Не вказано print.workout-exercise.for-workout-exercise-reply=На попередньому тренуванні: diff --git a/src/main/resources/schema.sql b/src/main/resources/schema.sql new file mode 100644 index 0000000..56e4c1a --- /dev/null +++ b/src/main/resources/schema.sql @@ -0,0 +1,63 @@ +create table if not exists bot_users +( + id bigint not null + constraint bot_users_pk + primary key, + username varchar(255) not null, + local_tag varchar(10) +); + +create table if not exists workout_weeks +( + id bigserial + constraint workout_weeks_pk + primary key, + current boolean, + week_start_date date not null, + week_end_date date not null, + user_id bigint not null + constraint user_id + references bot_users +); + +create table if not exists workout_days +( + id bigserial + constraint workout_day_pk + primary key, + name varchar(255), + date date not null, + is_workout_day boolean, + workout_week_id bigint not null + constraint workout_week_id + references workout_weeks +); + +create table if not exists workout_exercises +( + id bigserial + primary key, + number smallint, + workout_day_id bigint not null + constraint fk_workout_day + references workout_days, + name varchar(255) +); + +create table if not exists workout_sets +( + id bigserial + constraint workout_sets_pk + primary key, + number integer, + reps integer, + weight integer, + workout_ex_id bigint + constraint fk_workout_exercise + references workout_exercises + on delete cascade, + prev_workout_ex_id bigint + constraint fk_prev_workout_exercise + references workout_exercises + on delete cascade +); diff --git a/src/test/java/com/mykhailotiutiun/repcounterbot/services/WorkoutDayServiceImplTest.java b/src/test/java/com/mykhailotiutiun/repcounterbot/services/WorkoutDayServiceImplTest.java index 0f6eee3..a336718 100644 --- a/src/test/java/com/mykhailotiutiun/repcounterbot/services/WorkoutDayServiceImplTest.java +++ b/src/test/java/com/mykhailotiutiun/repcounterbot/services/WorkoutDayServiceImplTest.java @@ -17,7 +17,7 @@ import java.util.List; import java.util.Optional; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.Assert.*; import static org.mockito.Mockito.*; @RunWith(MockitoJUnitRunner.class) @@ -32,31 +32,36 @@ public class WorkoutDayServiceImplTest { @Test public void testGetWorkoutDayById(){ - WorkoutDay workoutDay = WorkoutDay.builder().id("1").build(); - when(workoutDayRepository.findById("1")).thenReturn(Optional.of(workoutDay)); - assertEquals(workoutDay, workoutDayService.getById("1")); + long id = 1L; + WorkoutDay workoutDay = WorkoutDay.builder().id(id).build(); + when(workoutDayRepository.findById(id)).thenReturn(Optional.of(workoutDay)); + assertEquals(workoutDay, workoutDayService.getById(id)); - when(workoutDayRepository.findById("1")).thenReturn(Optional.empty()); + when(workoutDayRepository.findById(id)).thenReturn(Optional.empty()); assertThrows(EntityNotFoundException.class, () -> { - workoutDayService.getById("1"); + workoutDayService.getById(id); }); } @Test public void testGetAllByWorkoutWeek(){ - WorkoutWeek workoutWeek = WorkoutWeek.builder().id("1").build(); + WorkoutWeek workoutWeek = WorkoutWeek.builder().id(1L).build(); - when(workoutDayRepository.findAllByWorkoutWeek(workoutWeek)).thenReturn(List.of(new WorkoutDay())); - assertNotNull(workoutDayService.getAllByWorkoutWeek(workoutWeek).get(0)); + List workoutDays = new ArrayList<>(); + for (int i = 7; i > 0; i--){ + workoutDays.add(WorkoutDay.builder().id((long) (i)).date(LocalDate.now().plusDays(i)).build()); + } + when(workoutDayRepository.findAllByWorkoutWeek(workoutWeek)).thenReturn(workoutDays); + assertEquals(workoutDays.get(6), workoutDayService.getAllByWorkoutWeek(workoutWeek).get(0)); } @Test public void testCreateAllFromOldWorkoutWeek(){ - WorkoutWeek workoutWeek = WorkoutWeek.builder().id("1").build(); - WorkoutWeek newWorkoutWeek = WorkoutWeek.builder().id("2").weekStartDate(LocalDate.now()).build(); + WorkoutWeek workoutWeek = WorkoutWeek.builder().id(1L).build(); + WorkoutWeek newWorkoutWeek = WorkoutWeek.builder().id(2L).weekStartDate(LocalDate.now()).build(); List oldWorkoutDays = new ArrayList<>(); for (int i = 0; i < 7; i++){ - oldWorkoutDays.add(WorkoutDay.builder().id(String.valueOf(i + 1)).build()); + oldWorkoutDays.add(WorkoutDay.builder().id((long) (i + 1)).date(LocalDate.now().plusDays(i)).build()); } when(workoutDayRepository.findAllByWorkoutWeek(workoutWeek)).thenReturn(oldWorkoutDays); workoutDayService.createAllFromOldWorkoutWeek(workoutWeek, newWorkoutWeek); @@ -66,26 +71,29 @@ public void testCreateAllFromOldWorkoutWeek(){ @Test public void testSave(){ - WorkoutDay workoutDay = WorkoutDay.builder().id("1").build(); + WorkoutDay workoutDay = WorkoutDay.builder().id(1L).build(); workoutDayService.save(workoutDay); verify(workoutDayRepository).save(workoutDay); } @Test public void setWorkoutDayName(){ - WorkoutDay workoutDay = WorkoutDay.builder().id("1").build(); - when(workoutDayRepository.findById("1")).thenReturn(Optional.of(workoutDay)); - workoutDayService.setName("1", "Name"); + long id = 1L; + String name = "Name"; + WorkoutDay workoutDay = WorkoutDay.builder().id(id).build(); + when(workoutDayRepository.findById(id)).thenReturn(Optional.of(workoutDay)); + workoutDayService.setName(id, name); verify(workoutDayRepository).save(workoutDay); - assertEquals(workoutDay.getName(), "Name"); + assertEquals(workoutDay.getName(), name); assertTrue(workoutDay.getIsWorkoutDay()); } @Test public void setRestWorkoutDay(){ - WorkoutDay workoutDay = WorkoutDay.builder().id("1").build(); - when(workoutDayRepository.findById("1")).thenReturn(Optional.of(workoutDay)); - workoutDayService.setRestWorkoutDay("1"); + long id = 1L; + WorkoutDay workoutDay = WorkoutDay.builder().id(id).build(); + when(workoutDayRepository.findById(id)).thenReturn(Optional.of(workoutDay)); + workoutDayService.setRestWorkoutDay(id); verify(workoutDayRepository).save(workoutDay); assertFalse(workoutDay.getIsWorkoutDay()); } diff --git a/src/test/java/com/mykhailotiutiun/repcounterbot/services/WorkoutExerciseServiceImplTest.java b/src/test/java/com/mykhailotiutiun/repcounterbot/services/WorkoutExerciseServiceImplTest.java index 932a034..9116bbb 100644 --- a/src/test/java/com/mykhailotiutiun/repcounterbot/services/WorkoutExerciseServiceImplTest.java +++ b/src/test/java/com/mykhailotiutiun/repcounterbot/services/WorkoutExerciseServiceImplTest.java @@ -35,24 +35,25 @@ public class WorkoutExerciseServiceImplTest { @Test public void getById(){ - WorkoutExercise workoutExercise = WorkoutExercise.builder().id("1").build(); + Long id = 1L; + WorkoutExercise workoutExercise = WorkoutExercise.builder().id(id).build(); workoutExercise.setWorkoutSets(new ArrayList<>(List.of(WorkoutSet.builder().number(2).build(), WorkoutSet.builder().number(1).build()))); - when(workoutExerciseRepository.findById("1")).thenReturn(Optional.of(workoutExercise)); - assertEquals(workoutExercise, workoutExerciseService.getById("1")); + when(workoutExerciseRepository.findById(id)).thenReturn(Optional.of(workoutExercise)); + assertEquals(workoutExercise, workoutExerciseService.getById(id)); assertEquals(1, (int) workoutExercise.getWorkoutSets().get(0).getNumber()); assertEquals(2, (int) workoutExercise.getWorkoutSets().get(1).getNumber()); - when(workoutExerciseRepository.findById("1")).thenReturn(Optional.empty()); + when(workoutExerciseRepository.findById(id)).thenReturn(Optional.empty()); assertThrows(EntityNotFoundException.class, () -> { - workoutExerciseService.getById("1"); + workoutExerciseService.getById(id); }); } @Test public void getLatestNumberByWorkoutDay(){ - WorkoutExercise workoutExercise = WorkoutExercise.builder().id("1").number((byte) 1).build(); - WorkoutDay workoutDay = WorkoutDay.builder().id("2").build(); + WorkoutExercise workoutExercise = WorkoutExercise.builder().id(1L).number((byte) 1).build(); + WorkoutDay workoutDay = WorkoutDay.builder().id(2L).build(); when(workoutExerciseRepository.findAllByWorkoutDayOrderByNumberDesc(workoutDay)).thenReturn(List.of(workoutExercise)); assertEquals(workoutExercise.getNumber(), workoutExerciseService.getLatestNumberByWorkoutDay(workoutDay)); @@ -62,8 +63,8 @@ public void getLatestNumberByWorkoutDay(){ @Test public void getByNumberAndWorkoutDay(){ - WorkoutExercise workoutExercise = WorkoutExercise.builder().id("1").number((byte) 1).build(); - WorkoutDay workoutDay = WorkoutDay.builder().id("2").build(); + WorkoutExercise workoutExercise = WorkoutExercise.builder().id(1L).number((byte) 1).build(); + WorkoutDay workoutDay = WorkoutDay.builder().id(2L).build(); when(workoutExerciseRepository.findByNumberAndWorkoutDay(workoutExercise.getNumber(), workoutDay)).thenReturn(Optional.of(workoutExercise)); assertEquals(workoutExercise, workoutExerciseService.getByNumberAndWorkoutDay((byte) 1, workoutDay)); @@ -75,16 +76,16 @@ public void getByNumberAndWorkoutDay(){ @Test public void getAllByWorkoutDay(){ - WorkoutExercise workoutExercise = WorkoutExercise.builder().id("1").number((byte) 1).build(); - WorkoutDay workoutDay = WorkoutDay.builder().id("2").build(); - when(workoutExerciseRepository.findAllByWorkoutDay(workoutDay)).thenReturn(List.of(workoutExercise)); + WorkoutExercise workoutExercise = WorkoutExercise.builder().id(1L).number((byte) 1).build(); + WorkoutDay workoutDay = WorkoutDay.builder().id(2L).build(); + when(workoutExerciseRepository.findAllByWorkoutDayOrderByNumberDesc(workoutDay)).thenReturn(List.of(workoutExercise)); assertEquals(workoutExercise, workoutExerciseService.getAllByWorkoutDay(workoutDay).get(0)); } @Test public void create(){ - WorkoutExercise workoutExercise = WorkoutExercise.builder().id("1").build(); - WorkoutDay workoutDay = WorkoutDay.builder().id("2").build(); + WorkoutExercise workoutExercise = WorkoutExercise.builder().id(1L).build(); + WorkoutDay workoutDay = WorkoutDay.builder().id(2L).build(); workoutExercise.setWorkoutDay(workoutDay); when(workoutExerciseRepository.findAllByWorkoutDayOrderByNumberDesc(workoutDay)).thenReturn(List.of(WorkoutExercise.builder().number((byte) 1).build())); workoutExerciseService.create(workoutExercise); @@ -94,33 +95,36 @@ public void create(){ @Test public void createAllFromOldWorkoutDay(){ - WorkoutDay workoutDay = WorkoutDay.builder().id("1").build(); - WorkoutDay newWorkoutDay = WorkoutDay.builder().id("2").build(); + WorkoutDay workoutDay = WorkoutDay.builder().id(1L).build(); + WorkoutDay newWorkoutDay = WorkoutDay.builder().id(2L).build(); List oldWorkoutExercises = new ArrayList<>(); for (int i = 0; i < 7; i++){ - oldWorkoutExercises.add(WorkoutExercise.builder().id(String.valueOf(i + 1)).build()); + oldWorkoutExercises.add(WorkoutExercise.builder().id((long) (i + 1)).build()); } - when(workoutExerciseRepository.findAllByWorkoutDay(workoutDay)).thenReturn(oldWorkoutExercises); + when(workoutExerciseRepository.findAllByWorkoutDayOrderByNumberDesc(workoutDay)).thenReturn(oldWorkoutExercises); workoutExerciseService.createAllFromOldWorkoutDay(workoutDay, newWorkoutDay); verify(workoutExerciseRepository, times(7)).save(any()); } @Test public void setName(){ - WorkoutExercise workoutExercise = WorkoutExercise.builder().id("1").workoutSets(new ArrayList<>()).build(); - when(workoutExerciseRepository.findById("1")).thenReturn(Optional.of(workoutExercise)); - workoutExerciseService.setName("1", "Name"); + Long id = 1L; + String name = "Name"; + WorkoutExercise workoutExercise = WorkoutExercise.builder().id(id).workoutSets(new ArrayList<>()).build(); + when(workoutExerciseRepository.findById(id)).thenReturn(Optional.of(workoutExercise)); + workoutExerciseService.setName(id, name); verify(workoutExerciseRepository).save(workoutExercise); - Assertions.assertEquals(workoutExercise.getName(), "Name"); + Assertions.assertEquals(workoutExercise.getName(), name); } @Test public void moveUp(){ - WorkoutExercise workoutExercise = WorkoutExercise.builder().id("1").number((byte) 2).workoutSets(new ArrayList<>()).build(); - WorkoutExercise upperWorkoutExercise = WorkoutExercise.builder().id("1").number((byte) 1).workoutSets(new ArrayList<>()).build(); + Long id = 1L; + WorkoutExercise workoutExercise = WorkoutExercise.builder().id(id).number((byte) 2).workoutSets(new ArrayList<>()).build(); + WorkoutExercise upperWorkoutExercise = WorkoutExercise.builder().id(id).number((byte) 1).workoutSets(new ArrayList<>()).build(); when(workoutExerciseRepository.findById(workoutExercise.getId())).thenReturn(Optional.of(workoutExercise)); when(workoutExerciseRepository.findByNumberAndWorkoutDay(eq((byte) 1), any())).thenReturn(Optional.of(upperWorkoutExercise)); - workoutExerciseService.moveUp("1"); + workoutExerciseService.moveUp(id); verify(workoutExerciseRepository).save(workoutExercise); verify(workoutExerciseRepository).save(upperWorkoutExercise); assertEquals(Byte.valueOf((byte) 1), workoutExercise.getNumber()); @@ -128,22 +132,23 @@ public void moveUp(){ when(workoutExerciseRepository.findById(workoutExercise.getId())).thenReturn(Optional.empty()); assertThrows(EntityNotFoundException.class, () -> { - workoutExerciseService.moveUp("1"); + workoutExerciseService.moveUp(id); }); when(workoutExerciseRepository.findById(workoutExercise.getId())).thenReturn(Optional.of(workoutExercise)); assertThrows(EntityNotFoundException.class, () -> { - workoutExerciseService.moveUp("1"); + workoutExerciseService.moveUp(id); }); } @Test public void moveDown(){ - WorkoutExercise workoutExercise = WorkoutExercise.builder().id("1").number((byte) 1).workoutSets(new ArrayList<>()).build(); - WorkoutExercise lowerWorkoutExercise = WorkoutExercise.builder().id("1").number((byte) 2).workoutSets(new ArrayList<>()).build(); + Long s = 1L; + WorkoutExercise workoutExercise = WorkoutExercise.builder().id(s).number((byte) 1).workoutSets(new ArrayList<>()).build(); + WorkoutExercise lowerWorkoutExercise = WorkoutExercise.builder().id(s).number((byte) 2).workoutSets(new ArrayList<>()).build(); when(workoutExerciseRepository.findById(workoutExercise.getId())).thenReturn(Optional.of(workoutExercise)); when(workoutExerciseRepository.findByNumberAndWorkoutDay(eq((byte) 2), any())).thenReturn(Optional.of(lowerWorkoutExercise)); - workoutExerciseService.moveDown("1"); + workoutExerciseService.moveDown(s); verify(workoutExerciseRepository).save(workoutExercise); verify(workoutExerciseRepository).save(lowerWorkoutExercise); assertEquals(Byte.valueOf((byte) 2), workoutExercise.getNumber()); @@ -151,21 +156,22 @@ public void moveDown(){ when(workoutExerciseRepository.findById(workoutExercise.getId())).thenReturn(Optional.empty()); assertThrows(EntityNotFoundException.class, () -> { - workoutExerciseService.moveDown("1"); + workoutExerciseService.moveDown(s); }); when(workoutExerciseRepository.findById(workoutExercise.getId())).thenReturn(Optional.of(workoutExercise)); assertThrows(EntityNotFoundException.class, () -> { - workoutExerciseService.moveDown("1"); + workoutExerciseService.moveDown(s); }); } @Test public void addSets(){ - WorkoutExercise workoutExercise = WorkoutExercise.builder().id("1").workoutSets(new ArrayList<>()).build(); - List workoutSets = List.of(WorkoutSet.builder().id("1").build(), WorkoutSet.builder().id("2").build()); - when(workoutExerciseRepository.findById("1")).thenReturn(Optional.of(workoutExercise)); - workoutExerciseService.addSets("1", workoutSets); + Long id = 1L; + WorkoutExercise workoutExercise = WorkoutExercise.builder().id(id).workoutSets(new ArrayList<>()).build(); + List workoutSets = List.of(WorkoutSet.builder().id(id).build(), WorkoutSet.builder().id(2L).build()); + when(workoutExerciseRepository.findById(id)).thenReturn(Optional.of(workoutExercise)); + workoutExerciseService.addSets(id, workoutSets); verify(workoutSetService, times(2)).save(any()); verify(workoutExerciseRepository).save(workoutExercise); assertEquals(workoutSets, workoutExercise.getWorkoutSets()); @@ -173,7 +179,8 @@ public void addSets(){ @Test public void deleteById(){ - workoutExerciseService.deleteById("1"); - verify(workoutExerciseRepository).deleteById("1"); + long id = 1L; + workoutExerciseService.deleteById(id); + verify(workoutExerciseRepository).deleteById(id); } } diff --git a/src/test/java/com/mykhailotiutiun/repcounterbot/services/WorkoutSetsServiceImplTest.java b/src/test/java/com/mykhailotiutiun/repcounterbot/services/WorkoutSetsServiceImplTest.java index 91a834b..c31cc03 100644 --- a/src/test/java/com/mykhailotiutiun/repcounterbot/services/WorkoutSetsServiceImplTest.java +++ b/src/test/java/com/mykhailotiutiun/repcounterbot/services/WorkoutSetsServiceImplTest.java @@ -21,7 +21,7 @@ public class WorkoutSetsServiceImplTest { @Test public void save(){ - WorkoutSet workoutSet = WorkoutSet.builder().id("1").build(); + WorkoutSet workoutSet = WorkoutSet.builder().id(1L).build(); workoutSetService.save(workoutSet); verify(workoutSetRepository).save(workoutSet); } diff --git a/src/test/java/com/mykhailotiutiun/repcounterbot/services/WorkoutWeekServiceImplTest.java b/src/test/java/com/mykhailotiutiun/repcounterbot/services/WorkoutWeekServiceImplTest.java index f22f602..e77523b 100644 --- a/src/test/java/com/mykhailotiutiun/repcounterbot/services/WorkoutWeekServiceImplTest.java +++ b/src/test/java/com/mykhailotiutiun/repcounterbot/services/WorkoutWeekServiceImplTest.java @@ -20,8 +20,7 @@ import java.time.LocalDate; import java.util.Optional; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.Assert.*; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; @@ -37,9 +36,6 @@ public class WorkoutWeekServiceImplTest { @Mock private LocalDateWeekUtil localDateWeekUtil; - @Mock - private LocaleMessageUtil localeMessageUtil; - @InjectMocks private WorkoutWeekServiceImpl workoutWeekService; @@ -53,7 +49,7 @@ void setUp() { public void testGetCurrentWorkoutWeekByUserId() { Long userId = 1L; - WorkoutWeek expectedWorkoutWeek = WorkoutWeek.builder().id("2").user(User.builder().id(userId).build()).weekEndDate(LocalDate.now()).build(); + WorkoutWeek expectedWorkoutWeek = WorkoutWeek.builder().id(2L).user(User.builder().id(userId).build()).weekEndDate(LocalDate.now()).build(); when(workoutWeekRepository.findByUserIdAndCurrent(userId, true)).thenReturn(Optional.of(expectedWorkoutWeek)); when(localDateWeekUtil.isCurrentWeek(any(), any())).thenReturn(true); WorkoutWeek actualWorkoutWeek = workoutWeekService.getCurrentWorkoutWeekByUserId(userId); @@ -73,7 +69,7 @@ public void testGetCurrentWorkoutWeekByUserId() { @Test public void testCreateWorkoutWeek() { - WorkoutWeek newWorkoutWeek = WorkoutWeek.builder().id("1").weekStartDate(LocalDate.now()).build(); + WorkoutWeek newWorkoutWeek = WorkoutWeek.builder().id(2L).weekStartDate(LocalDate.now()).build(); workoutWeekService.create(newWorkoutWeek); diff --git a/src/test/java/com/mykhailotiutiun/repcounterbot/util/LocaleMessageUtilImplTest.java b/src/test/java/com/mykhailotiutiun/repcounterbot/util/LocaleMessageUtilImplTest.java index 60bb509..c2bfcda 100644 --- a/src/test/java/com/mykhailotiutiun/repcounterbot/util/LocaleMessageUtilImplTest.java +++ b/src/test/java/com/mykhailotiutiun/repcounterbot/util/LocaleMessageUtilImplTest.java @@ -1,8 +1,7 @@ package com.mykhailotiutiun.repcounterbot.util; import com.mykhailotiutiun.repcounterbot.cache.SelectedLanguageCache; -import com.mykhailotiutiun.repcounterbot.model.User; -import com.mykhailotiutiun.repcounterbot.service.UserService; +import com.mykhailotiutiun.repcounterbot.language.SelectedLanguageProvider; import com.mykhailotiutiun.repcounterbot.util.impl.LocaleMessageUtilImpl; import org.junit.Test; import org.junit.runner.RunWith; @@ -25,7 +24,7 @@ public class LocaleMessageUtilImplTest { @Mock private MessageSource messageSource; @Mock - private UserService userService; + private SelectedLanguageProvider selectedLanguageProvider; @InjectMocks private LocaleMessageUtilImpl localeMessageUtil; @@ -42,7 +41,7 @@ public void getLocalTag(){ assertEquals("en-EN", localeMessageUtil.getLocalTag("1")); when(selectedLanguageCache.getSelectedLanguage("1")).thenReturn(null); - when(userService.getById(1L)).thenReturn(User.builder().localTag("en-EN").build()); + when(selectedLanguageProvider.getLocaleTag("1")).thenReturn("en-EN"); assertEquals("en-EN", localeMessageUtil.getLocalTag("1")); verify(selectedLanguageCache).setSelectedLanguage("1", "en-EN"); }