feat: set up project
This commit is contained in:
80
internal/platform/mongodb/order/repository.go
Normal file
80
internal/platform/mongodb/order/repository.go
Normal file
@@ -0,0 +1,80 @@
|
||||
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) error {
|
||||
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 fmt.Errorf("collection.UpdateOne: %s", err)
|
||||
}
|
||||
log.Printf("OrderCompleted update %+v\n", updateOpts)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user