diff --git a/api/user.go b/api/user.go index cdb9c7b..7155d8c 100644 --- a/api/user.go +++ b/api/user.go @@ -9,13 +9,6 @@ import ( "github.com/gotify/server/model" ) -type userResponse struct { - ID uint `json:"id"` - Name string `binding:"required" json:"name" query:"name" form:"name"` - Pass string `json:"pass,omitempty" form:"pass" query:"pass"` - Admin bool `json:"admin" form:"admin" query:"admin"` -} - // The UserDatabase interface for encapsulating database access. type UserDatabase interface { GetUsers() []*model.User @@ -35,7 +28,7 @@ type UserAPI struct { func (a *UserAPI) GetUsers(ctx *gin.Context) { users := a.DB.GetUsers() - var resp []*userResponse + var resp []*model.UserExternal for _, user := range users { resp = append(resp, toExternal(user)) } @@ -51,7 +44,7 @@ func (a *UserAPI) GetCurrentUser(ctx *gin.Context) { // CreateUser creates a user func (a *UserAPI) CreateUser(ctx *gin.Context) { - user := userResponse{} + user := model.UserExternal{} if err := ctx.Bind(&user); err == nil { if len(user.Pass) == 0 { ctx.AbortWithError(400, errors.New("password may not be empty")) @@ -110,7 +103,7 @@ func (a *UserAPI) ChangePassword(ctx *gin.Context) { // UpdateUserByID updates and user by id func (a *UserAPI) UpdateUserByID(ctx *gin.Context) { if id, err := toUInt(ctx.Param("id")); err == nil { - var user *userResponse + var user *model.UserExternal if err := ctx.Bind(&user); err == nil { if oldUser := a.DB.GetUserByID(id); oldUser != nil { internal := toInternal(user, oldUser.Pass) @@ -131,7 +124,7 @@ func toUInt(id string) (uint, error) { return uint(parsed), err } -func toInternal(response *userResponse, pw []byte) *model.User { +func toInternal(response *model.UserExternal, pw []byte) *model.User { user := &model.User{ Name: response.Name, Admin: response.Admin, @@ -144,8 +137,8 @@ func toInternal(response *userResponse, pw []byte) *model.User { return user } -func toExternal(internal *model.User) *userResponse { - return &userResponse{ +func toExternal(internal *model.User) *model.UserExternal { + return &model.UserExternal{ Name: internal.Name, Admin: internal.Admin, ID: internal.ID, diff --git a/error/handler.go b/error/handler.go index b475453..89f48a4 100644 --- a/error/handler.go +++ b/error/handler.go @@ -10,14 +10,9 @@ import ( "unicode" "gopkg.in/go-playground/validator.v8" + "github.com/gotify/server/model" ) -type errorWrapper struct { - Error string `json:"error"` - ErrorCode int `json:"errorCode"` - ErrorDescription string `json:"errorDescription"` -} - // Handler creates a gin middleware for handling errors. func Handler() gin.HandlerFunc { return func(c *gin.Context) { @@ -63,5 +58,5 @@ func writeError(ctx *gin.Context, errString string) { if ctx.Writer.Status() != http.StatusOK { status = ctx.Writer.Status() } - ctx.JSON(status, &errorWrapper{Error: http.StatusText(status), ErrorCode: status, ErrorDescription: errString}) + ctx.JSON(status, &model.Error{Error: http.StatusText(status), ErrorCode: status, ErrorDescription: errString}) } diff --git a/error/handler_test.go b/error/handler_test.go index 75d9ad3..24b4971 100644 --- a/error/handler_test.go +++ b/error/handler_test.go @@ -9,6 +9,7 @@ import ( "github.com/gin-gonic/gin" "github.com/stretchr/testify/assert" + "github.com/gotify/server/model" ) func TestDefaultErrorInternal(t *testing.T) { @@ -58,7 +59,7 @@ func TestValidationError(t *testing.T) { assert.NotNil(t, ctx.Bind(&testValidate{})) Handler()(ctx) - err := new(errorWrapper) + err := new(model.Error) json.NewDecoder(rec.Body).Decode(err) assert.Equal(t, 400, rec.Code) assert.Equal(t, "Bad Request", err.Error) diff --git a/error/notfound.go b/error/notfound.go index a11fb33..149bf28 100644 --- a/error/notfound.go +++ b/error/notfound.go @@ -4,12 +4,13 @@ import ( "net/http" "github.com/gin-gonic/gin" + "github.com/gotify/server/model" ) // NotFound creates a gin middleware for handling page not found. func NotFound() gin.HandlerFunc { return func(c *gin.Context) { - c.JSON(http.StatusNotFound, &errorWrapper{ + c.JSON(http.StatusNotFound, &model.Error{ Error: http.StatusText(http.StatusNotFound), ErrorCode: http.StatusNotFound, ErrorDescription: "page not found", diff --git a/model/application.go b/model/application.go index 3349810..b355601 100644 --- a/model/application.go +++ b/model/application.go @@ -1,6 +1,10 @@ package model -// Application holds information about an app which can send notifications. +// Application Model +// +// The Application holds information about an app which can send notifications. +// +// swagger:model Application type Application struct { ID string `gorm:"primary_key;unique_index"` UserID uint `gorm:"index" json:"-"` diff --git a/model/client.go b/model/client.go index 5317645..53a0193 100644 --- a/model/client.go +++ b/model/client.go @@ -1,6 +1,10 @@ package model +// Client Model +// // The Client holds information about a device which can receive notifications (and other stuff). +// +// swagger:model Client type Client struct { ID string `gorm:"primary_key;unique_index"` UserID uint `gorm:"index" json:"-"` diff --git a/model/error.go b/model/error.go new file mode 100644 index 0000000..2060c8e --- /dev/null +++ b/model/error.go @@ -0,0 +1,12 @@ +package model + +// Error Model +// +// The Error contains error relevant information. +// +// swagger:model Error +type Error struct { + Error string `json:"error"` + ErrorCode int `json:"errorCode"` + ErrorDescription string `json:"errorDescription"` +} diff --git a/model/message.go b/model/message.go index 6114c0e..a3e53a5 100644 --- a/model/message.go +++ b/model/message.go @@ -2,7 +2,11 @@ package model import "time" +// Message Model +// // The Message holds information about a message which was sent by an Application. +// +// swagger:model Message type Message struct { ID uint `gorm:"AUTO_INCREMENT;primary_key;index" json:"id"` ApplicationID string `json:"appid"` diff --git a/model/user.go b/model/user.go index 769aef6..e3b756c 100644 --- a/model/user.go +++ b/model/user.go @@ -9,3 +9,15 @@ type User struct { Applications []Application Clients []Client } + +// UserExternal Model +// +// The User holds information about the credentials and other stuff. +// +// swagger:model User +type UserExternal struct { + ID uint `json:"id"` + Name string `binding:"required" json:"name" query:"name" form:"name"` + Pass string `json:"pass,omitempty" form:"pass" query:"pass"` + Admin bool `json:"admin" form:"admin" query:"admin"` +}