mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-29 13:49:28 +08:00

* Add pagination to clients and channels endpoints Signed-off-by: Dušan Borovčanin <borovcanindusan1@gmail.com> * Refactor code Change method signature and rename Bulk methods back to All. Signed-off-by: Dušan Borovčanin <borovcanindusan1@gmail.com> * Rename transport_test.go to endpoint_test.go Signed-off-by: Dušan Borovčanin <borovcanindusan1@gmail.com> * Fix manager tests to support pagination Signed-off-by: Dušan Borovčanin <borovcanindusan1@gmail.com> * Add default offset and limit support Signed-off-by: Dušan Borovčanin <borovcanindusan1@gmail.com> * Update docs Signed-off-by: Dušan Borovčanin <borovcanindusan1@gmail.com> * Update tests to support pagination - Move maxLimitSize checking to request validation. - Add tests to support pagination. Signed-off-by: Dušan Borovčanin <borovcanindusan1@gmail.com> * Fix handling query params for pagination Signed-off-by: Dušan Borovčanin <borovcanindusan1@gmail.com> * Fix empty result set Return empty results if invalid offset and limit is passed to channel and client repository. Update tests accordingly. Signed-off-by: Dušan Borovčanin <borovcanindusan1@gmail.com> * Update manager API docs Signed-off-by: Dušan Borovčanin <borovcanindusan1@gmail.com> * Fix response to invalid limit query param Signed-off-by: Dušan Borovčanin <borovcanindusan1@gmail.com> * Remove offset and limmit checks in repository methods Signed-off-by: Dušan Borovčanin <borovcanindusan1@gmail.com>
282 lines
6.1 KiB
Go
282 lines
6.1 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/go-kit/kit/endpoint"
|
|
"github.com/mainflux/mainflux/manager"
|
|
)
|
|
|
|
func registrationEndpoint(svc manager.Service) endpoint.Endpoint {
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(userReq)
|
|
|
|
if err := req.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err := svc.Register(req.user)
|
|
return tokenRes{}, err
|
|
}
|
|
}
|
|
|
|
func loginEndpoint(svc manager.Service) endpoint.Endpoint {
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(userReq)
|
|
|
|
if err := req.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
token, err := svc.Login(req.user)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return tokenRes{token}, nil
|
|
}
|
|
}
|
|
|
|
func addClientEndpoint(svc manager.Service) endpoint.Endpoint {
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(addClientReq)
|
|
|
|
if err := req.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
id, err := svc.AddClient(req.key, req.client)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return clientRes{id: id, created: true}, nil
|
|
}
|
|
}
|
|
|
|
func updateClientEndpoint(svc manager.Service) endpoint.Endpoint {
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(updateClientReq)
|
|
|
|
if err := req.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req.client.ID = req.id
|
|
|
|
if err := svc.UpdateClient(req.key, req.client); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return clientRes{id: req.id, created: false}, nil
|
|
}
|
|
}
|
|
|
|
func viewClientEndpoint(svc manager.Service) endpoint.Endpoint {
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(viewResourceReq)
|
|
|
|
if err := req.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
client, err := svc.ViewClient(req.key, req.id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return viewClientRes{client}, nil
|
|
}
|
|
}
|
|
|
|
func listClientsEndpoint(svc manager.Service) endpoint.Endpoint {
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(listResourcesReq)
|
|
|
|
if err := req.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
clients, err := svc.ListClients(req.key, req.offset, req.limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return listClientsRes{clients}, nil
|
|
}
|
|
}
|
|
|
|
func removeClientEndpoint(svc manager.Service) endpoint.Endpoint {
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(viewResourceReq)
|
|
|
|
err := req.validate()
|
|
if err == manager.ErrNotFound {
|
|
return removeRes{}, nil
|
|
}
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err = svc.RemoveClient(req.key, req.id); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return removeRes{}, nil
|
|
}
|
|
}
|
|
|
|
func createChannelEndpoint(svc manager.Service) endpoint.Endpoint {
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(createChannelReq)
|
|
|
|
if err := req.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
id, err := svc.CreateChannel(req.key, req.channel)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return channelRes{id: id, created: true}, nil
|
|
}
|
|
}
|
|
|
|
func updateChannelEndpoint(svc manager.Service) endpoint.Endpoint {
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(updateChannelReq)
|
|
|
|
if err := req.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req.channel.ID = req.id
|
|
|
|
if err := svc.UpdateChannel(req.key, req.channel); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return channelRes{id: req.id, created: false}, nil
|
|
}
|
|
}
|
|
|
|
func viewChannelEndpoint(svc manager.Service) endpoint.Endpoint {
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(viewResourceReq)
|
|
|
|
if err := req.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
channel, err := svc.ViewChannel(req.key, req.id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return viewChannelRes{channel}, nil
|
|
}
|
|
}
|
|
|
|
func listChannelsEndpoint(svc manager.Service) endpoint.Endpoint {
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(listResourcesReq)
|
|
|
|
if err := req.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
channels, err := svc.ListChannels(req.key, req.offset, req.limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return listChannelsRes{channels}, nil
|
|
}
|
|
}
|
|
|
|
func removeChannelEndpoint(svc manager.Service) endpoint.Endpoint {
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(viewResourceReq)
|
|
|
|
if err := req.validate(); err != nil {
|
|
if err == manager.ErrNotFound {
|
|
return removeRes{}, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
if err := svc.RemoveChannel(req.key, req.id); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return removeRes{}, nil
|
|
}
|
|
}
|
|
func connectEndpoint(svc manager.Service) endpoint.Endpoint {
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
cr := request.(connectionReq)
|
|
|
|
if err := cr.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := svc.Connect(cr.key, cr.chanId, cr.clientId); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return connectionRes{}, nil
|
|
}
|
|
}
|
|
|
|
func disconnectEndpoint(svc manager.Service) endpoint.Endpoint {
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
cr := request.(connectionReq)
|
|
|
|
if err := cr.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := svc.Disconnect(cr.key, cr.chanId, cr.clientId); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return disconnectionRes{}, nil
|
|
}
|
|
}
|
|
|
|
func identityEndpoint(svc manager.Service) endpoint.Endpoint {
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(identityReq)
|
|
|
|
if err := req.validate(); err != nil {
|
|
return nil, manager.ErrUnauthorizedAccess
|
|
}
|
|
|
|
id, err := svc.Identity(req.key)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return identityRes{id: id}, nil
|
|
}
|
|
}
|
|
|
|
func canAccessEndpoint(svc manager.Service) endpoint.Endpoint {
|
|
return func(_ context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(viewResourceReq)
|
|
|
|
if err := req.validate(); err != nil {
|
|
return nil, manager.ErrUnauthorizedAccess
|
|
}
|
|
|
|
id, err := svc.CanAccess(req.key, req.id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return identityRes{id: id}, nil
|
|
}
|
|
}
|