api/routes/crud/read_one.go

50 lines
1.3 KiB
Go
Raw Normal View History

2018-07-10 23:45:11 +00:00
package crud
import (
2018-07-25 14:24:46 +00:00
"code.vikunja.io/api/models"
2018-07-25 15:30:29 +00:00
"fmt"
"github.com/labstack/echo"
2018-07-10 23:45:11 +00:00
"net/http"
)
// ReadOneWeb is the webhandler to get one object
func (c *WebHandler) ReadOneWeb(ctx echo.Context) error {
2018-07-21 13:08:46 +00:00
// Get the object & bind params to struct
if err := ParamBinder(c.CObject, ctx); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "No or invalid model provided.")
2018-07-10 23:45:11 +00:00
}
// Get our object
2018-07-21 13:08:46 +00:00
err := c.CObject.ReadOne()
2018-07-10 23:45:11 +00:00
if err != nil {
if models.IsErrListDoesNotExist(err) {
2018-07-11 12:27:16 +00:00
return echo.NewHTTPError(http.StatusNotFound)
2018-07-10 23:45:11 +00:00
}
if models.IsErrNamespaceDoesNotExist(err) {
return echo.NewHTTPError(http.StatusNotFound)
}
2018-07-21 13:08:46 +00:00
if models.IsErrTeamDoesNotExist(err) {
return echo.NewHTTPError(http.StatusNotFound)
}
fmt.Println(err)
2018-07-11 12:27:16 +00:00
return echo.NewHTTPError(http.StatusInternalServerError, "An error occured.")
2018-07-10 23:45:11 +00:00
}
2018-07-12 11:43:42 +00:00
// Check rights
// We can only check the rights on a full object, which is why we need to check it afterwards
currentUser, err := models.GetCurrentUser(ctx)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Could not determine the current user.")
}
if !c.CObject.CanRead(&currentUser) {
return echo.NewHTTPError(http.StatusForbidden, "You don't have the right to see this")
}
2018-07-10 23:45:11 +00:00
return ctx.JSON(http.StatusOK, c.CObject)
}