Switched to Echotron, added topics

This commit is contained in:
2022-11-09 00:45:08 +03:00
parent b221253a6c
commit 43f881178b
9 changed files with 282 additions and 108 deletions

View File

@ -14,15 +14,36 @@ func (d *DB) ChatExists(ctx context.Context, chat Chat) bool {
return d.EntryExists(ctx, "chats", filter)
}
func (d *DB) GetChat(ctx context.Context, chatId int64) (Chat, error) {
filter := bson.D{primitive.E{Key: "id", Value: chatId}}
var result Chat
err := d.Database.Collection("chats").FindOne(ctx, filter).Decode(&result)
if err != nil {
Log("get chat error", err)
return Chat{}, err
}
return result, nil
}
func (d *DB) NewChat(ctx context.Context, chat Chat) error {
if d.ChatExists(ctx, chat) {
return errors.New("chat entry already exists")
}
err := d.NewEntry(ctx, "chats", chat)
if err != nil {
log.Fatal(err)
Log("new chat error", err)
return err
}
log.Printf("New chat added: %s (%d)\n", chat.Title, chat.Id)
return nil
}
func (d *DB) UpdateChat(ctx context.Context, chat Chat, updates bson.D) error {
filter := bson.D{primitive.E{Key: "id", Value: chat.Id}}
result, err := d.Database.Collection("chats").UpdateOne(ctx, filter, updates)
if err != nil {
Log("chat update error", err)
}
log.Printf("Chat updated: %v", result)
return err
}

View File

@ -1,8 +1,9 @@
package db
type Chat struct {
Id int64
Title string
Id int64
Title string
TopicId int64 `bson:"topic_id"`
}
type User struct {