kickerbot_ng/src/db.go

168 lines
3.6 KiB
Go

package main
import (
"log"
"github.com/jmoiron/sqlx"
)
var schema = `
create table if not exists chats
(
id INTEGER PRIMARY KEY,
name TEXT,
username TEXT,
topic INTEGER,
active INTEGER
);
create table if not exists activations
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
code TEXT
);
create table if not exists messagesToDelete
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
message_id INTEGER,
chat_id INTEGER,
delete_date INTEGER
);
create table if not exists users
(
id INTEGER PRIMARY KEY,
name TEXT,
username TEXT
);
create table if not exists bans
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER,
reason TEXT,
ban_date INTEGER,
unban_date INTEGER,
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE
);
create table if not exists captchas
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER,
chat_id INTEGER,
message_id INTEGER,
correct_answer INTEGER,
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE
)
`
type ChatSchema struct {
Id int64 `json:"id" db:"id"`
Name string `json:"name" db:"name"`
Username string `json:"username" db:"username"`
Topic int64 `json:"topic" db:"topic"`
Active bool `json:"active" db:"active"`
}
type MessageToDelete struct {
Id int64 `db:"id"`
MessageId int `db:"message_id"`
ChatId int64 `db:"chat_id"`
DeleteDate int64 `db:"delete_date"`
}
type User struct {
Id int64 `db:"id"`
Name string `db:"name"`
Username string `db:"username"`
}
var db *sqlx.DB
func InitDb() error {
newdb, err := sqlx.Connect("sqlite", "./bot.db?_time_format=sqlite")
if err != nil {
return err
}
db = newdb
return nil
}
func NewChat(chat ChatSchema) error {
_, err := db.NamedExec(`insert into chats (id, name, username, topic, active) values (:id, :name, :username, :topic, :active)`, chat)
return err
}
func IsChatExists(id int64) bool {
var exists bool
err := db.Get(&exists, `SELECT exists(SELECT 1 FROM chats WHERE id = $1);`, id)
if err != nil {
log.Println("Can't check existing of chat", id, err)
return false
}
return exists
}
func IsChatActive(id int64) bool {
var active bool
err := db.Get(&active, `SELECT active from chats where id = $1`, id)
if err != nil {
return false
}
return active
}
func GetChatById(id int64) (ChatSchema, error) {
c := ChatSchema{}
err := db.Get(&c, `select * from chats where id = $1`, id)
return c, err
}
func NewActivation(code string) error {
_, err := db.Exec(`insert into activations (code) values ($1)`, code)
return err
}
func UseActivation(code string) bool {
exists := false
err := db.Get(&exists, `SELECT exists(SELECT 1 FROM chats WHERE code = $1);`, code)
if err != nil || !exists {
return false
}
_, err = db.Exec(`delete from activations where code = $1`, code)
return err == nil
}
func ActivateChat(id int64) error {
_, err := db.Exec(`update chats set active = 1 where id = $1`, id)
return err
}
func AddMessageToDelete(msg MessageToDelete) error {
_, err := db.NamedExec(`insert into messagesToDelete (message_id, chat_id, delete_date) values (:message_id, :chat_id, :delete_date)`, msg)
return err
}
func NewUser(user User) error {
_, err := db.NamedExec(`insert into users (id, name, username) values (:id, :name, :username)`, user)
return err
}
func IsUserExists(id int64) bool {
var exists bool
err := db.Get(&exists, `SELECT exists(SELECT 1 FROM users WHERE id = $1);`, id)
if err != nil {
log.Println("Can't check existing of chat", id, err)
return false
}
return exists
}
func GetUserById(id int64) (User, error) {
c := User{}
err := db.Get(&c, `select * from users where id = $1`, id)
return c, err
}