2024-08-14 14:44:21 +00:00
|
|
|
|
module repository
|
|
|
|
|
|
|
|
|
|
import models
|
2024-08-15 14:24:06 +00:00
|
|
|
|
import db.pg
|
2024-08-14 14:44:21 +00:00
|
|
|
|
|
2024-08-18 13:50:27 +00:00
|
|
|
|
// todo дообработать ошибки сейчас что то не возвращаются они
|
|
|
|
|
|
2024-08-14 14:44:21 +00:00
|
|
|
|
pub struct Repo {
|
|
|
|
|
pub mut:
|
|
|
|
|
db map[string]string
|
2024-08-15 15:54:02 +00:00
|
|
|
|
pg_db pg.DB
|
2024-08-14 14:44:21 +00:00
|
|
|
|
data_set_map map[string]models.Question
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-15 15:54:02 +00:00
|
|
|
|
pub fn (mut r Repo) connect_to_db(cfg pg.Config) ! {
|
|
|
|
|
r.pg_db = pg.connect(cfg)!
|
|
|
|
|
println('Connected to the database successfully.')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-08-14 14:51:08 +00:00
|
|
|
|
pub fn (mut r Repo)save_state(chat_id string,state string) {
|
|
|
|
|
r.db[chat_id]=state
|
|
|
|
|
println(r.db)
|
2024-08-14 14:44:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-27 11:35:31 +00:00
|
|
|
|
// pub fn (mut r Repo)get_state(chat_id string)!string{
|
|
|
|
|
// if chat_id in r.db {
|
|
|
|
|
// return r.db[chat_id]
|
|
|
|
|
// }
|
|
|
|
|
// return error('empty key')
|
|
|
|
|
// }
|
2024-08-14 14:44:21 +00:00
|
|
|
|
|
|
|
|
|
pub fn (mut r Repo)get_next_que_data(state string)!models.Question{
|
|
|
|
|
if state in r.data_set_map {
|
|
|
|
|
return r.data_set_map[state]
|
|
|
|
|
}
|
|
|
|
|
return error('empty')
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-02 15:20:27 +00:00
|
|
|
|
// todo допилить работу со стейтами, надо понять как без telegram id стейт получать и обновлять
|
2024-08-16 12:15:22 +00:00
|
|
|
|
// методы QuizRespondentState
|
|
|
|
|
// вызывается при /start, создает запись
|
2024-09-03 08:48:14 +00:00
|
|
|
|
pub fn (mut r Repo) create_state(state models.RespondentState) !i64 {
|
2024-08-18 09:13:02 +00:00
|
|
|
|
state_id := sql r.pg_db {
|
2024-08-16 12:15:22 +00:00
|
|
|
|
insert state into models.RespondentState
|
|
|
|
|
} or {
|
|
|
|
|
return error('Failed insert state: $err')
|
|
|
|
|
}
|
2024-08-18 09:13:02 +00:00
|
|
|
|
return state_id
|
2024-08-16 12:15:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// вызывать при любом другом, кроме вопроса с типом result
|
2024-09-03 08:48:14 +00:00
|
|
|
|
pub fn (mut r Repo) update_state(id i64, new_state i64) ! {
|
2024-08-16 12:15:22 +00:00
|
|
|
|
sql r.pg_db {
|
|
|
|
|
update models.RespondentState set state = new_state where id == id
|
|
|
|
|
} or {
|
|
|
|
|
return error('Failed update state: $err')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-03 08:48:14 +00:00
|
|
|
|
pub fn (mut r Repo) get_state(state_id i64)!models.RespondentState{
|
2024-08-27 11:35:31 +00:00
|
|
|
|
result := sql r.pg_db {
|
2024-09-03 08:48:14 +00:00
|
|
|
|
select from models.RespondentState where id == state_id
|
2024-08-27 11:35:31 +00:00
|
|
|
|
}or{
|
|
|
|
|
return error('error getting state from db')
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-29 12:20:40 +00:00
|
|
|
|
if result.len == 0{
|
|
|
|
|
return error('state not found')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result[0]
|
2024-08-27 11:35:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-16 12:15:22 +00:00
|
|
|
|
// выставляется при типе вопроса result = true
|
|
|
|
|
pub fn (mut r Repo) finish_state(id i64) ! {
|
|
|
|
|
sql r.pg_db {
|
|
|
|
|
update models.RespondentState set finish = true where id == id
|
|
|
|
|
} or {
|
|
|
|
|
return error('Failed finish state: $err')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-06 11:29:19 +00:00
|
|
|
|
pub fn (mut r Repo) get_state_by_tg_quiz_id(tg_id int,quiz_id i64)!models.RespondentState {
|
2024-09-05 14:29:13 +00:00
|
|
|
|
result := sql r.pg_db {
|
|
|
|
|
select from models.RespondentState where quiz_id == quiz_id && telegram_id == tg_id && finish == false
|
|
|
|
|
}or{
|
|
|
|
|
return error('error getting state from db')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if result.len == 0{
|
|
|
|
|
return error('state not found')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result[0]
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-10 12:14:50 +00:00
|
|
|
|
pub fn (mut r Repo)get_state_by_tg_quiz_id_finish_true(tg_id int,quiz_id i64)!int {
|
|
|
|
|
result := sql r.pg_db {
|
|
|
|
|
select from models.RespondentState where quiz_id == quiz_id && telegram_id == tg_id && finish == true
|
|
|
|
|
}or{
|
|
|
|
|
return error('error getting state from db')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if result.len == 0{
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result.len
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-16 12:15:22 +00:00
|
|
|
|
// методы с question
|
|
|
|
|
// получение вопроса по id
|
2024-08-15 15:54:02 +00:00
|
|
|
|
pub fn (mut r Repo) get_question_by_id(id i64) !models.QuizQuestion {
|
2024-08-16 09:16:42 +00:00
|
|
|
|
question := sql r.pg_db {
|
|
|
|
|
select from models.QuizQuestion where id == id && deleted == false
|
|
|
|
|
} or {
|
|
|
|
|
return error('Question not found')
|
|
|
|
|
}
|
2024-08-15 15:54:02 +00:00
|
|
|
|
|
2024-09-02 15:20:27 +00:00
|
|
|
|
if question.len==0{
|
|
|
|
|
return error('Question not found')
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-16 09:16:42 +00:00
|
|
|
|
return question[0]
|
2024-08-16 12:15:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-02 14:11:09 +00:00
|
|
|
|
pub fn (mut r Repo) get_questions_by_quiz_id(quiz_id i64) ![]models.QuizQuestion {
|
2024-08-29 12:20:40 +00:00
|
|
|
|
question := sql r.pg_db {
|
|
|
|
|
select from models.QuizQuestion where quiz_id == quiz_id && deleted == false order by page
|
|
|
|
|
} or {
|
|
|
|
|
return error('question not found for quiz_id')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if question.len==0{
|
|
|
|
|
return error("not found")
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-02 14:11:09 +00:00
|
|
|
|
return question
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-06 11:29:19 +00:00
|
|
|
|
pub fn (mut r Repo) get_all_results(id i64)![]models.QuizQuestion {
|
2024-09-02 14:11:09 +00:00
|
|
|
|
result := sql r.pg_db {
|
2024-09-02 15:20:27 +00:00
|
|
|
|
select from models.QuizQuestion where quiz_id == id && question_type == models.question_type_result.str() && deleted ==false
|
2024-09-02 14:11:09 +00:00
|
|
|
|
} or {
|
|
|
|
|
return error('question with type result not found for quiz_id')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// методы Quiz
|
|
|
|
|
|
2024-09-06 11:29:19 +00:00
|
|
|
|
pub fn (mut r Repo) get_quiz(quiz_id i64)!models.Quiz {
|
2024-09-02 14:11:09 +00:00
|
|
|
|
result := sql r.pg_db{
|
|
|
|
|
select from models.Quiz where id == quiz_id && deleted == false && archived == false
|
|
|
|
|
}or{
|
|
|
|
|
return error('QUIZ not found')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if result.len == 0{
|
|
|
|
|
return error('QUIZ not found')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result[0]
|
2024-08-29 12:20:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-16 12:15:22 +00:00
|
|
|
|
// методы TelegramIntegration
|
|
|
|
|
// получение интеграции по id
|
|
|
|
|
pub fn (mut r Repo) get_integration_by_id(id i64) !models.TelegramIntegration {
|
|
|
|
|
integration := sql r.pg_db {
|
|
|
|
|
select from models.TelegramIntegration where id == id && deleted == false limit 1
|
|
|
|
|
} or {
|
|
|
|
|
return error('Integration not found')
|
|
|
|
|
}
|
2024-08-19 09:13:40 +00:00
|
|
|
|
|
|
|
|
|
if integration.len == 0 {
|
|
|
|
|
return error('Integration not found')
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-16 12:15:22 +00:00
|
|
|
|
return integration[0]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// создает запись о интеграции
|
2024-08-16 15:17:45 +00:00
|
|
|
|
pub fn (mut r Repo) create_integration(integration models.TelegramIntegration) !int {
|
2024-08-18 09:13:02 +00:00
|
|
|
|
integration_id := sql r.pg_db {
|
2024-08-16 12:15:22 +00:00
|
|
|
|
insert integration into models.TelegramIntegration
|
|
|
|
|
} or {
|
|
|
|
|
return error('Failed create integration: $err')
|
|
|
|
|
}
|
2024-08-18 09:13:02 +00:00
|
|
|
|
return integration_id
|
2024-08-16 12:15:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// обновляет токен бота
|
|
|
|
|
pub fn (mut r Repo) update_bot_token(id i64, new_token string) ! {
|
|
|
|
|
sql r.pg_db {
|
|
|
|
|
update models.TelegramIntegration set bot_token = new_token where id == id && deleted == false
|
|
|
|
|
} or {
|
|
|
|
|
return error('Failed to update bot token: $err')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// обновляет поля ниже токена
|
|
|
|
|
pub fn (mut r Repo) update_integration_fields(id i64, integration models.TelegramIntegration) ! {
|
|
|
|
|
sql r.pg_db {
|
2024-08-18 08:37:44 +00:00
|
|
|
|
update models.TelegramIntegration set repeatable = integration.repeatable, bot_name = integration.bot_name, deleted = integration.deleted where id == id && deleted == false
|
2024-08-16 12:15:22 +00:00
|
|
|
|
} or {
|
|
|
|
|
return error('Failed update integration fields: $err')
|
|
|
|
|
}
|
2024-08-19 09:40:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn (mut r Repo)soft_delete_integration(quiz_id i64)!{
|
|
|
|
|
sql r.pg_db{
|
2024-09-02 15:20:27 +00:00
|
|
|
|
update models.TelegramIntegration set deleted = true where quiz_id == quiz_id
|
2024-08-19 09:40:37 +00:00
|
|
|
|
}or{
|
|
|
|
|
return error('Failed soft delete integration: $err')
|
|
|
|
|
}
|
2024-08-25 10:09:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn (mut r Repo) update_bot_name(bot_id i64,name string)!{
|
|
|
|
|
sql r.pg_db {
|
|
|
|
|
update models.TelegramIntegration set bot_name = name where id == bot_id
|
|
|
|
|
}or{
|
|
|
|
|
return error('Failed update bot name: $err')
|
|
|
|
|
}
|
2024-08-25 11:58:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-06 15:24:49 +00:00
|
|
|
|
// методы telegram_user_quiz_result
|
|
|
|
|
pub fn (mut r Repo) create_record_user_result(data models.TelegramUserQuizResult) ! {
|
|
|
|
|
sql r.pg_db {
|
|
|
|
|
insert data into models.TelegramUserQuizResult
|
|
|
|
|
} or {
|
|
|
|
|
return error('Failed create record user result: $err')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn (mut r Repo) update_record_user_result(data models.TelegramUserQuizResult)!{
|
|
|
|
|
sql r.pg_db {
|
2024-09-08 17:57:23 +00:00
|
|
|
|
update models.TelegramUserQuizResult set current_field = data.current_field,user_answer = data.user_answer, state = data.state,iter = data.iter where session == data.session
|
2024-09-06 15:24:49 +00:00
|
|
|
|
}or{
|
|
|
|
|
return error('Failed update bot name: $err')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn (r Repo) get_record_user_result(ses string)!models.TelegramUserQuizResult {
|
|
|
|
|
result := sql r.pg_db{
|
2024-09-08 10:28:38 +00:00
|
|
|
|
select from models.TelegramUserQuizResult where session == ses && state == models.user_result_tg_status_in_progress.str()
|
2024-09-06 15:24:49 +00:00
|
|
|
|
}or{
|
|
|
|
|
return error('record user result not found')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if result.len == 0{
|
|
|
|
|
return error('record user result not found')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result[0]
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-25 14:15:44 +00:00
|
|
|
|
// закрываем соединение с бд
|
2024-08-25 11:58:08 +00:00
|
|
|
|
pub fn (mut r Repo)stop_db(){
|
|
|
|
|
r.pg_db.close()
|
2024-08-15 15:54:02 +00:00
|
|
|
|
}
|