KickerBot/db/methods.go

49 lines
1.2 KiB
Go
Raw Normal View History

2022-02-02 00:37:58 +03:00
package db
import (
"context"
"errors"
"log"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)
func (d *DB) ChatExists(ctx context.Context, chat Chat) bool {
filter := bson.D{primitive.E{Key: "id", Value: chat.Id}}
return d.EntryExists(ctx, "chats", filter)
}
2022-11-09 00:45:08 +03:00
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
}
2022-02-02 00:37:58 +03:00
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 {
2022-11-09 00:45:08 +03:00
Log("new chat error", err)
2022-02-02 00:37:58 +03:00
return err
}
log.Printf("New chat added: %s (%d)\n", chat.Title, chat.Id)
return nil
}
2022-11-09 00:45:08 +03:00
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
}