diff --git a/dao/users.go b/dao/users.go index 9ef0ac3..27c5f82 100644 --- a/dao/users.go +++ b/dao/users.go @@ -7,7 +7,6 @@ import ( "time" "github.com/Viva-con-Agua/vcago/vmdb" - "github.com/Viva-con-Agua/vcago/vmod" "go.mongodb.org/mongo-driver/bson" ) @@ -35,7 +34,7 @@ func UserInsert(ctx context.Context, i *models.UserDatabase) (result *models.Use return } -func UsersGet(i *models.UserQuery, token *models.AccessToken) (result *[]models.ListUser, list_size int64, err error) { +func UsersGet(i *models.UserQuery, token *models.AccessToken) (result *[]models.ListUser, listSize int64, err error) { if err = models.UsersPermission(token); err != nil { return } @@ -48,14 +47,18 @@ func UsersGet(i *models.UserQuery, token *models.AccessToken) (result *[]models. if err = UserCollection.Aggregate(ctx, pipeline, result); err != nil { return } - - count := vmod.Count{} - var cErr error - if cErr = UserViewCollection.AggregateOne(context.Background(), models.UserPermittedPipeline(token).Match(filter).Count().Pipe, &count); cErr != nil { - log.Print(cErr) - list_size = 1 + count := new([]Count) + if err = UserCollection.Aggregate( + context.Background(), + models.UserCountPipeline(filter).Pipe, + count, + ); err != nil { + return + } + if len(*count) == 0 { + listSize = 0 } else { - list_size = int64(count.Total) + listSize = (*count)[0].ListSize } return } diff --git a/models/user.go b/models/user.go index 3a63641..77d7f6e 100644 --- a/models/user.go +++ b/models/user.go @@ -308,6 +308,19 @@ func UserMatchEmail(email string) bson.D { return filter.Bson() } +func UserCountPipeline(filter bson.D) *vmdb.Pipeline { + pipe := UserPipeline(false) + 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 (i *User) AuthToken() (r *vcago.AuthToken, err error) { accessToken := &AccessToken{ ID: i.ID,