diff --git a/.run/TelegaBot [generateMigration].run.xml b/.run/TelegaBot [generateMigration].run.xml index 70a4b6a..8230d98 100644 --- a/.run/TelegaBot [generateMigration].run.xml +++ b/.run/TelegaBot [generateMigration].run.xml @@ -4,7 +4,7 @@ diff --git a/build.gradle b/build.gradle index 78a3742..e8a8d20 100644 --- a/build.gradle +++ b/build.gradle @@ -2,7 +2,7 @@ plugins { id 'java' id 'org.springframework.boot' version '3.4.2' id 'io.spring.dependency-management' version '1.1.7' - id 'org.flywaydb.flyway' version '10.11.0' + id 'org.flywaydb.flyway' version '11.3.1' } group = 'ru.cathub' @@ -28,7 +28,8 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-cache' annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor' - implementation 'org.hibernate:hibernate-tools:6.4.4.Final' + // https://mvnrepository.com/artifact/org.hibernate/hibernate-tools + annotationProcessor 'org.hibernate.orm:hibernate-jpamodelgen:6.4.4.Final' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' @@ -52,6 +53,8 @@ dependencies { // https://mvnrepository.com/artifact/io.hypersistence/hypersistence-utils-hibernate-63 implementation group: 'io.hypersistence', name: 'hypersistence-utils-hibernate-63', version: '3.9.1' +// https://mvnrepository.com/artifact/org.flywaydb/flyway-database-postgresql + runtimeOnly group: 'org.flywaydb', name: 'flyway-database-postgresql', version: '11.3.1' } @@ -60,26 +63,6 @@ tasks.named('test') { useJUnitPlatform() } -tasks.register('generateMigration', JavaExec) { - dependsOn 'classes' - - classpath = sourceSets.main.runtimeClasspath - mainClass = 'org.hibernate.tool.Main' - - args = [ - '--hbm2ddl-auto', 'update', - '--properties', 'application.properties', - '--output', 'src/main/resources/db/migration/V' + getNextMigrationVersion() + '__auto_generated.sql' - ] -} - -def getNextMigrationVersion() { - def migrationDir = project.file('src/main/resources/db/migration') - def existing = migrationDir.listFiles()?.collect { - (it.name =~ /V(\d+)__/)[0][1] - }?.max() ?: 0 - return String.format("%03d", existing.toInteger() + 1) -} flyway { locations = ['classpath:db/migration'] diff --git a/src/main/java/ru/cathub/telegabot/bot/KeyboardHelper.java b/src/main/java/ru/cathub/telegabot/bot/KeyboardHelper.java index a46be0b..f38c47c 100644 --- a/src/main/java/ru/cathub/telegabot/bot/KeyboardHelper.java +++ b/src/main/java/ru/cathub/telegabot/bot/KeyboardHelper.java @@ -28,7 +28,7 @@ public class KeyboardHelper { KeyboardRow row2 = new KeyboardRow(); row2.add(new KeyboardButton(BACK)); - keyboardRows.add(row1); + //keyboardRows.add(row1); keyboardRows.add(row2); ReplyKeyboardMarkup keyboardMarkup = new ReplyKeyboardMarkup(keyboardRows); diff --git a/src/main/java/ru/cathub/telegabot/bot/TelegaBot.java b/src/main/java/ru/cathub/telegabot/bot/TelegaBot.java index 057fbd9..271cc3b 100644 --- a/src/main/java/ru/cathub/telegabot/bot/TelegaBot.java +++ b/src/main/java/ru/cathub/telegabot/bot/TelegaBot.java @@ -2,6 +2,7 @@ package ru.cathub.telegabot.bot; import jakarta.annotation.PostConstruct; import lombok.Getter; +import lombok.extern.slf4j.Slf4j; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -27,12 +28,14 @@ import ru.cathub.telegabot.model.BotUser; import ru.cathub.telegabot.service.BookManageService; import ru.cathub.telegabot.service.DataService; import ru.cathub.telegabot.service.TelegramBotService; +import ru.cathub.telegabot.service.TelegramClientService; import java.io.IOException; import java.nio.file.Files; import java.util.List; @Component +@Slf4j public class TelegaBot implements SpringLongPollingBot, LongPollingSingleThreadUpdateConsumer { // private final QwenService qwenService; @@ -48,17 +51,19 @@ public class TelegaBot implements SpringLongPollingBot, LongPollingSingleThreadU private final String botUsername; private final OkHttpTelegramClient telegramClient; private final TelegramBotService telegramBotService; + private final TelegramClientService telegramClientService; private final DataService dataService; public TelegaBot( @Value("${bot.token}") String botToken, @Value("${bot.name}") String botUsername, - TelegramBotService telegramBotService, DataService dataService + TelegramBotService telegramBotService, TelegramClientService telegramClientService, DataService dataService ) { this.botToken = botToken; this.botUsername = botUsername; this.telegramClient = new OkHttpTelegramClient(botToken); this.telegramBotService = telegramBotService; + this.telegramClientService = telegramClientService; this.dataService = dataService; } @@ -106,7 +111,8 @@ public class TelegaBot implements SpringLongPollingBot, LongPollingSingleThreadU try { telegramBotService.processInput(user,messageText); } catch (ServiceException e) { - throw new RuntimeException(e); + telegramClientService.sendMessage(user, "❌ Ошибка!"); + log.error(e.getMessage()); } } else if (update.hasMessage() && update.getMessage().hasDocument()) { //TODO diff --git a/src/main/java/ru/cathub/telegabot/model/Book.java b/src/main/java/ru/cathub/telegabot/model/Book.java index ba172d1..e879b1b 100644 --- a/src/main/java/ru/cathub/telegabot/model/Book.java +++ b/src/main/java/ru/cathub/telegabot/model/Book.java @@ -3,6 +3,8 @@ package ru.cathub.telegabot.model; import jakarta.persistence.*; import lombok.*; +import java.sql.Date; + @Data @Builder @NoArgsConstructor diff --git a/src/main/java/ru/cathub/telegabot/model/BookEditStateCache.java b/src/main/java/ru/cathub/telegabot/model/BookEditStateCache.java index 77125af..d2f523d 100644 --- a/src/main/java/ru/cathub/telegabot/model/BookEditStateCache.java +++ b/src/main/java/ru/cathub/telegabot/model/BookEditStateCache.java @@ -21,6 +21,7 @@ public class BookEditStateCache { NONE } + @Builder.Default() private BookEditState bookEditState = BookEditState.NONE; private Long bookId; diff --git a/src/main/java/ru/cathub/telegabot/service/impl/ProfileServiceImpl.java b/src/main/java/ru/cathub/telegabot/service/impl/ProfileServiceImpl.java index a4864e6..10e1f31 100644 --- a/src/main/java/ru/cathub/telegabot/service/impl/ProfileServiceImpl.java +++ b/src/main/java/ru/cathub/telegabot/service/impl/ProfileServiceImpl.java @@ -23,7 +23,7 @@ public class ProfileServiceImpl implements ProfileService { userRepository.save(user); telegramClientService.sendMessage( user, - "⌨️ Режим редактирования профиля:\nУстановите цель по количеству книг или вернитесь назад", + "⌨️ Режим редактирования профиля:\nВведите цель по количеству книг или вернитесь назад", KeyboardHelper.getProfileMenuKeyboard() ); } @@ -48,7 +48,8 @@ public class ProfileServiceImpl implements ProfileService { throw new NumberFormatException(); } } catch (NumberFormatException e) { - throw new ServiceException("❌ Пожалуйста, введите положительное число"); + telegramClientService.sendMessage(user, "❌ Ошибка! Введите целое число", + KeyboardHelper.getProfileMenuKeyboard()); } } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index a6618c4..7c0b96e 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -9,8 +9,7 @@ cbr.currency.rates.xml.url=http://www.cbr.ru/scripts/XML_daily.asp spring.datasource.url=jdbc:postgresql://192.168.0.10:5432/telegabot spring.datasource.username=postgres spring.datasource.password=Vftrixpo05 -spring.jpa.hibernate.ddl-auto=update -spring.jpa.generate-ddl=true +spring.jpa.hibernate.ddl-auto=validate spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect # Flyway Configuration diff --git a/src/main/resources/db/migration/V1__.sql b/src/main/resources/db/migration/V1__.sql new file mode 100644 index 0000000..559ea6b --- /dev/null +++ b/src/main/resources/db/migration/V1__.sql @@ -0,0 +1,36 @@ +CREATE TABLE books +( + id BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL, + title VARCHAR(255), + author VARCHAR(255), + rating INTEGER, + bot_user_id BIGINT, + CONSTRAINT pk_books PRIMARY KEY (id) +); + +CREATE TABLE bot_user +( + id BIGINT NOT NULL, + books_to_read INTEGER, + chat_id BIGINT, + user_type VARCHAR(255), + working_mode VARCHAR(255), + created_date_time TIMESTAMP WITHOUT TIME ZONE, + CONSTRAINT pk_botuser PRIMARY KEY (id) +); + +CREATE TABLE messages +( + id BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL, + content TEXT, + type VARCHAR(255), + created_at TIMESTAMP WITHOUT TIME ZONE, + bot_user_id BIGINT, + CONSTRAINT pk_messages PRIMARY KEY (id) +); + +ALTER TABLE books + ADD CONSTRAINT FK_BOOKS_ON_BOT_USER FOREIGN KEY (bot_user_id) REFERENCES bot_user (id); + +ALTER TABLE messages + ADD CONSTRAINT FK_MESSAGES_ON_BOTUSER FOREIGN KEY (bot_user_id) REFERENCES bot_user (id); \ No newline at end of file diff --git a/src/main/resources/db/migration/V1__Initial_schema.sql b/src/main/resources/db/migration/V1__Initial_schema.sql deleted file mode 100644 index 6ecfbef..0000000 --- a/src/main/resources/db/migration/V1__Initial_schema.sql +++ /dev/null @@ -1,29 +0,0 @@ -CREATE TABLE IF NOT EXISTS bot_user ( - id BIGINT PRIMARY KEY, - chat_id BIGINT NOT NULL, - user_type VARCHAR(20) NOT NULL, - working_mode VARCHAR(20) NOT NULL, - books_to_read INT, - created_date_time TIMESTAMP, - UNIQUE(chat_id) -); - -CREATE TABLE IF NOT EXISTS books ( - id BIGSERIAL PRIMARY KEY, - title VARCHAR(255) NOT NULL, - author VARCHAR(255) NOT NULL, - rating INT CHECK (rating BETWEEN 1 AND 5), - bot_user_id BIGINT NOT NULL REFERENCES bot_user(id), - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP -); - -CREATE TABLE IF NOT EXISTS messages ( - id BIGSERIAL PRIMARY KEY, - content TEXT NOT NULL, - type VARCHAR(20) NOT NULL, - created_at TIMESTAMP NOT NULL, - bot_user_id BIGINT NOT NULL REFERENCES bot_user(id) -); - -CREATE INDEX IF NOT EXISTS idx_books_bot_user_id ON books(bot_user_id); -CREATE INDEX IF NOT EXISTS idx_messages_bot_user_id ON messages(bot_user_id);