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
|
|
|
"encoding/json"
|
|
|
|
"github.com/go-redis/redis/v8"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
2024-01-03 15:45:41 +00:00
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
2023-12-29 12:41:26 +00:00
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/readpref"
|
2023-12-29 11:30:20 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-12-31 12:22:03 +00:00
|
|
|
type codewordRepository struct {
|
|
|
|
mdb *mongo.Collection
|
|
|
|
rdb *redis.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
type userRepository struct {
|
|
|
|
mdb *mongo.Collection
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUserRepository(deps Deps) *userRepository {
|
|
|
|
|
|
|
|
return &userRepository{mdb: deps.Mdb}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCodewordRepository(deps Deps) *codewordRepository {
|
|
|
|
|
|
|
|
return &codewordRepository{mdb: deps.Mdb, rdb: deps.Rdb}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *userRepository) FindByEmail(ctx context.Context, email string) (*models.User, error) {
|
|
|
|
var user models.User
|
2023-12-29 18:02:50 +00:00
|
|
|
|
2023-12-31 12:22:03 +00:00
|
|
|
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
|
2023-12-29 11:30:20 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 11:27:50 +00:00
|
|
|
func (r *codewordRepository) StoreRecoveryRecord(ctx context.Context, userID, email, key, url string) (string, error) {
|
2024-01-03 15:45:41 +00:00
|
|
|
newID := primitive.NewObjectID()
|
|
|
|
record := models.RestoreRequest{
|
|
|
|
ID: newID,
|
2023-12-31 12:22:03 +00:00
|
|
|
UserID: userID,
|
|
|
|
Email: email,
|
2024-01-03 15:45:41 +00:00
|
|
|
Sign: key,
|
2024-01-04 11:27:50 +00:00
|
|
|
SignUrl: url,
|
2023-12-31 12:22:03 +00:00
|
|
|
CreatedAt: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := r.mdb.InsertOne(ctx, record)
|
|
|
|
if err != nil {
|
2024-01-03 15:45:41 +00:00
|
|
|
return "", err
|
2023-12-31 12:22:03 +00:00
|
|
|
}
|
|
|
|
|
2024-01-03 15:45:41 +00:00
|
|
|
return newID.Hex(), nil
|
2023-12-29 11:30:20 +00:00
|
|
|
}
|
|
|
|
|
2024-01-03 15:45:41 +00:00
|
|
|
func (r *codewordRepository) InsertToQueue(ctx context.Context, userID string, email string, key []byte, id string) error {
|
2024-01-04 11:27:50 +00:00
|
|
|
task := models.RecoveryRecord{
|
|
|
|
ID: id,
|
|
|
|
UserID: userID,
|
|
|
|
Email: email,
|
|
|
|
Key: key,
|
|
|
|
}
|
2023-12-31 12:22:03 +00:00
|
|
|
|
2024-01-04 11:27:50 +00:00
|
|
|
taskBytes, err := json.Marshal(task)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-12-31 12:22:03 +00:00
|
|
|
|
2024-01-04 11:27:50 +00:00
|
|
|
if err := r.rdb.LPush(ctx, "recoveryQueue", taskBytes).Err(); err != nil {
|
|
|
|
return err
|
2023-12-31 12:22:03 +00:00
|
|
|
}
|
2023-12-29 11:30:20 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-04 11:27:50 +00:00
|
|
|
func (r *codewordRepository) GetRecoveryRecord(ctx context.Context, key string) (*models.RestoreRequest, error) {
|
|
|
|
var restoreRequest models.RestoreRequest
|
|
|
|
|
|
|
|
filter := bson.M{"sign": key}
|
|
|
|
|
|
|
|
err := r.mdb.FindOne(ctx, filter).Decode(&restoreRequest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &restoreRequest, nil
|
2023-12-29 11:30:20 +00:00
|
|
|
}
|
|
|
|
|
2023-12-31 12:22:03 +00:00
|
|
|
func (r *codewordRepository) Ping(ctx context.Context) error {
|
|
|
|
return r.mdb.Database().Client().Ping(ctx, readpref.Primary())
|
2023-12-29 11:30:20 +00:00
|
|
|
}
|