btc-pay-checker/internal/platform/mongodb/order/repository.go

81 lines
1.9 KiB
Go
Raw Normal View History

2023-07-19 11:47:46 +02:00
package order
import (
"context"
"fmt"
"log"
"time"
"gitea.urkob.com/urko/btc-pay-checker/internal/domain"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
type Repo struct {
collection *mongo.Collection
}
func NewRepo(collection *mongo.Collection) *Repo {
return &Repo{
collection: collection,
}
}
func (repo *Repo) Insert(ctx context.Context, order *domain.Order) (primitive.ObjectID, error) {
res, err := repo.collection.InsertOne(ctx, order)
if err != nil {
return primitive.NilObjectID, fmt.Errorf("v.collection.InsertOne: %s", err)
}
documentID, ok := res.InsertedID.(primitive.ObjectID)
if !ok {
return primitive.NilObjectID, fmt.Errorf("res.InsertedID")
}
return documentID, nil
}
func (repo *Repo) FromAmount(ctx context.Context, amount float64, timestamp time.Time) (*domain.Order, error) {
order := &domain.Order{}
filter := bson.M{
"amount": amount,
"expires_at": bson.M{
"$gte": timestamp,
},
"paid_at": time.Time{},
}
count, err := repo.collection.CountDocuments(ctx, filter)
if err != nil {
return nil, fmt.Errorf("doc.Decode: %s", err)
}
if count <= 0 {
return nil, fmt.Errorf("order not found")
} else if count > 1 {
return nil, fmt.Errorf("multiple orders found: %d", count)
}
doc := repo.collection.FindOne(ctx, filter)
if err := doc.Decode(order); err != nil {
return nil, fmt.Errorf("doc.Decode: %s", err)
}
return order, nil
}
func (repo *Repo) OrderCompleted(ctx context.Context, order *domain.Order) (*domain.Order, error) {
2023-07-19 11:47:46 +02:00
updateOpts, err := repo.collection.UpdateOne(ctx, bson.M{"_id": order.ID},
bson.M{
"tx": order.Tx,
"block": order.Block,
"paid_at": time.Now(),
},
)
if err != nil {
return nil, fmt.Errorf("collection.UpdateOne: %s", err)
2023-07-19 11:47:46 +02:00
}
log.Printf("OrderCompleted update %+v\n", updateOpts)
return order, nil
2023-07-19 11:47:46 +02:00
}