added skelet for work with pg
This commit is contained in:
parent
beaaf8ef08
commit
d1e3e7a485
13
main.v
13
main.v
@ -7,6 +7,7 @@ import tg_handle
|
|||||||
import repository
|
import repository
|
||||||
import context
|
import context
|
||||||
import time
|
import time
|
||||||
|
import db.pg
|
||||||
|
|
||||||
//v -enable-globals run main.v
|
//v -enable-globals run main.v
|
||||||
__global (
|
__global (
|
||||||
@ -32,7 +33,17 @@ fn init() {
|
|||||||
fn main() {
|
fn main() {
|
||||||
mut c := context.background()
|
mut c := context.background()
|
||||||
mut ctx, cancel := context.with_cancel(mut c)
|
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)
|
tg_handle.new_tg_bot("6712573453:AAFqTOsgwe_j48ZQ1GzWKQDT5Nwr-SAWjz8",repo,mut ctx)
|
||||||
|
|
||||||
// todo graceful shutdown kak?
|
// todo graceful shutdown kak?
|
||||||
|
@ -38,3 +38,15 @@ pub mut:
|
|||||||
contact string
|
contact string
|
||||||
finish bool
|
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 struct Repo {
|
||||||
pub mut:
|
pub mut:
|
||||||
db map[string]string
|
db map[string]string
|
||||||
|
pg_db pg.DB
|
||||||
data_set_map map[string]models.Question
|
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) {
|
pub fn (mut r Repo)save_state(chat_id string,state string) {
|
||||||
r.db[chat_id]=state
|
r.db[chat_id]=state
|
||||||
println(r.db)
|
println(r.db)
|
||||||
@ -28,3 +35,13 @@ pub fn (mut r Repo)get_next_que_data(state string)!models.Question{
|
|||||||
return error('empty')
|
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) ! {
|
fn (mut b TgBot) start(mut ctx context.Context) ! {
|
||||||
mut new := true
|
|
||||||
mut last_offset :=0
|
mut last_offset :=0
|
||||||
stop_ch := chan int{}
|
stop_ch := chan int{}
|
||||||
|
|
||||||
@ -44,8 +43,6 @@ fn (mut b TgBot) start(mut ctx context.Context) ! {
|
|||||||
else {
|
else {
|
||||||
mut updates := b.bot.get_updates(offset: last_offset+1, limit: 100,timeout: int(2*time.second))
|
mut updates := b.bot.get_updates(offset: last_offset+1, limit: 100,timeout: int(2*time.second))
|
||||||
for update in updates {
|
for update in updates {
|
||||||
println(update.message.text)
|
|
||||||
println(update.message.text)
|
|
||||||
if last_offset < update.update_id {
|
if last_offset < update.update_id {
|
||||||
last_offset = update.update_id
|
last_offset = update.update_id
|
||||||
if update.message.text == "/start" {
|
if update.message.text == "/start" {
|
||||||
|
Loading…
Reference in New Issue
Block a user