Skip to content

Commit

Permalink
solves #212
Browse files Browse the repository at this point in the history
  • Loading branch information
deinelieblings committed Sep 27, 2024
1 parent 55282f5 commit 3457c04
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
20 changes: 13 additions & 7 deletions dao/taking.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/Viva-con-Agua/vcago/vmod"
"github.com/Viva-con-Agua/vcapool"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)

var (
Expand Down Expand Up @@ -109,6 +108,10 @@ func TakingUpdate(ctx context.Context, i *models.TakingUpdate, token *vcapool.Ac
return
}

type Count struct {
ListSize int64 `bson:"list_size" json:"list_size"`
}

func TakingGet(ctx context.Context, query *models.TakingQuery, token *vcapool.AccessToken) (result []models.Taking, listSize int64, err error) {
if err = models.TakingPermission(token); err != nil {
return
Expand All @@ -124,15 +127,18 @@ func TakingGet(ctx context.Context, query *models.TakingQuery, token *vcapool.Ac
); err != nil {
return
}
opts := options.Count().SetHint("_id_")
if query.FullCount != "true" {
opts.SetSkip(query.Skip).SetLimit(query.Limit)
count := new([]Count)
if err = TakingCollection.Aggregate(
context.Background(),
models.TakingCountPipeline(filter).Pipe,
count,
); err != nil {
return
}
if cursor, cErr := UserViewCollection.Collection.CountDocuments(ctx, filter, opts); cErr != nil {
print(cErr)
if len(*count) == 0 {
listSize = 0
} else {
listSize = cursor
listSize = (*count)[0].ListSize
}
return
}
Expand Down
14 changes: 14 additions & 0 deletions models/taking.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ func TakingPipeline() *vmdb.Pipeline {
return pipe
}

func TakingCountPipeline(filter bson.D) *vmdb.Pipeline {
pipe := TakingPipeline()
pipe.Match(filter)
pipe.Append(bson.D{
{Key: "$group", Value: bson.D{
{Key: "_id", Value: nil}, {Key: "list_size", Value: bson.D{
{Key: "$sum", Value: 1},
}},
}},
})
pipe.Append(bson.D{{Key: "$project", Value: bson.D{{Key: "_id", Value: 0}}}})
return pipe
}

func TakingPipelineTicker() *vmdb.Pipeline {
pipe := vmdb.NewPipeline()
pipe.LookupUnwind(EventCollection, "_id", "taking_id", "event")
Expand Down

0 comments on commit 3457c04

Please sign in to comment.