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

trivial. fix filters #393

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require (
github.com/gorilla/handlers v1.5.2
github.com/gorilla/mux v1.8.1
github.com/gorilla/websocket v1.5.1
github.com/iancoleman/strcase v0.3.0
github.com/open-policy-agent/opa v0.62.1
github.com/opentracing/opentracing-go v1.2.0
github.com/patrickmn/go-cache v2.1.0+incompatible
Expand All @@ -41,7 +42,6 @@ require (
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
golang.org/x/net v0.22.0
golang.org/x/oauth2 v0.17.0
golang.org/x/text v0.14.0
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
gopkg.in/yaml.v3 v3.0.1
gorm.io/datatypes v1.2.0
Expand Down Expand Up @@ -130,6 +130,7 @@ require (
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/term v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.15.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZH
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/imdario/mergo v0.3.15 h1:M8XP7IuFNsqUx6VPK2P9OSmsYsI/YFaGil0uD21V3dM=
github.com/imdario/mergo v0.3.15/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
Expand Down
5 changes: 2 additions & 3 deletions internal/pagination/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import (
"strconv"
"strings"

"github.com/iancoleman/strcase"
filter "github.com/openinfradev/tks-api/internal/filter"
"github.com/openinfradev/tks-api/internal/helper"
"github.com/openinfradev/tks-api/internal/serializer"
"github.com/openinfradev/tks-api/pkg/domain"
"github.com/openinfradev/tks-api/pkg/log"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"gorm.io/gorm"

"goyave.dev/goyave/v4"
Expand Down Expand Up @@ -210,7 +209,7 @@ func NewPagination(urlParams *url.Values) *Pagination {
releation := ""
arrColumns := strings.Split(column, ".")
if len(arrColumns) > 1 {
releation = cases.Title(language.English, cases.Compact).String(arrColumns[0])
releation = strcase.ToCamel(arrColumns[0])
column = arrColumns[1]
}

Expand Down
13 changes: 12 additions & 1 deletion internal/repository/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,18 @@ func (r *OrganizationRepository) Fetch(ctx context.Context, pg *pagination.Pagin
pg = pagination.NewPagination(nil)
}

_, res := pg.Fetch(r.db.WithContext(ctx).Preload(clause.Associations), &out)
db := r.db.WithContext(ctx).Preload(clause.Associations).Model(&model.Organization{})

// [TODO] more pretty!
for _, filter := range pg.Filters {
if filter.Relation == "Admin" {
db = db.Joins("left outer join users on users.id::text = organizations.admin_id::text").
Where("users.name like ?", "%"+filter.Values[0]+"%")
break
}
}

_, res := pg.Fetch(db, &out)
if res.Error != nil {
return nil, res.Error
}
Expand Down
2 changes: 1 addition & 1 deletion internal/repository/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (r *PolicyRepository) Fetch(ctx context.Context, organizationId string, pg
}

_, res := pg.Fetch(r.db.WithContext(ctx).Preload(clause.Associations).
Where("organization_id = ?", organizationId), &out)
Where("policies.organization_id = ?", organizationId), &out)

if res.Error != nil {
return nil, res.Error
Expand Down
18 changes: 14 additions & 4 deletions internal/repository/system-notification-rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,20 @@ func (r *SystemNotificationRuleRepository) FetchWithOrganization(ctx context.Con
pg = pagination.NewPagination(nil)
}

_, res := pg.Fetch(
r.db.WithContext(ctx).Preload(clause.Associations).
Where("organization_id = ?", organizationId),
&out)
db := r.db.WithContext(ctx).Preload(clause.Associations).Model(&model.SystemNotificationRule{}).
Where("system_notification_rules.organization_id = ?", organizationId)

// [TODO] more pretty!
for _, filter := range pg.Filters {
if filter.Relation == "TargetUsers" {
db = db.Joins("left outer join system_notification_rule_users on system_notification_rules.id = system_notification_rule_users.system_notification_rule_id").
Joins("left outer join users on system_notification_rule_users.user_id = users.id").
Where("users.name like ?", "%"+filter.Values[0]+"%")
break
}
}

_, res := pg.Fetch(db, &out)
if res.Error != nil {
return nil, res.Error
}
Expand Down
1 change: 0 additions & 1 deletion internal/repository/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ func (r *UserRepository) ListWithPagination(ctx context.Context, pg *pagination.

// [TODO] more pretty!
for _, filter := range pg.Filters {
log.Info(ctx, "KTKFREE ", filter.Relation)
if filter.Relation == "Roles" {
db = db.Where("id IN (SELECT user_id FROM user_roles WHERE name IN ?)", filter.Values)
break
Expand Down
Loading