Merge pull request #1 from Selly-Modules/default-sort-latest

add default sort latest
This commit is contained in:
Nam Huynh 2021-11-09 11:30:15 +07:00 committed by GitHub
commit 269e92345d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 4 deletions

View File

@ -13,6 +13,7 @@ type AllQuery struct {
TargetID string
Page int64
Limit int64
Sort interface{}
}
// All ...
@ -23,15 +24,16 @@ func (s Service) All(query AllQuery) []Audit {
skip = query.Page * query.Limit
result = make([]Audit, 0)
)
opts := options.Find().SetLimit(query.Limit).SetSkip(skip).SetSort(bson.M{"_id": -1})
if query.Sort != nil {
opts.SetSort(query.Sort)
}
// Find db
cursor, err := s.DB.Collection(colName).Find(ctx, bson.D{
{"target", query.Target},
{"targetId", query.TargetID},
}, &options.FindOptions{
Limit: &query.Limit,
Skip: &skip,
})
}, opts)
if err != nil {
return result
}