1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-26 13:48:53 +08:00
Mainflux.mainflux/users/api/responses.go
Manuel Imperiale 9972d1d1a4
MF-1240 - Return to service transport layer only service errors (#1559)
* MF-1240 - Return to service transport layer only service errors

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Remove unecessary errors

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Rm duplicated errors and fix transport

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Revert http endpoint_test

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix conflict

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

Co-authored-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>
2022-02-14 22:49:23 +01:00

234 lines
4.4 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package api
import (
"fmt"
"net/http"
"github.com/mainflux/mainflux"
)
var (
_ mainflux.Response = (*tokenRes)(nil)
_ mainflux.Response = (*viewUserRes)(nil)
_ mainflux.Response = (*passwChangeRes)(nil)
_ mainflux.Response = (*updateGroupRes)(nil)
_ mainflux.Response = (*viewGroupRes)(nil)
_ mainflux.Response = (*createGroupRes)(nil)
_ mainflux.Response = (*createUserRes)(nil)
_ mainflux.Response = (*deleteRes)(nil)
)
// MailSent message response when link is sent
const MailSent = "Email with reset link is sent"
type pageRes struct {
Total uint64 `json:"total"`
Offset uint64 `json:"offset"`
Limit uint64 `json:"limit"`
}
type createUserRes struct {
ID string
created bool
}
func (res createUserRes) Code() int {
if res.created {
return http.StatusCreated
}
return http.StatusOK
}
func (res createUserRes) Headers() map[string]string {
if res.created {
return map[string]string{
"Location": fmt.Sprintf("/users/%s", res.ID),
}
}
return map[string]string{}
}
func (res createUserRes) Empty() bool {
return true
}
type tokenRes struct {
Token string `json:"token,omitempty"`
}
func (res tokenRes) Code() int {
return http.StatusCreated
}
func (res tokenRes) Headers() map[string]string {
return map[string]string{}
}
func (res tokenRes) Empty() bool {
return res.Token == ""
}
type updateUserRes struct{}
func (res updateUserRes) Code() int {
return http.StatusOK
}
func (res updateUserRes) Headers() map[string]string {
return map[string]string{}
}
func (res updateUserRes) Empty() bool {
return true
}
type viewUserRes struct {
ID string `json:"id"`
Email string `json:"email"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
func (res viewUserRes) Code() int {
return http.StatusOK
}
func (res viewUserRes) Headers() map[string]string {
return map[string]string{}
}
func (res viewUserRes) Empty() bool {
return false
}
type userPageRes struct {
pageRes
Users []viewUserRes `json:"users"`
}
func (res userPageRes) Code() int {
return http.StatusOK
}
func (res userPageRes) Headers() map[string]string {
return map[string]string{}
}
func (res userPageRes) Empty() bool {
return false
}
type createGroupRes struct {
ID string `json:"id"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
ParentID string `json:"parent_id"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
created bool
}
func (res createGroupRes) Code() int {
if res.created {
return http.StatusCreated
}
return http.StatusOK
}
func (res createGroupRes) Headers() map[string]string {
if res.created {
return map[string]string{
"Location": fmt.Sprintf("/groups/%s", res.ID),
}
}
return map[string]string{}
}
func (res createGroupRes) Empty() bool {
return true
}
type updateGroupRes struct{}
func (res updateGroupRes) Code() int {
return http.StatusOK
}
func (res updateGroupRes) Headers() map[string]string {
return map[string]string{}
}
func (res updateGroupRes) Empty() bool {
return true
}
type viewGroupRes struct {
ID string `json:"id"`
Name string `json:"name"`
ParentID string `json:"parent_id"`
OwnerID string `json:"owner_id"`
Description string `json:"description"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
func (res viewGroupRes) Code() int {
return http.StatusOK
}
func (res viewGroupRes) Headers() map[string]string {
return map[string]string{}
}
func (res viewGroupRes) Empty() bool {
return false
}
type passwResetReqRes struct {
Msg string `json:"msg"`
}
func (res passwResetReqRes) Code() int {
return http.StatusCreated
}
func (res passwResetReqRes) Headers() map[string]string {
return map[string]string{}
}
func (res passwResetReqRes) Empty() bool {
return false
}
type passwChangeRes struct {
}
func (res passwChangeRes) Code() int {
return http.StatusCreated
}
func (res passwChangeRes) Headers() map[string]string {
return map[string]string{}
}
func (res passwChangeRes) Empty() bool {
return false
}
type deleteRes struct{}
func (res deleteRes) Code() int {
return http.StatusNoContent
}
func (res deleteRes) Headers() map[string]string {
return map[string]string{}
}
func (res deleteRes) Empty() bool {
return true
}