42 lines
926 B
Go
42 lines
926 B
Go
package helpers
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"gitea.pena/PenaSide/treasurer/internal/models"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
)
|
|
|
|
type MongoHelper struct {
|
|
collection *mongo.Collection
|
|
}
|
|
|
|
func NewMongoHelper(ctx context.Context, config *mongopkg.Configuration) (*MongoHelper, error) {
|
|
database, err := mongopkg.Connect(ctx, &mongopkg.ConnectDeps{
|
|
Configuration: config,
|
|
Timeout: 10 * time.Second,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &MongoHelper{collection: database.Collection("payments")}, nil
|
|
}
|
|
|
|
func (h *MongoHelper) GetPayments(ctx context.Context) ([]models.Payment, error) {
|
|
return mongopkg.Find[models.Payment](ctx, &mongopkg.RequestSettings{
|
|
Driver: h.collection,
|
|
Filter: bson.M{},
|
|
})
|
|
}
|
|
|
|
func (h *MongoHelper) Clear(ctx context.Context) error {
|
|
if _, err := h.collection.DeleteMany(ctx, bson.M{}); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|