List chats implements.

This commit is contained in:
Benjamin A. Shelton 2026-10-20 15:16:47 -06:00
commit da076321ad
Signed by: zancarius
GPG key ID: 8BD42DBD6C337E01

View file

@ -42,3 +42,23 @@ func (cm *ChatModel) AddChat(id int, name string) (chat *Chat, err error) {
}
return chat, nil
}
func (cm *ChatModel) ListChats() ([]*Chat, error) {
var chats []*Chat
err := cm.db.GetContext(cm.ctx, chats,
`select id, name from chats where id < 0`)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return chats, nil
}
var serr sqlite3.Error
if errors.As(err, &serr) {
switch serr.ExtendedCode {
case sqlite3.ErrConstraintUnique, sqlite3.ErrConstraintPrimaryKey:
return nil, ErrUniqueConstraint.Do(err)
}
}
return nil, err
}
return chats, nil
}