codeword/internal/repository/user_repository.go

38 lines
789 B
Go
Raw Normal View History

2023-12-29 11:30:20 +00:00
package repository
import (
"codeword/internal/models"
2023-12-29 12:41:26 +00:00
"context"
2023-12-31 12:22:03 +00:00
"github.com/go-redis/redis/v8"
"go.mongodb.org/mongo-driver/bson"
2023-12-29 12:41:26 +00:00
"go.mongodb.org/mongo-driver/mongo"
2023-12-29 11:30:20 +00:00
)
2023-12-31 12:22:03 +00:00
type Deps struct {
Mdb *mongo.Collection
Rdb *redis.Client
2023-12-29 11:30:20 +00:00
}
2024-01-11 16:29:53 +00:00
type UserRepository struct {
2023-12-31 12:22:03 +00:00
mdb *mongo.Collection
}
2024-01-11 16:29:53 +00:00
func NewUserRepository(deps Deps) *UserRepository {
2023-12-31 12:22:03 +00:00
2024-01-11 16:29:53 +00:00
return &UserRepository{mdb: deps.Mdb}
2023-12-31 12:22:03 +00:00
}
2024-01-05 11:37:06 +00:00
// ищем пользователя по мейлу в коллекции users
2024-01-11 16:29:53 +00:00
func (r *UserRepository) FindByEmail(ctx context.Context, email string) (*models.User, error) {
2023-12-31 12:22:03 +00:00
var user models.User
2023-12-29 18:02:50 +00:00
2024-01-19 12:10:54 +00:00
err := r.mdb.FindOne(ctx, bson.M{"login": email}).Decode(&user)
2023-12-31 12:22:03 +00:00
if err != nil {
if err == mongo.ErrNoDocuments {
return nil, nil
}
2024-01-15 08:43:55 +00:00
return nil, ErrPromoUserNotFound
2023-12-31 12:22:03 +00:00
}
return &user, nil
2023-12-29 11:30:20 +00:00
}