diff --git a/main.v b/main.v index fe182db..6a2df31 100644 --- a/main.v +++ b/main.v @@ -7,6 +7,7 @@ import tg_handle import repository import context import time +import db.pg //v -enable-globals run main.v __global ( @@ -32,7 +33,17 @@ fn init() { fn main() { mut c := context.background() mut ctx, cancel := context.with_cancel(mut c) - repo := repository.Repo{db: map[string]string{},data_set_map: data_set_map} + mut repo := repository.Repo{db: map[string]string{},data_set_map: data_set_map} + // todo не коннектит к бд надо понять почему + repo.connect_to_db(pg.Config{ + host: 'localhost' + port: 35432 + user: 'squiz' + password: 'Redalert2' + dbname: 'squiz' + })or { + eprintln("err conect to db") + } tg_handle.new_tg_bot("6712573453:AAFqTOsgwe_j48ZQ1GzWKQDT5Nwr-SAWjz8",repo,mut ctx) // todo graceful shutdown kak? diff --git a/models/models.v b/models/models.v index 46c1da1..6c3def7 100644 --- a/models/models.v +++ b/models/models.v @@ -38,3 +38,15 @@ pub mut: contact string finish bool } + +pub struct QuizQuestion { +pub mut: + id i64 + quiz_id i64 + title string + description string + question_type string + content string + required bool +} + diff --git a/repository/repository.v b/repository/repository.v index dbdc7c0..7b6bd7b 100644 --- a/repository/repository.v +++ b/repository/repository.v @@ -6,9 +6,16 @@ import db.pg pub struct Repo { pub mut: db map[string]string + pg_db pg.DB data_set_map map[string]models.Question } +pub fn (mut r Repo) connect_to_db(cfg pg.Config) ! { + r.pg_db = pg.connect(cfg)! + println('Connected to the database successfully.') +} + + pub fn (mut r Repo)save_state(chat_id string,state string) { r.db[chat_id]=state println(r.db) @@ -28,3 +35,13 @@ pub fn (mut r Repo)get_next_que_data(state string)!models.Question{ return error('empty') } +pub fn (mut r Repo) get_question_by_id(id i64) !models.QuizQuestion { + query := 'SELECT id, quiz_id, title, description, questiontype, required, content + FROM question + WHERE id = $1 AND deleted = FALSE' + + result := r.pg_db.exec_param(query, id.str())! + + println(result) + return models.QuizQuestion{} +} \ No newline at end of file diff --git a/tg_handle/tg.v b/tg_handle/tg.v index b54b3c5..b5facbd 100644 --- a/tg_handle/tg.v +++ b/tg_handle/tg.v @@ -26,7 +26,6 @@ pub fn new_tg_bot(token string,repo repository.Repo,mut ctx context.Context){ } fn (mut b TgBot) start(mut ctx context.Context) ! { - mut new := true mut last_offset :=0 stop_ch := chan int{} @@ -44,8 +43,6 @@ fn (mut b TgBot) start(mut ctx context.Context) ! { else { mut updates := b.bot.get_updates(offset: last_offset+1, limit: 100,timeout: int(2*time.second)) for update in updates { - println(update.message.text) - println(update.message.text) if last_offset < update.update_id { last_offset = update.update_id if update.message.text == "/start" {