1
0

Replaced CObject with a function returning an object

This commit is contained in:
kolaente
2018-10-11 17:53:59 +02:00
parent a612037ee1
commit d8f6a628db
8 changed files with 66 additions and 39 deletions

View File

@ -4,17 +4,15 @@ import (
"code.vikunja.io/api/models"
"github.com/labstack/echo"
"net/http"
"reflect"
)
// CreateWeb is the handler to create an object
func (c *WebHandler) CreateWeb(ctx echo.Context) error {
// Re-initialize our model
p := reflect.ValueOf(c.CObject).Elem()
p.Set(reflect.Zero(p.Type()))
// Get our model
currentStruct := c.EmptyStruct()
// Get the object & bind params to struct
if err := ParamBinder(c.CObject, ctx); err != nil {
if err := ParamBinder(currentStruct, ctx); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "No or invalid model provided.")
}
@ -25,16 +23,16 @@ func (c *WebHandler) CreateWeb(ctx echo.Context) error {
}
// Check rights
if !c.CObject.CanCreate(&currentUser) {
if !currentStruct.CanCreate(&currentUser) {
models.Log.Noticef("%s [ID: %d] tried to create while not having the rights for it", currentUser.Username, currentUser.ID)
return echo.NewHTTPError(http.StatusForbidden)
}
// Create
err = c.CObject.Create(&currentUser)
err = currentStruct.Create(&currentUser)
if err != nil {
return HandleHTTPError(err)
}
return ctx.JSON(http.StatusCreated, c.CObject)
return ctx.JSON(http.StatusCreated, currentStruct)
}

View File

@ -8,8 +8,12 @@ import (
// DeleteWeb is the web handler to delete something
func (c *WebHandler) DeleteWeb(ctx echo.Context) error {
// Get our model
currentStruct := c.EmptyStruct()
// Bind params to struct
if err := ParamBinder(c.CObject, ctx); err != nil {
if err := ParamBinder(currentStruct, ctx); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid URL param.")
}
@ -18,12 +22,12 @@ func (c *WebHandler) DeleteWeb(ctx echo.Context) error {
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError)
}
if !c.CObject.CanDelete(&user) {
if !currentStruct.CanDelete(&user) {
models.Log.Noticef("%s [ID: %d] tried to delete while not having the rights for it", user.Username, user.ID)
return echo.NewHTTPError(http.StatusForbidden)
}
err = c.CObject.Delete()
err = currentStruct.Delete()
if err != nil {
return HandleHTTPError(err)
}

View File

@ -9,10 +9,13 @@ import (
// WebHandler defines the webhandler object
// This does web stuff, aka returns json etc. Uses CRUDable Methods to get the data
type WebHandler struct {
CObject interface {
models.CRUDable
models.Rights
}
EmptyStruct func() CObject
}
// CObject is the definition of our object, holds the structs
type CObject interface {
models.CRUDable
models.Rights
}
// HandleHTTPError does what it says

View File

@ -8,17 +8,20 @@ import (
// ReadAllWeb is the webhandler to get all objects of a type
func (c *WebHandler) ReadAllWeb(ctx echo.Context) error {
// Get our model
currentStruct := c.EmptyStruct()
currentUser, err := models.GetCurrentUser(ctx)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Could not determine the current user.")
}
// Get the object & bind params to struct
if err := ParamBinder(c.CObject, ctx); err != nil {
if err := ParamBinder(currentStruct, ctx); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "No or invalid model provided.")
}
lists, err := c.CObject.ReadAll(&currentUser)
lists, err := currentStruct.ReadAll(&currentUser)
if err != nil {
return HandleHTTPError(err)
}

View File

@ -8,14 +8,16 @@ import (
// ReadOneWeb is the webhandler to get one object
func (c *WebHandler) ReadOneWeb(ctx echo.Context) error {
// Get our model
currentStruct := c.EmptyStruct()
// Get the object & bind params to struct
if err := ParamBinder(c.CObject, ctx); err != nil {
if err := ParamBinder(currentStruct, ctx); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "No or invalid model provided.")
}
// Get our object
err := c.CObject.ReadOne()
err := currentStruct.ReadOne()
if err != nil {
return HandleHTTPError(err)
}
@ -26,10 +28,10 @@ func (c *WebHandler) ReadOneWeb(ctx echo.Context) error {
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Could not determine the current user.")
}
if !c.CObject.CanRead(&currentUser) {
if !currentStruct.CanRead(&currentUser) {
models.Log.Noticef("%s [ID: %d] tried to read while not having the rights for it", currentUser.Username, currentUser.ID)
return echo.NewHTTPError(http.StatusForbidden, "You don't have the right to see this")
}
return ctx.JSON(http.StatusOK, c.CObject)
return ctx.JSON(http.StatusOK, currentStruct)
}

View File

@ -4,17 +4,16 @@ import (
"code.vikunja.io/api/models"
"github.com/labstack/echo"
"net/http"
"reflect"
)
// UpdateWeb is the webhandler to update an object
func (c *WebHandler) UpdateWeb(ctx echo.Context) error {
// Re-initialize our model
p := reflect.ValueOf(c.CObject).Elem()
p.Set(reflect.Zero(p.Type()))
// Get our model
currentStruct := c.EmptyStruct()
// Get the object & bind params to struct
if err := ParamBinder(c.CObject, ctx); err != nil {
if err := ParamBinder(currentStruct, ctx); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "No or invalid model provided.")
}
@ -23,16 +22,16 @@ func (c *WebHandler) UpdateWeb(ctx echo.Context) error {
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Could not determine the current user.")
}
if !c.CObject.CanUpdate(&currentUser) {
if !currentStruct.CanUpdate(&currentUser) {
models.Log.Noticef("%s [ID: %d] tried to update while not having the rights for it", currentUser.Username, currentUser.ID)
return echo.NewHTTPError(http.StatusForbidden)
}
// Do the update
err = c.CObject.Update()
err = currentStruct.Update()
if err != nil {
return HandleHTTPError(err)
}
return ctx.JSON(http.StatusOK, c.CObject)
return ctx.JSON(http.StatusOK, currentStruct)
}