Also use helper method in user api

This commit is contained in:
Jannis Mattheis 2018-03-10 21:27:24 +01:00 committed by Jannis Mattheis
parent ec1eb50d0e
commit 2d1d68f5ae
1 changed files with 6 additions and 18 deletions

View File

@ -2,7 +2,6 @@ package api
import ( import (
"errors" "errors"
"strconv"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/gotify/server/auth" "github.com/gotify/server/auth"
@ -59,28 +58,24 @@ func (a *UserAPI) CreateUser(ctx *gin.Context) {
// GetUserByID returns the user by id // GetUserByID returns the user by id
func (a *UserAPI) GetUserByID(ctx *gin.Context) { func (a *UserAPI) GetUserByID(ctx *gin.Context) {
if id, err := toUInt(ctx.Param("id")); err == nil { withID(ctx, "id", func(id uint) {
if user := a.DB.GetUserByID(uint(id)); user != nil { if user := a.DB.GetUserByID(uint(id)); user != nil {
ctx.JSON(200, toExternal(user)) ctx.JSON(200, toExternal(user))
} else { } else {
ctx.AbortWithError(404, errors.New("user does not exist")) ctx.AbortWithError(404, errors.New("user does not exist"))
} }
} else { })
ctx.AbortWithError(400, errors.New("invalid id"))
}
} }
// DeleteUserByID deletes the user by id // DeleteUserByID deletes the user by id
func (a *UserAPI) DeleteUserByID(ctx *gin.Context) { func (a *UserAPI) DeleteUserByID(ctx *gin.Context) {
if id, err := toUInt(ctx.Param("id")); err == nil { withID(ctx, "id", func(id uint) {
if user := a.DB.GetUserByID(id); user != nil { if user := a.DB.GetUserByID(id); user != nil {
a.DB.DeleteUserByID(id) a.DB.DeleteUserByID(id)
} else { } else {
ctx.AbortWithError(404, errors.New("user does not exist")) ctx.AbortWithError(404, errors.New("user does not exist"))
} }
} else { })
ctx.AbortWithError(400, errors.New("invalid id"))
}
} }
// ChangePassword changes the password from the current user // ChangePassword changes the password from the current user
@ -95,7 +90,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 { withID(ctx, "id", func(id uint) {
var user *model.UserExternalWithPass var user *model.UserExternalWithPass
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 {
@ -107,14 +102,7 @@ func (a *UserAPI) UpdateUserByID(ctx *gin.Context) {
ctx.AbortWithError(404, errors.New("user does not exist")) ctx.AbortWithError(404, errors.New("user does not exist"))
} }
} }
} else { })
ctx.AbortWithError(400, errors.New("invalid id"))
}
}
func toUInt(id string) (uint, error) {
parsed, err := strconv.ParseUint(id, 10, 32)
return uint(parsed), err
} }
func (a *UserAPI) toInternal(response *model.UserExternalWithPass, pw []byte) *model.User { func (a *UserAPI) toInternal(response *model.UserExternalWithPass, pw []byte) *model.User {