Merge pull request #6 from Selly-Modules/add-filter-author-and-time
add filter author and time
This commit is contained in:
commit
e7c7c46b75
|
@ -3,6 +3,7 @@ package audit
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"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"
|
||||||
|
@ -12,9 +13,16 @@ import (
|
||||||
type AllQuery struct {
|
type AllQuery struct {
|
||||||
Target string
|
Target string
|
||||||
TargetID string
|
TargetID string
|
||||||
Page int64
|
|
||||||
Limit int64
|
// Additional filter
|
||||||
Sort interface{}
|
Author string
|
||||||
|
CreateTimeFrom time.Time
|
||||||
|
CreateTimeTo time.Time
|
||||||
|
|
||||||
|
// Pagination
|
||||||
|
Page int64
|
||||||
|
Limit int64
|
||||||
|
Sort interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// All ...
|
// All ...
|
||||||
|
@ -25,10 +33,7 @@ func (s Service) All(query AllQuery) (result []Audit, total int64) {
|
||||||
skip = query.Page * query.Limit
|
skip = query.Page * query.Limit
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
)
|
)
|
||||||
cond := bson.D{
|
cond := s.getQueryCondition(query)
|
||||||
{"target", query.Target},
|
|
||||||
{"targetId", query.TargetID},
|
|
||||||
}
|
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
@ -54,3 +59,30 @@ func (s Service) All(query AllQuery) (result []Audit, total int64) {
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
return result, total
|
return result, total
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s Service) getQueryCondition(query AllQuery) bson.D {
|
||||||
|
cond := bson.D{
|
||||||
|
{"target", query.Target},
|
||||||
|
{"targetId", query.TargetID},
|
||||||
|
}
|
||||||
|
if query.Author != "" {
|
||||||
|
cond = append(cond, bson.E{
|
||||||
|
Key: "author.id",
|
||||||
|
Value: query.Author,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
if !query.CreateTimeFrom.IsZero() || !query.CreateTimeTo.IsZero() {
|
||||||
|
v := bson.M{}
|
||||||
|
if !query.CreateTimeFrom.IsZero() {
|
||||||
|
v["$gte"] = query.CreateTimeFrom
|
||||||
|
}
|
||||||
|
if !query.CreateTimeTo.IsZero() {
|
||||||
|
v["$lt"] = query.CreateTimeTo
|
||||||
|
}
|
||||||
|
cond = append(cond, bson.E{
|
||||||
|
Key: "createdAt",
|
||||||
|
Value: v,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return cond
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue