update query - response total to pagination #5
			
				
			
		
		
		
	| 
						 | 
					@ -2,6 +2,7 @@ package audit
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"context"
 | 
						"context"
 | 
				
			||||||
 | 
						"sync"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"go.mongodb.org/mongo-driver/bson"
 | 
						"go.mongodb.org/mongo-driver/bson"
 | 
				
			||||||
	"go.mongodb.org/mongo-driver/mongo/options"
 | 
						"go.mongodb.org/mongo-driver/mongo/options"
 | 
				
			||||||
| 
						 | 
					@ -17,27 +18,39 @@ type AllQuery struct {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// All ...
 | 
					// All ...
 | 
				
			||||||
func (s Service) All(query AllQuery) []Audit {
 | 
					func (s Service) All(query AllQuery) (result []Audit, total int64) {
 | 
				
			||||||
	var (
 | 
						var (
 | 
				
			||||||
		ctx     = context.Background()
 | 
							ctx     = context.Background()
 | 
				
			||||||
		colName = getColName(query.Target)
 | 
							colName = getColName(query.Target)
 | 
				
			||||||
		skip    = query.Page * query.Limit
 | 
							skip    = query.Page * query.Limit
 | 
				
			||||||
		result  = make([]Audit, 0)
 | 
							wg      sync.WaitGroup
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
 | 
						cond := bson.D{
 | 
				
			||||||
 | 
							{"target", query.Target},
 | 
				
			||||||
 | 
							{"targetId", query.TargetID},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						wg.Add(1)
 | 
				
			||||||
 | 
						go func() {
 | 
				
			||||||
 | 
							defer wg.Done()
 | 
				
			||||||
		opts := options.Find().SetLimit(query.Limit).SetSkip(skip).SetSort(bson.M{"_id": -1})
 | 
							opts := options.Find().SetLimit(query.Limit).SetSkip(skip).SetSort(bson.M{"_id": -1})
 | 
				
			||||||
		if query.Sort != nil {
 | 
							if query.Sort != nil {
 | 
				
			||||||
			opts.SetSort(query.Sort)
 | 
								opts.SetSort(query.Sort)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Find db
 | 
							// Find db
 | 
				
			||||||
	cursor, err := s.DB.Collection(colName).Find(ctx, bson.D{
 | 
							cursor, err := s.DB.Collection(colName).Find(ctx, cond, opts)
 | 
				
			||||||
		{"target", query.Target},
 | 
					 | 
				
			||||||
		{"targetId", query.TargetID},
 | 
					 | 
				
			||||||
	}, opts)
 | 
					 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
		return result
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		defer cursor.Close(ctx)
 | 
							defer cursor.Close(ctx)
 | 
				
			||||||
		cursor.All(ctx, &result)
 | 
							cursor.All(ctx, &result)
 | 
				
			||||||
	return result
 | 
						}()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						wg.Add(1)
 | 
				
			||||||
 | 
						go func() {
 | 
				
			||||||
 | 
							defer wg.Done()
 | 
				
			||||||
 | 
							total, _ = s.DB.Collection(colName).CountDocuments(ctx, cond)
 | 
				
			||||||
 | 
						}()
 | 
				
			||||||
 | 
						wg.Wait()
 | 
				
			||||||
 | 
						return result, total
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue