Add /user endpoint (#1)

This commit is contained in:
JonasFranz 2018-09-17 16:25:05 +00:00 committed by Gitea
parent 42bd69321a
commit 26d37bbedf
3 changed files with 16 additions and 25 deletions

View file

@ -58,11 +58,11 @@ func userAddOrUpdate(c echo.Context) error {
}
// Check if the user exists
var exists bool
var exists = true
_, err := models.GetUserByID(datUser.ID)
if err != nil {
if models.IsErrUserDoesNotExist(err) {
exists = true
exists = false
} else {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not check if the user exists."})
}

View file

@ -4,29 +4,11 @@ import (
"code.vikunja.io/api/models"
"github.com/labstack/echo"
"net/http"
"strconv"
)
// UserShow gets all informations about a user
// UserShow gets all information about a user
func UserShow(c echo.Context) error {
// TODO: only allow users to show itself/with privacy options
user := c.Param("id")
if user == "" {
return c.JSON(http.StatusBadRequest, models.Message{"User ID cannot be empty."})
}
// Make int
userID, err := strconv.ParseInt(user, 10, 64)
if err != nil {
return c.JSON(http.StatusBadRequest, models.Message{"User ID is invalid."})
}
// Get User Infos
userInfos, err := models.GetUserByID(userID)
userInfos, err := models.GetCurrentUser(c)
if err != nil {
if models.IsErrUserDoesNotExist(err) {
return c.JSON(http.StatusNotFound, models.Message{"The user does not exist."})
@ -34,8 +16,15 @@ func UserShow(c echo.Context) error {
return c.JSON(http.StatusInternalServerError, models.Message{"Error getting user infos."})
}
// Obfucate his password
userInfos.Password = ""
user, err := models.GetUserByID(userInfos.ID)
if err != nil {
if models.IsErrUserDoesNotExist(err) {
return c.JSON(http.StatusNotFound, models.Message{"The user does not exist."})
}
return c.JSON(http.StatusInternalServerError, models.Message{"Error getting user infos."})
}
// Obfuscate his password
user.Password = ""
return c.JSON(http.StatusOK, userInfos)
return c.JSON(http.StatusOK, user)
}

View file

@ -139,4 +139,6 @@ func RegisterRoutes(e *echo.Echo) {
}
a.PUT("/teams/:team/members", teamMemberHandler.CreateWeb)
a.DELETE("/teams/:team/members/:user", teamMemberHandler.DeleteWeb)
a.GET("/user", apiv1.UserShow)
}