added skelet for work with pg

This commit is contained in:
Pavel 2024-08-15 18:54:02 +03:00
parent beaaf8ef08
commit d1e3e7a485
4 changed files with 41 additions and 4 deletions

13
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?

@ -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
}

@ -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{}
}

@ -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" {