1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-29 13:49:28 +08:00
Aleksandar Novaković ea1993665d MF-483 - Enable channels and devices corresponding lists in backend (#520)
* Add list channels by thing id endpoint

Add list channels by thing id endpoint to things service. Add
pagination format and logic.

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Add fetch channels by thing endpoint

Add fetch channels by thing endpoint with pagination.

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Update list endpoints to contain pagination

Update list endpoints to contain pagination metadata.

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Add tests for two new endpoints

Add tests for two new endpoints and update existing ones. Also,
remove connected field from channel response.

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Fix tests for SDK

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Add SDK methods for new endpoints

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Update swagger docs for things service

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Add error handling to http tests

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Fix response body in http tests

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Remove unused responses from things service

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Add test cases to things postgres tests

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>

* Add test cases for event sourcing

Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2019-01-08 11:53:24 +01:00

224 lines
3.9 KiB
Go

//
// Copyright (c) 2018
// Mainflux
//
// SPDX-License-Identifier: Apache-2.0
//
package http
import (
"fmt"
"net/http"
"github.com/mainflux/mainflux"
)
var (
_ mainflux.Response = (*identityRes)(nil)
_ mainflux.Response = (*removeRes)(nil)
_ mainflux.Response = (*thingRes)(nil)
_ mainflux.Response = (*viewThingRes)(nil)
_ mainflux.Response = (*thingsPageRes)(nil)
_ mainflux.Response = (*channelRes)(nil)
_ mainflux.Response = (*viewChannelRes)(nil)
_ mainflux.Response = (*channelsPageRes)(nil)
_ mainflux.Response = (*connectionRes)(nil)
_ mainflux.Response = (*disconnectionRes)(nil)
)
type identityRes struct {
id uint64
}
func (res identityRes) Headers() map[string]string {
return map[string]string{
"X-thing-id": fmt.Sprint(res.id),
}
}
func (res identityRes) Code() int {
return http.StatusOK
}
func (res identityRes) Empty() bool {
return true
}
type removeRes struct{}
func (res removeRes) Code() int {
return http.StatusNoContent
}
func (res removeRes) Headers() map[string]string {
return map[string]string{}
}
func (res removeRes) Empty() bool {
return true
}
type thingRes struct {
id string
created bool
}
func (res thingRes) Code() int {
if res.created {
return http.StatusCreated
}
return http.StatusOK
}
func (res thingRes) Headers() map[string]string {
if res.created {
return map[string]string{
"Location": fmt.Sprintf("/things/%s", res.id),
}
}
return map[string]string{}
}
func (res thingRes) Empty() bool {
return true
}
type viewThingRes struct {
ID string `json:"id"`
Owner string `json:"-"`
Type string `json:"type"`
Name string `json:"name,omitempty"`
Key string `json:"key"`
Metadata string `json:"metadata,omitempty"`
}
func (res viewThingRes) Code() int {
return http.StatusOK
}
func (res viewThingRes) Headers() map[string]string {
return map[string]string{}
}
func (res viewThingRes) Empty() bool {
return false
}
type thingsPageRes struct {
pageRes
Things []viewThingRes `json:"things"`
}
func (res thingsPageRes) Code() int {
return http.StatusOK
}
func (res thingsPageRes) Headers() map[string]string {
return map[string]string{}
}
func (res thingsPageRes) Empty() bool {
return false
}
type channelRes struct {
id string
created bool
}
func (res channelRes) Code() int {
if res.created {
return http.StatusCreated
}
return http.StatusOK
}
func (res channelRes) Headers() map[string]string {
if res.created {
return map[string]string{
"Location": fmt.Sprintf("/channels/%s", res.id),
}
}
return map[string]string{}
}
func (res channelRes) Empty() bool {
return true
}
type viewChannelRes struct {
ID string `json:"id"`
Owner string `json:"-"`
Name string `json:"name,omitempty"`
Things []viewThingRes `json:"connected,omitempty"`
Metadata string `json:"metadata,omitempty"`
}
func (res viewChannelRes) Code() int {
return http.StatusOK
}
func (res viewChannelRes) Headers() map[string]string {
return map[string]string{}
}
func (res viewChannelRes) Empty() bool {
return false
}
type channelsPageRes struct {
pageRes
Channels []viewChannelRes `json:"channels"`
}
func (res channelsPageRes) Code() int {
return http.StatusOK
}
func (res channelsPageRes) Headers() map[string]string {
return map[string]string{}
}
func (res channelsPageRes) Empty() bool {
return false
}
type connectionRes struct{}
func (res connectionRes) Code() int {
return http.StatusOK
}
func (res connectionRes) Headers() map[string]string {
return map[string]string{}
}
func (res connectionRes) Empty() bool {
return true
}
type disconnectionRes struct{}
func (res disconnectionRes) Code() int {
return http.StatusNoContent
}
func (res disconnectionRes) Headers() map[string]string {
return map[string]string{}
}
func (res disconnectionRes) Empty() bool {
return true
}
type pageRes struct {
Total uint64 `json:"total"`
Offset uint64 `json:"offset"`
Limit uint64 `json:"limit"`
}