Skip to content

Commit

Permalink
fix issue openpitrix#83
Browse files Browse the repository at this point in the history
  • Loading branch information
huojiao2006 committed Aug 7, 2019
1 parent 765caed commit 87a60a4
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 43 deletions.
17 changes: 11 additions & 6 deletions pkg/db/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func GetChain(tx *gorm.DB) *Chain {
}
}

func (c *Chain) BuildFilterConditions(req Request, tableName string, exclude ...string) *Chain {
func (c *Chain) BuildFilterConditions(req Request, tableName string, andOr string, exclude ...string) *Chain {
for _, field := range structs.Fields(req) {
column := GetFieldName(field)
param := field.Value()
Expand All @@ -101,13 +101,13 @@ func (c *Chain) BuildFilterConditions(req Request, tableName string, exclude ...
}
if column == SearchWordColumnName && stringutil.Contains(models.SearchWordColumnTable, tableName) {
value := getReqValue(param)
c.getSearchFilter(false, tableName, value, exclude...)
c.getSearchFilter(false, tableName, value, andOr, exclude...)
}
}
return c
}

func (c *Chain) BuildFilterConditionsWithPrefix(req Request, tableName string, exclude ...string) *Chain {
func (c *Chain) BuildFilterConditionsWithPrefix(req Request, tableName string, andOr string, exclude ...string) *Chain {
for _, field := range structs.Fields(req) {
column := GetFieldName(field)
param := field.Value()
Expand All @@ -126,7 +126,7 @@ func (c *Chain) BuildFilterConditionsWithPrefix(req Request, tableName string, e

if column == SearchWordColumnName && stringutil.Contains(models.SearchWordColumnTable, tableName) {
value := getReqValue(param)
c.getSearchFilter(true, tableName, value, exclude...)
c.getSearchFilter(true, tableName, value, andOr, exclude...)
}
}
return c
Expand Down Expand Up @@ -167,7 +167,7 @@ func (c *Chain) AddQueryOrderDir(req Request, defaultColumn string) *Chain {
return c
}

func (c *Chain) getSearchFilter(withPrefix bool, tableName string, value interface{}, exclude ...string) {
func (c *Chain) getSearchFilter(withPrefix bool, tableName string, value interface{}, andOr string, exclude ...string) {
var conditions []string
if vs, ok := value.([]string); ok {
for _, v := range vs {
Expand All @@ -194,5 +194,10 @@ func (c *Chain) getSearchFilter(withPrefix bool, tableName string, value interfa
logger.Warnf(nil, "search_word [%+v] is not string", value)
}
condition := strings.Join(conditions, " OR ")
c.DB = c.DB.Where(condition)
if andOr == "or" {
c.DB = c.DB.Or(condition)
} else {
c.DB = c.DB.Where(condition)
}

}
2 changes: 1 addition & 1 deletion pkg/models/query_setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var SearchColumns = map[string][]string{
TaskColTaskId, TaskColNfId, TaskColStatus, TaskColErrorCode,
},
TableAddress: {
AddrColId, AddrColAddress, AddrColNotifyType, AddrColRemarks,
AddrColId, AddrColAddress, AddrColNotifyType, AddrColRemarks, AddrColStatus,
},
TableAddressList: {
AddrLsColId, AddrLsColName, AddrLsColName, AddrLsColExtra,
Expand Down
57 changes: 29 additions & 28 deletions pkg/services/notification/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,36 +295,37 @@ func TestDescribeAddresses(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

var AddressIds []string
AddressIds = append(AddressIds, "addr-4MMNPyVzqMEr")

var AddressListIds []string
AddressListIds = append(AddressListIds, "adl-lW4DmnoJWp98")

var statuses []string
statuses = append(statuses, "active")

var nfTypes []string
nfTypes = append(nfTypes, "email")

var addrs []string
addrs = append(addrs, "[email protected]")

var req = &pb.DescribeAddressesRequest{
AddressId: AddressIds,
AddressListId: AddressListIds,
Address: addrs,
NotifyType: nfTypes,
Status: statuses,
Limit: 20,
Offset: 0,
SearchWord: pbutil.ToProtoString("successful"),
SortKey: pbutil.ToProtoString("status"),
Reverse: pbutil.ToProtoBool(true),
}
//var AddressIds []string
//AddressIds = append(AddressIds, "addr-4MMNPyVzqMEr")
//
//var AddressListIds []string
//AddressListIds = append(AddressListIds, "adl-lW4DmnoJWp98")
//
//var statuses []string
//statuses = append(statuses, "active")
//
//var nfTypes []string
//nfTypes = append(nfTypes, "email")
//
//var addrs []string
//addrs = append(addrs, "[email protected]")
//
//var req = &pb.DescribeAddressesRequest{
// AddressId: AddressIds,
// AddressListId: AddressListIds,
// Address: addrs,
// NotifyType: nfTypes,
// Status: statuses,
// Limit: 20,
// Offset: 0,
// SearchWord: pbutil.ToProtoString("successful"),
// SortKey: pbutil.ToProtoString("status"),
// Reverse: pbutil.ToProtoBool(true),
//}
var req = &pb.DescribeAddressesRequest{SearchWord: pbutil.ToProtoString("adl-Bmwv3o4yyBvo")}
resp, err := s.DescribeAddresses(ctx, req)
if err != nil {
t.Fatalf("TestDescribeAddresses failed[%s]", AddressIds)
t.Fatalf("TestDescribeAddresses failed[%+v]", req)
}
t.Log(nil, "Test Passed, TestDescribeAddresses", resp)

Expand Down
4 changes: 2 additions & 2 deletions pkg/services/notification/resource_control/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func DescribeAddressesWithAddrListId(ctx context.Context, req *pb.DescribeAddres
var count uint64

dbChain = dbChain.
BuildFilterConditionsWithPrefix(req, models.TableAddress).
BuildFilterConditionsWithPrefix(req, models.TableAddressList, models.AddrLsColStatus).
BuildFilterConditionsWithPrefix(req, models.TableAddress, "or").
BuildFilterConditionsWithPrefix(req, models.TableAddressList, "or", models.AddrLsColStatus).
AddQueryOrderDirWithPrefix(models.TableAddress, req, models.AddrColCreateTime)

err := dbChain.
Expand Down
4 changes: 2 additions & 2 deletions pkg/services/notification/resource_control/address_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func DescribeAddressLists(ctx context.Context, req *pb.DescribeAddressListReques

if err := nfdb.GetChain(global.GetInstance().GetDB().Table(models.TableAddressList)).
AddQueryOrderDir(req, models.AddrColCreateTime).
BuildFilterConditions(req, models.TableAddressList).
BuildFilterConditions(req, models.TableAddressList, "and").
Offset(offset).
Limit(limit).
Find(&addressLists).Error; err != nil {
Expand All @@ -81,7 +81,7 @@ func DescribeAddressLists(ctx context.Context, req *pb.DescribeAddressListReques
}

if err := nfdb.GetChain(global.GetInstance().GetDB().Table(models.TableAddressList)).
BuildFilterConditions(req, models.TableAddressList).
BuildFilterConditions(req, models.TableAddressList, "and").
Count(&count).Error; err != nil {
logger.Errorf(ctx, "Describe addresses list count failed, %+v.", err)
return nil, 0, err
Expand Down
4 changes: 2 additions & 2 deletions pkg/services/notification/resource_control/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func DescribeNotifications(ctx context.Context, req *pb.DescribeNotificationsReq
err := nfdb.GetChain(global.GetInstance().GetDB().
Table(models.TableNotification)).
AddQueryOrderDir(req, models.NfColCreateTime).
BuildFilterConditions(req, models.TableNotification).
BuildFilterConditions(req, models.TableNotification, "and").
Offset(offset).
Limit(limit).
Find(&nfs).Error
Expand All @@ -99,7 +99,7 @@ func DescribeNotifications(ctx context.Context, req *pb.DescribeNotificationsReq
}

if err := nfdb.GetChain(global.GetInstance().GetDB().Table(models.TableNotification)).
BuildFilterConditions(req, models.TableNotification).
BuildFilterConditions(req, models.TableNotification, "and").
Count(&count).Error; err != nil {
logger.Errorf(ctx, "Failed to describe notification count, %+v.", err)
return nil, 0, err
Expand Down
4 changes: 2 additions & 2 deletions pkg/services/notification/resource_control/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func DescribeTasks(ctx context.Context, req *pb.DescribeTasksRequest) ([]*models

if err := nfdb.GetChain(global.GetInstance().GetDB().Table(models.TableTask)).
AddQueryOrderDir(req, models.TaskColCreateTime).
BuildFilterConditions(req, models.TableTask).
BuildFilterConditions(req, models.TableTask, "and").
Offset(offset).
Limit(limit).
Find(&tasks).Error; err != nil {
Expand All @@ -101,7 +101,7 @@ func DescribeTasks(ctx context.Context, req *pb.DescribeTasksRequest) ([]*models
}

if err := nfdb.GetChain(global.GetInstance().GetDB().Table(models.TableTask)).
BuildFilterConditions(req, models.TableTask).
BuildFilterConditions(req, models.TableTask, "and").
Count(&count).Error; err != nil {
logger.Errorf(ctx, "Failed to describe task count, %+v", err)
return nil, 0, err
Expand Down

0 comments on commit 87a60a4

Please sign in to comment.