codeword/internal/repository/user_repository.go

41 lines
1.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package repository
import (
"codeword/internal/models"
"context"
"github.com/go-redis/redis/v8"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
type Deps struct {
Mdb *mongo.Collection
Rdb *redis.Client
}
// todo: возможно стоит разделить два репозитория в одном файле на два файла чтобы не было путаницы,
// а deps структуру оставить одну дабы избежать путаницу
type userRepository struct {
mdb *mongo.Collection
}
func NewUserRepository(deps Deps) *userRepository {
return &userRepository{mdb: deps.Mdb}
}
// ищем пользователя по мейлу в коллекции users
func (r *userRepository) FindByEmail(ctx context.Context, email string) (*models.User, error) {
var user models.User
err := r.mdb.FindOne(ctx, bson.M{"email": email}).Decode(&user)
if err != nil {
if err == mongo.ErrNoDocuments {
return nil, nil
}
return nil, err
}
return &user, nil
}