Skip to content

Commit

Permalink
Minor fix for no-db
Browse files Browse the repository at this point in the history
  • Loading branch information
aqerd committed Dec 1, 2024
1 parent e7e5648 commit 85a451a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/main/java/oop/project/handlers/Keyboards.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static ReplyKeyboardMarkup createKeyboard(List<String> commands, int colu

private static ReplyKeyboardMarkup buildCommandKeyboard() {
List<String> commands = List.of("Genre", "Year", "Movie Search", "Actor Search",
"Similar", "Recommended", "Popular", "Find by ID", "Top Rated", "Set Age", "Help");
"Similar", "Recommended", "Popular", "Find by ID", "Top Rated", "Set Age", "Subscribe", "Help");
int columns = 2;
return createKeyboard(commands, columns);
}
Expand Down
70 changes: 35 additions & 35 deletions src/main/java/oop/project/handlers/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ public class Message {
private static final TelegramClient TG_CLIENT = getTelegramClient();

private static final Database database = new Database();
private static final Gson gson = new Gson();
// private static final Gson gson = new Gson();

private static final int SEARCH_NUM = 6;

private static final HashMap<Integer, Integer> yearMovieIndexMap = new HashMap<>();
private static final HashMap<String, Integer> genreMovieIndexMap = new HashMap<>();
// private static final HashMap<Integer, Integer> yearMovieIndexMap = new HashMap<>();
// private static final HashMap<String, Integer> genreMovieIndexMap = new HashMap<>();
private static final Map<Long, CommandWaiter> COMMAND_WAITER = new ConcurrentHashMap<>();;

public static void handleMessage(Update update) {
long chatId = update.getMessage().getChatId();
database.insertChatId(chatId);
loadGenreIndexFromDatabase(chatId);
loadYearIndexFromDatabase(chatId);
// database.insertChatId(chatId);
// loadGenreIndexFromDatabase(chatId);
// loadYearIndexFromDatabase(chatId);

String messageText = update.getMessage().getText();
String responseMessage;
Expand Down Expand Up @@ -112,21 +112,21 @@ public static void handleMessage(Update update) {
}
}

private static void loadGenreIndexFromDatabase(long chatId) {
String jsonGenreString = database.getGenreIndexesJson(chatId);
if (jsonGenreString != null) {
Type type = new TypeToken<HashMap<String, Integer>>(){}.getType();
genreMovieIndexMap.putAll(gson.fromJson(jsonGenreString, type));
}
}

private static void loadYearIndexFromDatabase(long chatId) {
String jsonYearString = database.getYearIndexesJson(chatId);
if (jsonYearString != null) {
Type type = new TypeToken<HashMap<Integer, Integer>>(){}.getType();
yearMovieIndexMap.putAll(gson.fromJson(jsonYearString, type));
}
}
// private static void loadGenreIndexFromDatabase(long chatId) {
// String jsonGenreString = database.getGenreIndexesJson(chatId);
// if (jsonGenreString != null) {
// Type type = new TypeToken<HashMap<String, Integer>>(){}.getType();
// genreMovieIndexMap.putAll(gson.fromJson(jsonGenreString, type));
// }
// }
//
// private static void loadYearIndexFromDatabase(long chatId) {
// String jsonYearString = database.getYearIndexesJson(chatId);
// if (jsonYearString != null) {
// Type type = new TypeToken<HashMap<Integer, Integer>>(){}.getType();
// yearMovieIndexMap.putAll(gson.fromJson(jsonYearString, type));
// }
// }

public static String handleCommands(String messageText, long chatId) {
String responseMessage;
Expand Down Expand Up @@ -184,10 +184,10 @@ public static String handleCommands(String messageText, long chatId) {
return responseMessage;
}

private static void updateGenreIndexInDatabase(long chatId) {
String jsonGenreString = gson.toJson(genreMovieIndexMap);
database.updateGenreIndexesJson(chatId, jsonGenreString);
}
// private static void updateGenreIndexInDatabase(long chatId) {
// String jsonGenreString = gson.toJson(genreMovieIndexMap);
// database.updateGenreIndexesJson(chatId, jsonGenreString);
// }

public static String handleGenre(String messageText, long chatId) {
Validator<String> validCommand = new CommandValidator();
Expand All @@ -197,16 +197,16 @@ public static String handleGenre(String messageText, long chatId) {
}

String genreId = Genres.valueOf(messageText.toUpperCase().replace(" ", "_")).getGenreId();
int currentIndex = genreMovieIndexMap.getOrDefault(genreId, 0);
// int currentIndex = genreMovieIndexMap.getOrDefault(genreId, 0);
String responseMessage;
String moviesListBuilder = "Фильмы жанра " + messageText + ":\n";
MovieParameters params = MovieParameters.builder().withGenres(genreId).build();
ListDeserializer<FilmDeserializer> movies = tmdbService().findMovie(params).sortByPopularity();

try {
if (movies != null && movies.getResults() != null && !movies.getResults().isEmpty()) {
responseMessage = responseWithListOfMovies(movies, moviesListBuilder, currentIndex, genreMovieIndexMap, genreId);
updateGenreIndexInDatabase(chatId);
responseMessage = responseWithListOfMovies(movies, moviesListBuilder /*, currentIndex, genreMovieIndexMap, genreId*/);
// updateGenreIndexInDatabase(chatId);
} else {
responseMessage = "Извините, я не нашел фильмов для жанра " + messageText;
}
Expand All @@ -218,10 +218,10 @@ public static String handleGenre(String messageText, long chatId) {
return responseMessage;
}

private static void updateYearIndexInDatabase(long chatId) {
String jsonYearString = gson.toJson(yearMovieIndexMap);
database.updateYearIndexesJson(chatId, jsonYearString);
}
// private static void updateYearIndexInDatabase(long chatId) {
// String jsonYearString = gson.toJson(yearMovieIndexMap);
// database.updateYearIndexesJson(chatId, jsonYearString);
// }

public static String handleYear(String messageText, long chatId) {
Validator<String> validCommand = new CommandValidator();
Expand All @@ -236,16 +236,16 @@ public static String handleYear(String messageText, long chatId) {
}

int userYear = Integer.parseInt(messageText);
int currentIndex = yearMovieIndexMap.getOrDefault(userYear, 0);
// int currentIndex = yearMovieIndexMap.getOrDefault(userYear, 0);
String responseMessage;
String moviesListBuilder = "Фильмы, выпущенные в " + userYear + " году:\n";
MovieParameters params = MovieParameters.builder().withYear(userYear).build();
ListDeserializer<FilmDeserializer> movies = tmdbService().findMovie(params).sortByPopularity();

try {
if (movies != null && movies.getResults() != null && !movies.getResults().isEmpty()) {
responseMessage = responseWithListOfMovies(movies, moviesListBuilder, currentIndex, yearMovieIndexMap, userYear);
updateYearIndexInDatabase(chatId);
responseMessage = responseWithListOfMovies(movies, moviesListBuilder /*, currentIndex, yearMovieIndexMap, userYear*/);
// updateYearIndexInDatabase(chatId);
} else {
responseMessage = "Извините, я не нашел фильмов за " + userYear + " год";
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/oop/project/shared/Replies.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class Replies {
/popular - Популярные фильмы
/toprated - Высоко-оценённые фильмы
/findbyid - Поиск фильма по ID TMDB
/subscribe - Подписка на еженедельную рассылку
/setage - Установить возрастное ограничение""");
REPLIES.put("year", "Введите год, и я найду фильмы, выпущенные в этом году");
REPLIES.put("movie search", "Введите фильм который вы хотите найти");
Expand Down

0 comments on commit 85a451a

Please sign in to comment.