Skip to content

Commit

Permalink
Merge branch 'main' into save-into-db
Browse files Browse the repository at this point in the history
  • Loading branch information
aqerd authored Nov 17, 2024
2 parents 54bd450 + 420b650 commit f074f74
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 28 deletions.
18 changes: 11 additions & 7 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,31 @@ name: Java Maven CI
on:
push:
branches:
- main
- '**'
paths:
- 'src/**'
pull_request:
branches:
- main
- '**'
paths:
- 'src/**'

jobs:
build:
build-and-test:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Setup JDK
uses: actions/setup-java@v2
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '17'

- name: Cache Maven packages
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
Expand All @@ -33,5 +37,5 @@ jobs:
- name: Build with Maven
run: mvn -B package --file pom.xml

- name: Run Tests
- name: Run clean tests
run: mvn clean test
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# Filmbot
[![Follow @aqerd on GitHub](https://img.shields.io/github/followers/aqerd?label=Ruslan%20Suleymanov&style=social-&labelColor=black&color=111111)](https://github.com/aqerd)
[![Follow @AnnaKhairillina on GitHub](https://img.shields.io/github/followers/AnnaKhairillina?label=Anna%20Khairullina&style=social-&labelColor=black&color=111111)](https://github.com/AnnaKhairillina)
[![Watch filmbot on GitHub](https://img.shields.io/github/watchers/aqerd/filmbot?label=Watch&style=social-&labelColor=black&color=111111)](https://github.com/aqerd/filmbot/subscription)
[![Star filmbot on GitHub](https://img.shields.io/github/stars/aqerd/filmbot?label=Star&style=social-&labelColor=black&color=111111)](https://github.com/aqerd/filmbot)
[![Open issues for filmbot](https://img.shields.io/github/issues/aqerd/filmbot?label=Issues&labelColor=black&color=111111)](https://github.com/aqerd/filmbot/issues)
#### This bot helps you find films you want to watch based on some filters like genre, year and etc

## 🚀 Usage
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/oopproject/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public void updateUserAge(long chatId, int userAge) {
e.printStackTrace();
}
}

public Integer getUserAge(long chatId) {
String selectUserAgeQuery = "SELECT userAge FROM users WHERE chatId = ?";
try (PreparedStatement selectStatement = connection.prepareStatement(selectUserAgeQuery)) {
Expand All @@ -62,7 +63,6 @@ public Integer getUserAge(long chatId) {

}


public void updateGenreIndexesJson(long chatId, String genreIndexesJson) {
String updateQuery = "UPDATE users SET genreIndexesJson = ? WHERE chatId = ?";
try (PreparedStatement updateStatement = connection.prepareStatement(updateQuery)) {
Expand Down Expand Up @@ -112,7 +112,8 @@ public String getYearIndexesJson(long chatId) {
}
return null;
}
public void updateSubscribe(long chatId, boolean subscribed) {

public void updateSubscribe(long chatId, boolean subscribed) {
String query = "UPDATE users SET subscribed = ? WHERE chatId = ?";
try (PreparedStatement updateStatement = connection.prepareStatement(query)) {
updateStatement.setBoolean(1, subscribed);
Expand All @@ -136,5 +137,4 @@ public List<Long> getSubscribedUsers() {
}
return users;
}
}

}
16 changes: 7 additions & 9 deletions src/main/java/org/oopproject/TelegramBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@
import static org.oopproject.utils.Config.tmdbService;
import static org.oopproject.utils.Validators.isCommand;
import static org.oopproject.utils.Replies.getReply;

import java.lang.reflect.Type;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardRow;
import org.telegram.telegrambots.meta.generics.TelegramClient;
import org.telegram.telegrambots.meta.api.objects.Update;
Expand All @@ -33,8 +31,9 @@
public class TelegramBot implements LongPollingSingleThreadUpdateConsumer {
private final TelegramClient telegramClient;
private final ExecutorService executorService = Executors.newFixedThreadPool(4);

private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
private final Database database=new Database();
private final Database database = new Database();
private final Gson gson = new Gson();

public int nOfFilms = 10;
Expand Down Expand Up @@ -99,6 +98,7 @@ public void handleUpdate(Update update) {
}
}
}

private void loadGenreIndexFromDatabase(long chatId) {
String jsonGenreString = database.getGenreIndexesJson(chatId);
if (jsonGenreString != null) {
Expand All @@ -115,8 +115,6 @@ private void loadYearIndexFromDatabase(long chatId) {
}
}



protected String handleCommands(String messageText, long chatId) {
String responseMessage;

Expand Down Expand Up @@ -289,11 +287,11 @@ protected String handleYear(String messageText, long chatId) {
protected void handleSubscribe(long chatId) {
database.updateSubscribe(chatId, true);
}

protected void handleUnsubscribe(long chatId) {
database.updateSubscribe(chatId, false);
}


protected String handleAge(String messageText, long chatId) {
if (isCommand(messageText)) {
commandWaiter.put(chatId, NONE);
Expand All @@ -309,7 +307,6 @@ protected String handleAge(String messageText, long chatId) {
responseMessage = "Спасибо! Учтем ваш ответ";
commandWaiter.put(chatId, NONE);
database.updateUserAge(chatId, userAge);

} else {
responseMessage = "Пожалуйста, введите корректное число (от 0 до 100)";
}
Expand All @@ -326,8 +323,9 @@ public void startBroadcasting() {
for (Long chatId : subscribedUsers) {
sendMessage(chatId, "Подписчику");
}
},0,1, TimeUnit.MINUTES);
}, 0, 1, TimeUnit.MINUTES);
}

private void sendMessage(long chatId, String text) {
SendMessage message=SendMessage.builder()
.chatId(chatId)
Expand All @@ -339,6 +337,7 @@ private void sendMessage(long chatId, String text) {
e.printStackTrace();
}
}

public String handleSubscription(String messageText, long chatId) {
if (isCommand(messageText)) {
commandWaiter.put(chatId, NONE);
Expand All @@ -362,7 +361,6 @@ public String handleSubscription(String messageText, long chatId) {
} catch (NumberFormatException e) {
responseMessage = "Пожалуйста, введите число (1 для подписки, 0 для отписки).";
}

return responseMessage;
}

Expand Down
7 changes: 5 additions & 2 deletions src/test/java/org/oopproject/HandleAgeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.sql.SQLException;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class HandleAgeTest extends TelegramBot {
private TelegramBot telegramBot;

public HandleAgeTest() {
public HandleAgeTest() throws SQLException {
super("dummy_token");
}

@BeforeEach
void setUp() {
void setUp() throws SQLException {
telegramBot = new TelegramBot("dummy_token");
}

Expand Down
7 changes: 5 additions & 2 deletions src/test/java/org/oopproject/HandleCommandsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.sql.SQLException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.oopproject.utils.Replies.getReply;

public class HandleCommandsTest extends TelegramBot {
private TelegramBot telegramBot;

public HandleCommandsTest() {
public HandleCommandsTest() throws SQLException {
super("dummy_token");
}

@BeforeEach
void setUp() {
void setUp() throws SQLException {
telegramBot = new TelegramBot("dummy_token");
}

Expand Down
7 changes: 5 additions & 2 deletions src/test/java/org/oopproject/HandleGenreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.sql.SQLException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class HandleGenreTest extends TelegramBot {
private TelegramBot telegramBot;

public HandleGenreTest() {
public HandleGenreTest() throws SQLException {
super("dummy_token");
}

@BeforeEach
void setUp() {
void setUp() throws SQLException {
telegramBot = new TelegramBot("dummy_token");
}

Expand Down
5 changes: 3 additions & 2 deletions src/test/java/org/oopproject/HandleYearTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.sql.SQLException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class HandleYearTest extends TelegramBot {
private TelegramBot telegramBot;

public HandleYearTest() {
public HandleYearTest() throws SQLException {
super("dummy_token");
}

@BeforeEach
void setUp() {
void setUp() throws SQLException {
telegramBot = new TelegramBot("dummy_token");
}

Expand Down

0 comments on commit f074f74

Please sign in to comment.