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 all 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"`
}
73 changes: 54 additions & 19 deletions projections/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"gorm.io/gorm"
"gorm.io/gorm/clause"
"strings"
"time"
)

//GORMProjection interface struct
Expand Down Expand Up @@ -249,6 +250,10 @@ func (p *GORMProjection) GetContentEntities(ctx context.Context, entityFactory w
var filtersProp map[string]FilterProperty
props, _ := json.Marshal(filterOptions)
json.Unmarshal(props, &filtersProp)
filtersProp, err := DateTimeCheck(entityFactory, filtersProp)
if err != nil {
return nil, int64(0), err
}
builder := entityFactory.DynamicStruct(ctx)
if builder != nil {
schemes = builder.NewSliceOfStructs()
Expand All @@ -264,7 +269,24 @@ func (p *GORMProjection) GetContentEntities(ctx context.Context, entityFactory w
return entities, count, result.Error
}

//paginate to query results
//DateTimeChecks checks to make sure the format is correctly as well as it manipulates the date
func DateTimeCheck(entityFactory weos.EntityFactory, properties map[string]FilterProperty) (map[string]FilterProperty, error) {
var err error
schema := entityFactory.Schema()
for key, value := range properties {
if schema.Properties[key] != nil && schema.Properties[key].Value.Format == "date-time" {
_, err := time.Parse(time.RFC3339, value.Value.(string))
if err != nil {
return nil, err
}

}
}

return properties, err
}

//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 +301,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 +333,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
15 changes: 9 additions & 6 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