package storage import ( "context" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" ) type MongodbStorage struct { Db *mongo.Client } func (m MongodbStorage) CreateUser(user *User) (primitive.ObjectID, error) { ctx := context.TODO() store := NewStore[User](&m, "users") return store.InsertOne(ctx, user) } func (m MongodbStorage) GetUserByID(id primitive.ObjectID) (*User, error) { ctx := context.TODO() store := NewStore[User](&m, "users") return store.GetById(ctx, id) } func (m MongodbStorage) GetUser(filter *bson.D) (*User, error) { ctx := context.TODO() store := NewStore[User](&m, "users") return store.GetOne(ctx, filter) } func (m MongodbStorage) GetUsers(filter *bson.D) ([]*User, error) { ctx := context.TODO() store := NewStore[User](&m, "users") return store.GetMany(ctx, filter) } func (m MongodbStorage) UpdateUser(user *User) error { ctx := context.TODO() store := NewStore[User](&m, "users") return store.ReplaceByID(ctx, user.Id, user) } func (m MongodbStorage) DeleteUser(id primitive.ObjectID) error { ctx := context.TODO() store := NewStore[User](&m, "users") return store.DeleteByID(ctx, id) } func (m MongodbStorage) CreateSession(s *Session) (primitive.ObjectID, error) { ctx := context.TODO() store := NewStore[Session](&m, "sessions") return store.InsertOne(ctx, s) } func (m MongodbStorage) GetSessionByToken(token string) (*Session, error) { ctx := context.TODO() store := NewStore[Session](&m, "sessions") return store.GetOne(ctx, &bson.D{{Key: "token", Value: token}}) } func (m MongodbStorage) GetUserSessions(user *User) ([]*Session, error) { ctx := context.TODO() store := NewStore[Session](&m, "sessions") return store.GetMany(ctx, &bson.D{{Key: "userid", Value: user.Id}}) } func (m MongodbStorage) DeleteSession(id primitive.ObjectID) error { ctx := context.TODO() store := NewStore[Session](&m, "sessions") return store.DeleteByID(ctx, id) } func (m MongodbStorage) CreateBookmark(bookmark *Bookmark) (primitive.ObjectID, error) { ctx := context.TODO() store := NewStore[Bookmark](&m, "bookmarks") return store.InsertOne(ctx, bookmark) } func (m MongodbStorage) GetBookmarkByID(id primitive.ObjectID) (*Bookmark, error) { ctx := context.TODO() store := NewStore[Bookmark](&m, "bookmarks") return store.GetById(ctx, id) } func (m MongodbStorage) GetBookmark(filter *bson.D) (*Bookmark, error) { ctx := context.TODO() store := NewStore[Bookmark](&m, "bookmarks") return store.GetOne(ctx, filter) } func (m MongodbStorage) GetBookmarks(filter *bson.D) ([]*Bookmark, error) { ctx := context.TODO() store := NewStore[Bookmark](&m, "bookmarks") return store.GetMany(ctx, filter) } func (m MongodbStorage) GetUserBookmarks(user *User, filter *bson.D) ([]*Bookmark, error) { passedFilter := getFilter(filter) f := bson.D{{Key: "userid", Value: user.Id}} f = append(f, passedFilter...) ctx := context.TODO() store := NewStore[Bookmark](&m, "bookmarks") return store.GetMany(ctx, &f) } func (m MongodbStorage) Collection(col string) *mongo.Collection { return m.Db.Database("nash").Collection(col) }