From ba2d3586425936d9f7611837be768287ae9611a8 Mon Sep 17 00:00:00 2001 From: Artem Streltsov Date: Sun, 20 Oct 2024 17:34:58 +0100 Subject: [PATCH] rename message field to question --- database/database.go | 20 ++++++++++---------- main.go | 18 +++++++++--------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/database/database.go b/database/database.go index c245571..5e5e3b2 100644 --- a/database/database.go +++ b/database/database.go @@ -18,7 +18,7 @@ type Message struct { ID int UserID int64 Username string - Message string + Question string Answer string } @@ -50,7 +50,7 @@ func (db *DB) Migrate() { } func (db *DB) GetPendingMessages() ([]Message, error) { - rows, err := db.conn.Query("SELECT id, username, message FROM messages WHERE answered = 0") + rows, err := db.conn.Query("SELECT id, username, question FROM messages WHERE answered = 0") if err != nil { return nil, err } @@ -59,7 +59,7 @@ func (db *DB) GetPendingMessages() ([]Message, error) { var messages []Message for rows.Next() { var msg Message - if err := rows.Scan(&msg.ID, &msg.Username, &msg.Message); err != nil { + if err := rows.Scan(&msg.ID, &msg.Username, &msg.Question); err != nil { return nil, err } messages = append(messages, msg) @@ -68,7 +68,7 @@ func (db *DB) GetPendingMessages() ([]Message, error) { } func (db *DB) GetAnsweredMessages() ([]Message, error) { - rows, err := db.conn.Query("SELECT id, username, message, answer FROM messages WHERE answered = 1") + rows, err := db.conn.Query("SELECT id, username, question, answer FROM messages WHERE answered = 1") if err != nil { return nil, err } @@ -77,7 +77,7 @@ func (db *DB) GetAnsweredMessages() ([]Message, error) { var messages []Message for rows.Next() { var msg Message - if err := rows.Scan(&msg.ID, &msg.Username, &msg.Message, &msg.Answer); err != nil { + if err := rows.Scan(&msg.ID, &msg.Username, &msg.Question, &msg.Answer); err != nil { return nil, err } messages = append(messages, msg) @@ -86,7 +86,7 @@ func (db *DB) GetAnsweredMessages() ([]Message, error) { } func (db *DB) GetUserMessages(username string) ([]Message, error) { - rows, err := db.conn.Query("SELECT message, answer FROM messages WHERE username=?", username) + rows, err := db.conn.Query("SELECT question, answer FROM messages WHERE username=?", username) if err != nil { return nil, err } @@ -95,7 +95,7 @@ func (db *DB) GetUserMessages(username string) ([]Message, error) { var messages []Message for rows.Next() { var msg Message - if err := rows.Scan(&msg.Message, &msg.Answer); err != nil { + if err := rows.Scan(&msg.Question, &msg.Answer); err != nil { return nil, err } messages = append(messages, msg) @@ -103,8 +103,8 @@ func (db *DB) GetUserMessages(username string) ([]Message, error) { return messages, nil } -func (db *DB) SaveMessage(userID int64, username, message string) (int64, error) { - result, err := db.conn.Exec("INSERT INTO messages (user_id, username, message) VALUES (?, ?, ?)", userID, username, message) +func (db *DB) SaveMessage(userID int64, username, question string) (int64, error) { + result, err := db.conn.Exec("INSERT INTO messages (user_id, username, question) VALUES (?, ?, ?)", userID, username, question) if err != nil { return 0, err } @@ -113,7 +113,7 @@ func (db *DB) SaveMessage(userID int64, username, message string) (int64, error) func (db *DB) GetMessageByID(questionID int) (Message, error) { var msg Message - err := db.conn.QueryRow("SELECT user_id, username, message FROM messages WHERE id = ?", questionID).Scan(&msg.UserID, &msg.Username, &msg.Message) + err := db.conn.QueryRow("SELECT user_id, username, question FROM messages WHERE id = ?", questionID).Scan(&msg.UserID, &msg.Username, &msg.Question) return msg, err } diff --git a/main.go b/main.go index fca11ef..7d0d80c 100644 --- a/main.go +++ b/main.go @@ -51,16 +51,16 @@ func main() { func handleAdminMessage(bot *tgbotapi.BotAPI, database *db.DB, userStates map[int64]string, chatID int64, text string) { switch { case text == "/start": - msg := "Добро пожаловать, Вы - администратор. Вы можете использовать команды:\n/see_queries - посмотреть вопросы\n/answer - ответить на вопрос\n/see_answered - посмотреть Ваши ответы." + msg := "Добро пожаловать, Вы - администратор. Вы можете использовать команды:\n/see_questions - посмотреть вопросы\n/answer - ответить на вопрос\n/see_answers - посмотреть Ваши ответы." bot.Send(tgbotapi.NewMessage(chatID, msg)) - case text == "/see_queries": + case text == "/see_questions": messages, err := database.GetPendingMessages() if err != nil { log.Printf("Error reading messages: %v", err) return } sendMessagesList(bot, chatID, messages, "Нет новых вопросов.") - case text == "/see_answered": + case text == "/see_answers": messages, err := database.GetAnsweredMessages() if err != nil { log.Printf("Error reading messages: %v", err) @@ -98,11 +98,11 @@ func handleAdminMessage(bot *tgbotapi.BotAPI, database *db.DB, userStates map[in userStates[chatID] = "" return } - sendAnswerToUser(bot, msg.UserID, msg.Message, answer) + sendAnswerToUser(bot, msg.UserID, msg.Question, answer) bot.Send(tgbotapi.NewMessage(chatID, "Сообщение отправлено пользователю.")) userStates[chatID] = "" default: - bot.Send(tgbotapi.NewMessage(chatID, "Неизвестная команда.\nДоступные команды:\n/see_queries\n/see_answered\n/answer")) + bot.Send(tgbotapi.NewMessage(chatID, "Неизвестная команда.\nДоступные команды:\n/see_questions\n/see_answers\n/answer")) } } @@ -150,7 +150,7 @@ func parseQuestionID(text string) (int, error) { func sendMessagesList(bot *tgbotapi.BotAPI, chatID int64, messages []db.Message, emptyMsg string) { var response strings.Builder for _, msg := range messages { - response.WriteString(fmt.Sprintf("ID: %d\nПользователь: %s\nСообщение: %s\nОтветить: /answer_%d\n", msg.ID, msg.Username, msg.Message, msg.ID)) + response.WriteString(fmt.Sprintf("ID: %d\nПользователь: %s\nСообщение: %s\nОтветить: /answer_%d\n", msg.ID, msg.Username, msg.Question, msg.ID)) } if response.Len() == 0 { response.WriteString(emptyMsg) @@ -161,7 +161,7 @@ func sendMessagesList(bot *tgbotapi.BotAPI, chatID int64, messages []db.Message, func sendAnsweredList(bot *tgbotapi.BotAPI, chatID int64, messages []db.Message, emptyMsg string) { var response strings.Builder for _, msg := range messages { - response.WriteString(fmt.Sprintf("ID: %d\nПользователь: %s\nВопрос:\n%s\nОтвет:\n%s\n\n", msg.ID, msg.Username, msg.Message, msg.Answer)) + response.WriteString(fmt.Sprintf("ID: %d\nПользователь: %s\nВопрос:\n%s\nОтвет:\n%s\n\n", msg.ID, msg.Username, msg.Question, msg.Answer)) } if response.Len() == 0 { response.WriteString(emptyMsg) @@ -173,9 +173,9 @@ func sendUserMessagesList(bot *tgbotapi.BotAPI, chatID int64, messages []db.Mess var response strings.Builder response.WriteString("Ваши вопросы\n\n") for _, msg := range messages { - response.WriteString(fmt.Sprintf("Вопрос:\n%s\nОтвет:\n%s\n\n", msg.Message, msg.Answer)) + response.WriteString(fmt.Sprintf("Вопрос:\n%s\nОтвет:\n%s\n\n", msg.Question, msg.Answer)) } - if response.Len() == 0 { + if len(messages) == 0 { response.WriteString("У Вас нет вопросов.") } bot.Send(tgbotapi.NewMessage(chatID, response.String()))