Move models and add docu to models

This commit is contained in:
Jannis Mattheis 2018-02-16 21:14:00 +01:00 committed by Jannis Mattheis
parent a5acb20d0e
commit 9c47b73b1d
9 changed files with 49 additions and 23 deletions

View File

@ -9,13 +9,6 @@ import (
"github.com/gotify/server/model" "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. // The UserDatabase interface for encapsulating database access.
type UserDatabase interface { type UserDatabase interface {
GetUsers() []*model.User GetUsers() []*model.User
@ -35,7 +28,7 @@ type UserAPI struct {
func (a *UserAPI) GetUsers(ctx *gin.Context) { func (a *UserAPI) GetUsers(ctx *gin.Context) {
users := a.DB.GetUsers() users := a.DB.GetUsers()
var resp []*userResponse var resp []*model.UserExternal
for _, user := range users { for _, user := range users {
resp = append(resp, toExternal(user)) resp = append(resp, toExternal(user))
} }
@ -51,7 +44,7 @@ func (a *UserAPI) GetCurrentUser(ctx *gin.Context) {
// CreateUser creates a user // CreateUser creates a user
func (a *UserAPI) CreateUser(ctx *gin.Context) { func (a *UserAPI) CreateUser(ctx *gin.Context) {
user := userResponse{} user := model.UserExternal{}
if err := ctx.Bind(&user); err == nil { if err := ctx.Bind(&user); err == nil {
if len(user.Pass) == 0 { if len(user.Pass) == 0 {
ctx.AbortWithError(400, errors.New("password may not be empty")) 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 // UpdateUserByID updates and user by id
func (a *UserAPI) UpdateUserByID(ctx *gin.Context) { func (a *UserAPI) UpdateUserByID(ctx *gin.Context) {
if id, err := toUInt(ctx.Param("id")); err == nil { if id, err := toUInt(ctx.Param("id")); err == nil {
var user *userResponse var user *model.UserExternal
if err := ctx.Bind(&user); err == nil { if err := ctx.Bind(&user); err == nil {
if oldUser := a.DB.GetUserByID(id); oldUser != nil { if oldUser := a.DB.GetUserByID(id); oldUser != nil {
internal := toInternal(user, oldUser.Pass) internal := toInternal(user, oldUser.Pass)
@ -131,7 +124,7 @@ func toUInt(id string) (uint, error) {
return uint(parsed), err return uint(parsed), err
} }
func toInternal(response *userResponse, pw []byte) *model.User { func toInternal(response *model.UserExternal, pw []byte) *model.User {
user := &model.User{ user := &model.User{
Name: response.Name, Name: response.Name,
Admin: response.Admin, Admin: response.Admin,
@ -144,8 +137,8 @@ func toInternal(response *userResponse, pw []byte) *model.User {
return user return user
} }
func toExternal(internal *model.User) *userResponse { func toExternal(internal *model.User) *model.UserExternal {
return &userResponse{ return &model.UserExternal{
Name: internal.Name, Name: internal.Name,
Admin: internal.Admin, Admin: internal.Admin,
ID: internal.ID, ID: internal.ID,

View File

@ -10,14 +10,9 @@ import (
"unicode" "unicode"
"gopkg.in/go-playground/validator.v8" "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. // Handler creates a gin middleware for handling errors.
func Handler() gin.HandlerFunc { func Handler() gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
@ -63,5 +58,5 @@ func writeError(ctx *gin.Context, errString string) {
if ctx.Writer.Status() != http.StatusOK { if ctx.Writer.Status() != http.StatusOK {
status = ctx.Writer.Status() 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})
} }

View File

@ -9,6 +9,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/gotify/server/model"
) )
func TestDefaultErrorInternal(t *testing.T) { func TestDefaultErrorInternal(t *testing.T) {
@ -58,7 +59,7 @@ func TestValidationError(t *testing.T) {
assert.NotNil(t, ctx.Bind(&testValidate{})) assert.NotNil(t, ctx.Bind(&testValidate{}))
Handler()(ctx) Handler()(ctx)
err := new(errorWrapper) err := new(model.Error)
json.NewDecoder(rec.Body).Decode(err) json.NewDecoder(rec.Body).Decode(err)
assert.Equal(t, 400, rec.Code) assert.Equal(t, 400, rec.Code)
assert.Equal(t, "Bad Request", err.Error) assert.Equal(t, "Bad Request", err.Error)

View File

@ -4,12 +4,13 @@ import (
"net/http" "net/http"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/gotify/server/model"
) )
// NotFound creates a gin middleware for handling page not found. // NotFound creates a gin middleware for handling page not found.
func NotFound() gin.HandlerFunc { func NotFound() gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
c.JSON(http.StatusNotFound, &errorWrapper{ c.JSON(http.StatusNotFound, &model.Error{
Error: http.StatusText(http.StatusNotFound), Error: http.StatusText(http.StatusNotFound),
ErrorCode: http.StatusNotFound, ErrorCode: http.StatusNotFound,
ErrorDescription: "page not found", ErrorDescription: "page not found",

View File

@ -1,6 +1,10 @@
package model 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 { type Application struct {
ID string `gorm:"primary_key;unique_index"` ID string `gorm:"primary_key;unique_index"`
UserID uint `gorm:"index" json:"-"` UserID uint `gorm:"index" json:"-"`

View File

@ -1,6 +1,10 @@
package model package model
// Client Model
//
// The Client holds information about a device which can receive notifications (and other stuff). // The Client holds information about a device which can receive notifications (and other stuff).
//
// swagger:model Client
type Client struct { type Client struct {
ID string `gorm:"primary_key;unique_index"` ID string `gorm:"primary_key;unique_index"`
UserID uint `gorm:"index" json:"-"` UserID uint `gorm:"index" json:"-"`

12
model/error.go Normal file
View File

@ -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"`
}

View File

@ -2,7 +2,11 @@ package model
import "time" import "time"
// Message Model
//
// The Message holds information about a message which was sent by an Application. // The Message holds information about a message which was sent by an Application.
//
// swagger:model Message
type Message struct { type Message struct {
ID uint `gorm:"AUTO_INCREMENT;primary_key;index" json:"id"` ID uint `gorm:"AUTO_INCREMENT;primary_key;index" json:"id"`
ApplicationID string `json:"appid"` ApplicationID string `json:"appid"`

View File

@ -9,3 +9,15 @@ type User struct {
Applications []Application Applications []Application
Clients []Client 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"`
}