Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: WEOS-1134 Update list method on projection to accept filter map #90

Merged
merged 15 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions controllers/rest/dtos.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ type ListApiResponse struct {

//FilterProperties is the properties need to use filters
type FilterProperties struct {
Field string
Operator string
Value interface{}
Values []interface{}
Field string `json:"field"`
Operator string `json:"operator"`
Value interface{} `json:"value"`
Values []interface{} `json:"values"`
}
51 changes: 32 additions & 19 deletions projections/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func (p *GORMProjection) GetContentEntities(ctx context.Context, entityFactory w
return entities, count, result.Error
}

//paginate to query results
//paginate is used for querying results
func paginate(page int, limit int) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
actualLimit := limit
Expand All @@ -279,7 +279,7 @@ func paginate(page int, limit int) func(db *gorm.DB) *gorm.DB {
}
}

// function that sorts the query results
//sort is used to sort the query results
func sort(order map[string]string) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
for key, value := range order {
Expand Down Expand Up @@ -311,26 +311,39 @@ func NewProjection(ctx context.Context, db *gorm.DB, logger weos.Log) (*GORMProj

FilterQuery = func(options map[string]FilterProperty) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
for _, filter := range options {
operator := "="
switch filter.Operator {
case "gt":
operator = ">"
case "lt":
case "ne":
case "like":
//TODO check db
case "in":
if options != nil {
for _, filter := range options {
operator := "="
switch filter.Operator {
case "gt":
operator = ">"
case "lt":
operator = "<"
case "ne":
operator = "!="
case "like":
if projection.db.Dialector.Name() == "postgres" {
operator = "ILIKE"
} else {
operator = " LIKE"
}
case "in":
operator = "IN"

}
}

if len(filter.Values) == 0 {
db.Where(filter.Field+operator+"?", filter.Value)
} else {
// TODO add a for loop to generate the number of ? needed
db.Where(filter.Field+operator+"?", filter.Values...)
}
if len(filter.Values) == 0 {
if filter.Operator == "like" {
db.Where(filter.Field+" "+operator+" ?", "%"+filter.Value.(string)+"%")
} else {
db.Where(filter.Field+" "+operator+" ?", filter.Value)
}

} else {
db.Where(filter.Field+" "+operator+" ?", filter.Values)
}

}
}
return db
}
Expand Down
42 changes: 9 additions & 33 deletions projections/projections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2150,7 +2150,10 @@ components:
if found != limit {
t.Errorf("expected to find %d blogs got %d", limit, found)
}

//err = gormDB.Migrator().DropTable("Blog")
//if err != nil {
// t.Errorf("error removing table '%s' '%s'", "Blog", err)
//}
})
t.Run("get a basic list with the foreign key returned", func(t *testing.T) {

Expand Down Expand Up @@ -2351,7 +2354,7 @@ components:
filter := &projections.FilterProperty{
Field: "id",
Operator: "ne",
Value: "1",
Value: uint(1),
Values: nil,
}
filters := map[string]interface{}{filter.Field: filter}
Expand Down Expand Up @@ -2404,7 +2407,7 @@ components:
"id": "asc",
}
ctxt := context.Background()
vals := []string{"hugs2"}
vals := []interface{}{"hugs2"}
filter := &projections.FilterProperty{
Field: "title",
Operator: "in",
Expand Down Expand Up @@ -2432,7 +2435,7 @@ components:
"id": "asc",
}
ctxt := context.Background()
arrValues := []string{"hugs1", "hugs3"}
arrValues := []interface{}{"hugs1", "hugs3"}
filter := &projections.FilterProperty{
Field: "title",
Operator: "in",
Expand Down Expand Up @@ -2463,7 +2466,7 @@ components:
filter := &projections.FilterProperty{
Field: "id",
Operator: "lt",
Value: "2",
Value: uint(2),
Values: nil,
}
filters := map[string]interface{}{filter.Field: filter}
Expand Down Expand Up @@ -2491,7 +2494,7 @@ components:
filter := &projections.FilterProperty{
Field: "id",
Operator: "gt",
Value: "3",
Value: uint(3),
Values: nil,
}
filters := map[string]interface{}{filter.Field: filter}
Expand Down Expand Up @@ -2611,33 +2614,6 @@ components:
t.Errorf("expected length of results to be %d got %d", 1, len(results))
}
})
t.Run("testing invalid date time format on filter ", func(t *testing.T) {
page := 1
limit := 0
sortOptions := map[string]string{
"id": "asc",
}
ctxt := context.Background()
filter := &projections.FilterProperty{
Field: "last_updated",
Operator: "lt",
Value: "2006-01-02T15:04:00Z+dsujhsd",
Values: nil,
}

filters := map[string]interface{}{filter.Field: filter}
results, total, err := p.GetContentEntities(ctxt, blogEntityFactory, page, limit, "", sortOptions, filters)
if err == nil {
t.Fatalf("expected a date time error but got nil")
}
if results != nil {
t.Errorf("unexpect error expected results to be nil ")
}
if total != int64(0) {
t.Errorf("expecter total to be 0 got %d", total)
}

})
}

func TestProjections_Delete(t *testing.T) {
Expand Down