List chats implements.
This commit is contained in:
parent
ccb838714a
commit
da076321ad
1 changed files with 20 additions and 0 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue