Go-TestAPI/types/responses.go

52 lines
1.1 KiB
Go
Raw Normal View History

2024-06-07 12:52:09 +00:00
package types
import (
"github.com/go-playground/validator/v10"
"reflect"
"strconv"
)
type ManufacturerResponse struct {
ID string
Name string
}
type ProductResponse struct {
ID uint
Name string
Price uint
ManufacturerID uint
Manufacturer ManufacturerResponse
}
type TokenResponse struct {
Token string
}
type ErrorResponse struct {
Message string `json:"error"`
}
type ValidationErrorResponse struct {
Errors validator.ValidationErrors `json:"error"`
}
func NotFoundError(obj string, id interface{}) ErrorResponse {
var idStr string
switch reflect.TypeOf(id).Kind() {
case reflect.String:
idStr = id.(string)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
idStr = strconv.FormatInt(reflect.ValueOf(id).Int(), 10)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
idStr = strconv.FormatUint(reflect.ValueOf(id).Uint(), 10)
default:
idStr = ""
}
return ErrorResponse{Message: obj + " with id " + idStr + " not found"}
}
type MessageResponse struct {
Message string `json:"message"`
}