Skip to content

Commit

Permalink
rename message field to question
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-streltsov committed Oct 20, 2024
1 parent dfecb9b commit ba2d358
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
20 changes: 10 additions & 10 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Message struct {
ID int
UserID int64
Username string
Message string
Question string
Answer string
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand All @@ -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
}
Expand All @@ -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)
Expand All @@ -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
}
Expand All @@ -95,16 +95,16 @@ 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)
}
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
}
Expand All @@ -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
}

Expand Down
18 changes: 9 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"))
}
}

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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()))
Expand Down

0 comments on commit ba2d358

Please sign in to comment.