add support for query nil

Signed-off-by: wang yan <wangyan@vmware.com>
This commit is contained in:
wang yan 2019-01-11 16:19:38 +08:00
parent 6bd6fbd4ad
commit d349c256e8
2 changed files with 20 additions and 7 deletions

View File

@ -45,15 +45,15 @@ func GetRobotByID(id int64) (*models.Robot, error) {
// ListRobots list robots according to the query conditions
func ListRobots(query *models.RobotQuery) ([]*models.Robot, error) {
qs := getRobotQuerySetter(query)
qs := getRobotQuerySetter(query).OrderBy("Name")
if query != nil {
if query.Size > 0 {
qs = qs.Limit(query.Size)
if query.Page > 0 {
qs = qs.Offset((query.Page - 1) * query.Size)
}
}
qs = qs.OrderBy("Name")
}
robots := []*models.Robot{}
_, err := qs.All(&robots)
return robots, err
@ -61,6 +61,11 @@ func ListRobots(query *models.RobotQuery) ([]*models.Robot, error) {
func getRobotQuerySetter(query *models.RobotQuery) orm.QuerySeter {
qs := GetOrmer().QueryTable(&models.Robot{})
if query == nil {
return qs
}
if len(query.Name) > 0 {
if query.FuzzyMatchName {
qs = qs.Filter("Name__icontains", query.Name)

View File

@ -155,3 +155,11 @@ func TestDeleteRobot(t *testing.T) {
assert.Nil(t, robot)
}
func TestListAllRobot(t *testing.T) {
robots, err := ListRobots(nil)
require.Nil(t, err)
assert.Equal(t, 5, len(robots))
}