Add chat management.
Presently this only allows adding a chat to the bot.
This commit is contained in:
parent
d8ee0f8c16
commit
6a02e2f4fe
4 changed files with 100 additions and 2 deletions
|
|
@ -2,16 +2,39 @@ package models
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
type Chat struct {
|
||||
ID int
|
||||
Name string
|
||||
ID int `db:"id"`
|
||||
Name string `db:"name"`
|
||||
}
|
||||
|
||||
type ChatModel struct {
|
||||
ctx context.Context
|
||||
db *sqlx.DB
|
||||
}
|
||||
|
||||
func NewChatModel(ctx context.Context, db *sqlx.DB) *ChatModel {
|
||||
return &ChatModel{ctx, db}
|
||||
}
|
||||
|
||||
func (cm *ChatModel) AddChat(id int, name string) (*Chat, error) {
|
||||
var chat *Chat
|
||||
err := cm.db.GetContext(cm.ctx, chat,
|
||||
`insert into chats (id, name) values (?, ?)`, id, name)
|
||||
if err != nil {
|
||||
var serr sqlite3.Error
|
||||
if errors.As(err, &serr) {
|
||||
switch serr.ExtendedCode {
|
||||
case sqlite3.ErrConstraintUnique:
|
||||
return nil, ErrUniqueConstraint.Do(err)
|
||||
}
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return chat, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue