1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-29 13:49:28 +08:00
Ivan Milošević 30ba38c919
MF-1357 - Add new endpoint for searching things (#1383)
* Add new enpoint for thing search

Signed-off-by: Ivan Milosevic <iva@blokovi.com>

* Rename endpoint to /search
Use same request as list endpoint

Signed-off-by: Ivan Milosevic <iva@blokovi.com>

* Add optional parameters in body (offset, limit)
Add swagger file

Signed-off-by: Ivan Milosevic <iva@blokovi.com>

* move all parameters into body

Signed-off-by: Ivan Milosevic <iva@blokovi.com>

* fix swagger

Signed-off-by: Ivan Milosevic <iva@blokovi.com>

* fix error description

Signed-off-by: Ivan Milosevic <iva@blokovi.com>

* Add tests

Signed-off-by: Ivan Milosevic <iva@blokovi.com>

* remove dead code
fix tests

Signed-off-by: Ivan Milosevic <iva@blokovi.com>

* remove unused var

Signed-off-by: Ivan Milosevic <iva@blokovi.com>

* fix sdk tests

Signed-off-by: Ivan Milosevic <iva@blokovi.com>

* add url endpoint for search test

Signed-off-by: Ivan Milosevic <iva@blokovi.com>

* description in swagger
fix tracer string
change test offset

Signed-off-by: Ivan Milosevic <iva@blokovi.com>

* rename in tests searchThReq to searchThingReq

Signed-off-by: Ivan Milosevic <iva@blokovi.com>
2021-03-11 10:28:44 +01:00

86 lines
1.8 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package mocks
import (
"fmt"
"sort"
"github.com/mainflux/mainflux/things"
)
// Since mocks will store data in map, and they need to resemble the real
// identifiers as much as possible, a key will be created as combination of
// owner and their own identifiers. This will allow searching either by
// prefix or suffix.
func key(owner string, id string) string {
return fmt.Sprintf("%s-%s", owner, id)
}
func sortThings(pm things.PageMetadata, ths []things.Thing) []things.Thing {
switch pm.Order {
case "name":
if pm.Dir == "asc" {
sort.SliceStable(ths, func(i, j int) bool {
return ths[i].Name < ths[j].Name
})
}
if pm.Dir == "desc" {
sort.SliceStable(ths, func(i, j int) bool {
return ths[i].Name > ths[j].Name
})
}
case "id":
if pm.Dir == "asc" {
sort.SliceStable(ths, func(i, j int) bool {
return ths[i].ID < ths[j].ID
})
}
if pm.Dir == "desc" {
sort.SliceStable(ths, func(i, j int) bool {
return ths[i].ID > ths[j].ID
})
}
default:
sort.SliceStable(ths, func(i, j int) bool {
return ths[i].ID < ths[j].ID
})
}
return ths
}
func sortChannels(pm things.PageMetadata, chs []things.Channel) []things.Channel {
switch pm.Order {
case "name":
if pm.Dir == "asc" {
sort.SliceStable(chs, func(i, j int) bool {
return chs[i].Name < chs[j].Name
})
}
if pm.Dir == "desc" {
sort.SliceStable(chs, func(i, j int) bool {
return chs[i].Name > chs[j].Name
})
}
case "id":
if pm.Dir == "asc" {
sort.SliceStable(chs, func(i, j int) bool {
return chs[i].ID < chs[j].ID
})
}
if pm.Dir == "desc" {
sort.SliceStable(chs, func(i, j int) bool {
return chs[i].ID > chs[j].ID
})
}
default:
sort.SliceStable(chs, func(i, j int) bool {
return chs[i].ID < chs[j].ID
})
}
return chs
}