-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* JRTB-1: added repository layer. * STEP_6 JRTB-5: added ability to subscribe on group JRTB-6: added ability to see the list of the group subscription. * STEP_6
- Loading branch information
Showing
38 changed files
with
1,060 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,23 +19,24 @@ jobs: | |
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up MySQL | ||
uses: mirromutth/[email protected] | ||
with: | ||
mysql version: '8.0.15' | ||
mysql database: 'dev_jrtb_db' | ||
mysql root password: 'root' | ||
mysql user: 'dev_jrtb_db_user' | ||
mysql password: 'dev_jrtb_db_password' | ||
- name: Set up JDK 17 | ||
uses: actions/setup-java@v3 | ||
with: | ||
java-version: '17' | ||
distribution: 'temurin' | ||
cache: maven | ||
- name: Build with Maven | ||
run: mvn -B package --file pom.xml | ||
- uses: actions/checkout@v3 | ||
- name: Set up MySQL | ||
uses: mirromutth/[email protected] | ||
with: | ||
mysql version: '8.0.15' | ||
mysql database: 'dev_jrtb_db' | ||
mysql root password: 'root' | ||
mysql user: 'dev_jrtb_db_user' | ||
mysql password: 'dev_jrtb_db_password' | ||
- name: Set up JDK 17 | ||
uses: actions/setup-java@v3 | ||
with: | ||
java-version: '17' | ||
distribution: 'temurin' | ||
cache: maven | ||
- name: Build with Maven | ||
run: mvn -B package --file pom.xml | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/AddGroupSubCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.github.JBolivarLi.javarushtelegrambot.bot.command; | ||
|
||
import com.github.JBolivarLi.javarushtelegrambot.bot.command.Command; | ||
import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.JavaRushGroupClient; | ||
import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto.GroupDiscussionInfo; | ||
import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto.GroupRequestArgs; | ||
import com.github.JBolivarLi.javarushtelegrambot.bot.repository.entity.GroupSub; | ||
import com.github.JBolivarLi.javarushtelegrambot.bot.service.GroupSubService; | ||
import com.github.JBolivarLi.javarushtelegrambot.bot.service.SendBotMessageService; | ||
import org.telegram.telegrambots.meta.api.objects.Update; | ||
|
||
import java.util.stream.Collectors; | ||
|
||
import static com.github.JBolivarLi.javarushtelegrambot.bot.command.CommandName.ADD_GROUP_SUB; | ||
import static com.github.JBolivarLi.javarushtelegrambot.bot.command.CommandUtils.getChatId; | ||
import static com.github.JBolivarLi.javarushtelegrambot.bot.command.CommandUtils.getMessage; | ||
import static java.util.Objects.isNull; | ||
import static org.apache.commons.lang3.StringUtils.SPACE; | ||
|
||
|
||
/** | ||
* Add Group subscription {@link Command}. | ||
*/ | ||
//todo add unit test for the command logic. | ||
public class AddGroupSubCommand implements Command { | ||
|
||
private final SendBotMessageService sendBotMessageService; | ||
private final JavaRushGroupClient javaRushGroupClient; | ||
private final GroupSubService groupSubService; | ||
|
||
public AddGroupSubCommand(SendBotMessageService sendBotMessageService, JavaRushGroupClient javaRushGroupClient, | ||
GroupSubService groupSubService) { | ||
this.sendBotMessageService = sendBotMessageService; | ||
this.javaRushGroupClient = javaRushGroupClient; | ||
this.groupSubService = groupSubService; | ||
} | ||
private boolean isNumeric(String str) { | ||
if (str == null || str.isEmpty()) { | ||
return false; | ||
} | ||
for (char c : str.toCharArray()) { | ||
if (!Character.isDigit(c)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
|
||
@Override | ||
public void execute(Update update) { | ||
if (getMessage(update).equalsIgnoreCase(ADD_GROUP_SUB.getCommandName())) { | ||
sendGroupIdList(getChatId(update)); | ||
return; | ||
} | ||
String groupId = getMessage(update).split(SPACE)[1]; | ||
String chatId = getChatId(update); | ||
if (isNumeric(groupId)) { | ||
GroupDiscussionInfo groupById = javaRushGroupClient.getGroupById(Integer.parseInt(groupId)); | ||
if (isNull(groupById.getId())) { | ||
sendGroupNotFound(chatId, groupId); | ||
} | ||
GroupSub savedGroupSub = groupSubService.save(chatId, groupById); | ||
sendBotMessageService.sendMessage(chatId, "Подписал на группу " + savedGroupSub.getTitle()); | ||
} else { | ||
sendNotValidGroupID(chatId, groupId); | ||
} | ||
} | ||
|
||
private void sendGroupNotFound(String chatId, String groupId) { | ||
String groupNotFoundMessage = "Нет группы с ID = \"%s\""; | ||
sendBotMessageService.sendMessage(chatId, String.format(groupNotFoundMessage, groupId)); | ||
} | ||
|
||
private void sendNotValidGroupID(String chatId, String groupId) { | ||
String groupNotFoundMessage = "Неправильный ID группы = \"%s\""; | ||
sendBotMessageService.sendMessage(chatId, String.format(groupNotFoundMessage, groupId)); | ||
; } | ||
|
||
private void sendGroupIdList(String chatId) { | ||
String groupIds = javaRushGroupClient.getGroupList(GroupRequestArgs.builder().build()).stream() | ||
.map(group -> String.format("%s - %s \n", group.getTitle(), group.getId())) | ||
.collect(Collectors.joining()); | ||
|
||
String message = "Чтобы подписаться на группу - передай комадну вместе с ID группы. \n" + | ||
"Например: /addGroupSub 16 \n\n" + | ||
"я подготовил список всех групп - выбирай какую хочешь :) \n\n" + | ||
"имя группы - ID группы \n\n" + | ||
"%s"; | ||
|
||
sendBotMessageService.sendMessage(chatId, String.format(message, groupIds)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/CommandUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.github.JBolivarLi.javarushtelegrambot.bot.command; | ||
|
||
|
||
import org.telegram.telegrambots.meta.api.objects.Update; | ||
|
||
/** | ||
* Utils class for Commands. | ||
*/ | ||
public class CommandUtils { | ||
|
||
/** | ||
* Retrieve chatId from {@link Update} object. | ||
* | ||
* @param update provided {@link Update} | ||
* @return chatID from the provided {@link Update} object. | ||
*/ | ||
public static String getChatId(Update update) { | ||
return update.getMessage().getChatId().toString(); | ||
} | ||
|
||
/** | ||
* Retrieve text of the message from {@link Update} object. | ||
* | ||
* @param update provided {@link Update} | ||
* @return the text of the message from the provided {@link Update} object. | ||
*/ | ||
public static String getMessage(Update update) { | ||
return update.getMessage().getText(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/java/com/github/JBolivarLi/javarushtelegrambot/bot/command/ListGroupSubCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.github.JBolivarLi.javarushtelegrambot.bot.command; | ||
|
||
import com.github.JBolivarLi.javarushtelegrambot.bot.repository.entity.TelegramUser; | ||
import com.github.JBolivarLi.javarushtelegrambot.bot.service.SendBotMessageService; | ||
import com.github.JBolivarLi.javarushtelegrambot.bot.service.TelegramUserService; | ||
import org.telegram.telegrambots.meta.api.objects.Update; | ||
import com.github.JBolivarLi.javarushtelegrambot.bot.repository.entity.GroupSub; | ||
import java.util.stream.Collectors; | ||
import javax.ws.rs.NotFoundException; | ||
|
||
import static com.github.JBolivarLi.javarushtelegrambot.bot.command.CommandUtils.getChatId; | ||
|
||
/** | ||
* {@link Command} for getting list of {@link GroupSub}. | ||
*/ | ||
public class ListGroupSubCommand implements Command { | ||
|
||
private final SendBotMessageService sendBotMessageService; | ||
private final TelegramUserService telegramUserService; | ||
|
||
public ListGroupSubCommand(SendBotMessageService sendBotMessageService, TelegramUserService telegramUserService) { | ||
this.sendBotMessageService = sendBotMessageService; | ||
this.telegramUserService = telegramUserService; | ||
} | ||
|
||
@Override | ||
public void execute(Update update) { | ||
//todo add exception handling | ||
TelegramUser telegramUser = telegramUserService.findByChatId(getChatId(update)) | ||
.orElseThrow(NotFoundException::new); | ||
|
||
String message = "Я нашел все подписки на группы: \n\n"; | ||
String collectedGroups = telegramUser.getGroupSubs().stream() | ||
.map(it -> "Группа: " + it.getTitle() + " , ID = " + it.getId() + " \n") | ||
.collect(Collectors.joining()); | ||
|
||
sendBotMessageService.sendMessage(telegramUser.getChatId(), message + collectedGroups); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...ava/com/github/JBolivarLi/javarushtelegrambot/bot/javarushclient/JavaRushGroupClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient; | ||
import com.github.JBolivarLi.javarushtelegrambot.bot.javarushclient.dto.*; | ||
|
||
|
||
import java.util.List; | ||
|
||
/** | ||
* Client for Javarush Open API corresponds to Groups. | ||
*/ | ||
public interface JavaRushGroupClient { | ||
|
||
/** | ||
* Get all the {@link GroupInfo} filtered by provided {@link GroupRequestArgs}. | ||
* | ||
* @param requestArgs provided {@link GroupRequestArgs}. | ||
* @return the collection of the {@link GroupInfo} objects. | ||
*/ | ||
List<GroupInfo> getGroupList(GroupRequestArgs requestArgs); | ||
|
||
/** | ||
* Get all the {@link GroupDiscussionInfo} filtered by provided {@link GroupRequestArgs}. | ||
* | ||
* @param requestArgs provided {@link GroupRequestArgs} | ||
* @return the collection of the {@link GroupDiscussionInfo} objects. | ||
*/ | ||
List<GroupDiscussionInfo> getGroupDiscussionList(GroupRequestArgs requestArgs); | ||
|
||
/** | ||
* Get count of groups filtered by provided {@link GroupRequestArgs}. | ||
* | ||
* @param countRequestArgs provided {@link GroupsCountRequestArgs}. | ||
* @return the count of the groups. | ||
*/ | ||
Integer getGroupCount(GroupsCountRequestArgs countRequestArgs); | ||
|
||
/** | ||
* Get {@link GroupDiscussionInfo} by provided ID. | ||
* | ||
* @param id provided ID. | ||
* @return {@link GroupDiscussionInfo} object. | ||
*/ | ||
GroupDiscussionInfo getGroupById(Integer id); | ||
} | ||
|
Oops, something went wrong.