2026-10-07 16:53:03 -06:00
|
|
|
package models
|
2026-10-09 15:38:08 -06:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2026-10-14 14:26:18 -06:00
|
|
|
"errors"
|
2026-10-09 15:38:08 -06:00
|
|
|
|
|
|
|
|
"github.com/jmoiron/sqlx"
|
2026-10-14 14:26:18 -06:00
|
|
|
"github.com/mattn/go-sqlite3"
|
2026-10-09 15:38:08 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Chat struct {
|
2026-10-14 14:26:18 -06:00
|
|
|
ID int `db:"id"`
|
|
|
|
|
Name string `db:"name"`
|
2026-10-09 15:38:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ChatModel struct {
|
|
|
|
|
ctx context.Context
|
|
|
|
|
db *sqlx.DB
|
|
|
|
|
}
|
2026-10-14 14:26:18 -06:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|