goext/wmo/queryFind.go

77 lines
2.2 KiB
Go
Raw Permalink Normal View History

2023-06-06 21:33:49 +02:00
package wmo
import (
"context"
2023-08-21 15:08:35 +02:00
"errors"
2023-06-06 21:33:49 +02:00
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
2023-08-21 15:08:35 +02:00
"gogs.mikescher.com/BlackForestBytes/goext/exerr"
2023-06-06 21:33:49 +02:00
)
func (c *Coll[TData]) FindOne(ctx context.Context, filter bson.M) (TData, error) {
2023-06-10 16:22:14 +02:00
mongoRes := c.coll.FindOne(ctx, filter)
2023-08-21 15:08:35 +02:00
if err := mongoRes.Err(); err != nil {
return *new(TData), exerr.Wrap(err, "mongo-query[find-one] failed").
Str("collection", c.Name()).
Any("filter", filter).
Build()
}
2023-06-06 21:33:49 +02:00
2023-06-10 16:22:14 +02:00
return c.decodeSingle(ctx, mongoRes)
2023-06-06 21:33:49 +02:00
}
func (c *Coll[TData]) FindOneOpt(ctx context.Context, filter bson.M) (*TData, error) {
2023-06-10 16:22:14 +02:00
mongoRes := c.coll.FindOne(ctx, filter)
2023-06-06 21:33:49 +02:00
2023-06-10 16:22:14 +02:00
res, err := c.decodeSingle(ctx, mongoRes)
2023-08-21 15:08:35 +02:00
if errors.Is(err, mongo.ErrNoDocuments) {
2023-06-06 21:33:49 +02:00
return nil, nil
}
if err != nil {
2023-08-21 15:08:35 +02:00
return nil, exerr.Wrap(err, "mongo-query[find-one-opt] failed").Any("filter", filter).Str("collection", c.Name()).Build()
2023-06-06 21:33:49 +02:00
}
return &res, nil
}
func (c *Coll[TData]) FindOneByID(ctx context.Context, id EntityID) (TData, error) {
2023-06-10 16:22:14 +02:00
mongoRes := c.coll.FindOne(ctx, bson.M{"_id": id})
2023-08-21 15:08:35 +02:00
if err := mongoRes.Err(); err != nil {
return *new(TData), exerr.Wrap(err, "mongo-query[find-one-by-id] failed").
Str("collection", c.Name()).
Id("id", id).
Build()
}
2023-06-06 21:33:49 +02:00
2023-06-10 16:22:14 +02:00
return c.decodeSingle(ctx, mongoRes)
2023-06-06 21:33:49 +02:00
}
func (c *Coll[TData]) FindOneOptByID(ctx context.Context, id EntityID) (*TData, error) {
2023-06-10 16:22:14 +02:00
mongoRes := c.coll.FindOne(ctx, bson.M{"_id": id})
2023-06-06 21:33:49 +02:00
2023-06-10 16:22:14 +02:00
res, err := c.decodeSingle(ctx, mongoRes)
2023-08-21 15:08:35 +02:00
if errors.Is(err, mongo.ErrNoDocuments) {
2023-06-06 21:33:49 +02:00
return nil, nil
}
if err != nil {
2023-08-21 15:08:35 +02:00
return nil, exerr.Wrap(err, "mongo-query[find-one-opt-by-id] failed").Id("id", id).Str("collection", c.Name()).Build()
2023-06-06 21:33:49 +02:00
}
return &res, nil
}
func (c *Coll[TData]) Find(ctx context.Context, filter bson.M, opts ...*options.FindOptions) ([]TData, error) {
cursor, err := c.coll.Find(ctx, filter, opts...)
if err != nil {
2023-08-21 15:08:35 +02:00
return nil, exerr.Wrap(err, "mongo-query[find-one-opt] failed").Any("filter", filter).Any("opts", opts).Str("collection", c.Name()).Build()
2023-06-06 21:33:49 +02:00
}
2023-06-10 16:22:14 +02:00
res, err := c.decodeAll(ctx, cursor)
2023-06-06 21:33:49 +02:00
if err != nil {
2023-08-21 15:08:35 +02:00
return nil, exerr.Wrap(err, "failed to decode values").Build()
2023-06-06 21:33:49 +02:00
}
return res, nil
}