Replaced CObject with a function returning an object
This commit is contained in:
parent
a612037ee1
commit
d8f6a628db
8 changed files with 66 additions and 39 deletions
|
@ -5,7 +5,7 @@ Authorization: Bearer {{auth_token}}
|
||||||
###
|
###
|
||||||
|
|
||||||
# Get one list
|
# Get one list
|
||||||
GET http://localhost:8080/api/v1/lists/13546850
|
GET http://localhost:8080/api/v1/lists/2
|
||||||
Authorization: Bearer {{auth_token}}
|
Authorization: Bearer {{auth_token}}
|
||||||
|
|
||||||
###
|
###
|
||||||
|
|
|
@ -4,17 +4,15 @@ import (
|
||||||
"code.vikunja.io/api/models"
|
"code.vikunja.io/api/models"
|
||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// CreateWeb is the handler to create an object
|
// CreateWeb is the handler to create an object
|
||||||
func (c *WebHandler) CreateWeb(ctx echo.Context) error {
|
func (c *WebHandler) CreateWeb(ctx echo.Context) error {
|
||||||
// Re-initialize our model
|
// Get our model
|
||||||
p := reflect.ValueOf(c.CObject).Elem()
|
currentStruct := c.EmptyStruct()
|
||||||
p.Set(reflect.Zero(p.Type()))
|
|
||||||
|
|
||||||
// Get the object & bind params to struct
|
// 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.")
|
return echo.NewHTTPError(http.StatusBadRequest, "No or invalid model provided.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,16 +23,16 @@ func (c *WebHandler) CreateWeb(ctx echo.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check rights
|
// Check rights
|
||||||
if !c.CObject.CanCreate(¤tUser) {
|
if !currentStruct.CanCreate(¤tUser) {
|
||||||
models.Log.Noticef("%s [ID: %d] tried to create while not having the rights for it", currentUser.Username, currentUser.ID)
|
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)
|
return echo.NewHTTPError(http.StatusForbidden)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create
|
// Create
|
||||||
err = c.CObject.Create(¤tUser)
|
err = currentStruct.Create(¤tUser)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return HandleHTTPError(err)
|
return HandleHTTPError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return ctx.JSON(http.StatusCreated, c.CObject)
|
return ctx.JSON(http.StatusCreated, currentStruct)
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,8 +8,12 @@ import (
|
||||||
|
|
||||||
// DeleteWeb is the web handler to delete something
|
// DeleteWeb is the web handler to delete something
|
||||||
func (c *WebHandler) DeleteWeb(ctx echo.Context) error {
|
func (c *WebHandler) DeleteWeb(ctx echo.Context) error {
|
||||||
|
|
||||||
|
// Get our model
|
||||||
|
currentStruct := c.EmptyStruct()
|
||||||
|
|
||||||
// Bind params to struct
|
// 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.")
|
return echo.NewHTTPError(http.StatusBadRequest, "Invalid URL param.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,12 +22,12 @@ func (c *WebHandler) DeleteWeb(ctx echo.Context) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return echo.NewHTTPError(http.StatusInternalServerError)
|
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)
|
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)
|
return echo.NewHTTPError(http.StatusForbidden)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = c.CObject.Delete()
|
err = currentStruct.Delete()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return HandleHTTPError(err)
|
return HandleHTTPError(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,10 +9,13 @@ import (
|
||||||
// WebHandler defines the webhandler object
|
// WebHandler defines the webhandler object
|
||||||
// This does web stuff, aka returns json etc. Uses CRUDable Methods to get the data
|
// This does web stuff, aka returns json etc. Uses CRUDable Methods to get the data
|
||||||
type WebHandler struct {
|
type WebHandler struct {
|
||||||
CObject interface {
|
EmptyStruct func() CObject
|
||||||
models.CRUDable
|
}
|
||||||
models.Rights
|
|
||||||
}
|
// CObject is the definition of our object, holds the structs
|
||||||
|
type CObject interface {
|
||||||
|
models.CRUDable
|
||||||
|
models.Rights
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandleHTTPError does what it says
|
// HandleHTTPError does what it says
|
||||||
|
|
|
@ -8,17 +8,20 @@ import (
|
||||||
|
|
||||||
// ReadAllWeb is the webhandler to get all objects of a type
|
// ReadAllWeb is the webhandler to get all objects of a type
|
||||||
func (c *WebHandler) ReadAllWeb(ctx echo.Context) error {
|
func (c *WebHandler) ReadAllWeb(ctx echo.Context) error {
|
||||||
|
// Get our model
|
||||||
|
currentStruct := c.EmptyStruct()
|
||||||
|
|
||||||
currentUser, err := models.GetCurrentUser(ctx)
|
currentUser, err := models.GetCurrentUser(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return echo.NewHTTPError(http.StatusInternalServerError, "Could not determine the current user.")
|
return echo.NewHTTPError(http.StatusInternalServerError, "Could not determine the current user.")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the object & bind params to struct
|
// 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.")
|
return echo.NewHTTPError(http.StatusBadRequest, "No or invalid model provided.")
|
||||||
}
|
}
|
||||||
|
|
||||||
lists, err := c.CObject.ReadAll(¤tUser)
|
lists, err := currentStruct.ReadAll(¤tUser)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return HandleHTTPError(err)
|
return HandleHTTPError(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,14 +8,16 @@ import (
|
||||||
|
|
||||||
// ReadOneWeb is the webhandler to get one object
|
// ReadOneWeb is the webhandler to get one object
|
||||||
func (c *WebHandler) ReadOneWeb(ctx echo.Context) error {
|
func (c *WebHandler) ReadOneWeb(ctx echo.Context) error {
|
||||||
|
// Get our model
|
||||||
|
currentStruct := c.EmptyStruct()
|
||||||
|
|
||||||
// Get the object & bind params to struct
|
// 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.")
|
return echo.NewHTTPError(http.StatusBadRequest, "No or invalid model provided.")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get our object
|
// Get our object
|
||||||
err := c.CObject.ReadOne()
|
err := currentStruct.ReadOne()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return HandleHTTPError(err)
|
return HandleHTTPError(err)
|
||||||
}
|
}
|
||||||
|
@ -26,10 +28,10 @@ func (c *WebHandler) ReadOneWeb(ctx echo.Context) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return echo.NewHTTPError(http.StatusInternalServerError, "Could not determine the current user.")
|
return echo.NewHTTPError(http.StatusInternalServerError, "Could not determine the current user.")
|
||||||
}
|
}
|
||||||
if !c.CObject.CanRead(¤tUser) {
|
if !currentStruct.CanRead(¤tUser) {
|
||||||
models.Log.Noticef("%s [ID: %d] tried to read while not having the rights for it", currentUser.Username, currentUser.ID)
|
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 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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,17 +4,16 @@ import (
|
||||||
"code.vikunja.io/api/models"
|
"code.vikunja.io/api/models"
|
||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// UpdateWeb is the webhandler to update an object
|
// UpdateWeb is the webhandler to update an object
|
||||||
func (c *WebHandler) UpdateWeb(ctx echo.Context) error {
|
func (c *WebHandler) UpdateWeb(ctx echo.Context) error {
|
||||||
// Re-initialize our model
|
|
||||||
p := reflect.ValueOf(c.CObject).Elem()
|
// Get our model
|
||||||
p.Set(reflect.Zero(p.Type()))
|
currentStruct := c.EmptyStruct()
|
||||||
|
|
||||||
// Get the object & bind params to struct
|
// 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.")
|
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 {
|
if err != nil {
|
||||||
return echo.NewHTTPError(http.StatusInternalServerError, "Could not determine the current user.")
|
return echo.NewHTTPError(http.StatusInternalServerError, "Could not determine the current user.")
|
||||||
}
|
}
|
||||||
if !c.CObject.CanUpdate(¤tUser) {
|
if !currentStruct.CanUpdate(¤tUser) {
|
||||||
models.Log.Noticef("%s [ID: %d] tried to update while not having the rights for it", currentUser.Username, currentUser.ID)
|
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)
|
return echo.NewHTTPError(http.StatusForbidden)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do the update
|
// Do the update
|
||||||
err = c.CObject.Update()
|
err = currentStruct.Update()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return HandleHTTPError(err)
|
return HandleHTTPError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return ctx.JSON(http.StatusOK, c.CObject)
|
return ctx.JSON(http.StatusOK, currentStruct)
|
||||||
}
|
}
|
||||||
|
|
|
@ -77,7 +77,9 @@ func RegisterRoutes(e *echo.Echo) {
|
||||||
a.GET("/users", apiv1.UserList)
|
a.GET("/users", apiv1.UserList)
|
||||||
|
|
||||||
listHandler := &crud.WebHandler{
|
listHandler := &crud.WebHandler{
|
||||||
CObject: &models.List{},
|
EmptyStruct: func() crud.CObject {
|
||||||
|
return &models.List{}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
a.GET("/lists", listHandler.ReadAllWeb)
|
a.GET("/lists", listHandler.ReadAllWeb)
|
||||||
a.GET("/lists/:list", listHandler.ReadOneWeb)
|
a.GET("/lists/:list", listHandler.ReadOneWeb)
|
||||||
|
@ -86,14 +88,18 @@ func RegisterRoutes(e *echo.Echo) {
|
||||||
a.PUT("/namespaces/:namespace/lists", listHandler.CreateWeb)
|
a.PUT("/namespaces/:namespace/lists", listHandler.CreateWeb)
|
||||||
|
|
||||||
taskHandler := &crud.WebHandler{
|
taskHandler := &crud.WebHandler{
|
||||||
CObject: &models.ListTask{},
|
EmptyStruct: func() crud.CObject {
|
||||||
|
return &models.ListTask{}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
a.PUT("/lists/:list", taskHandler.CreateWeb)
|
a.PUT("/lists/:list", taskHandler.CreateWeb)
|
||||||
a.DELETE("/tasks/:listtask", taskHandler.DeleteWeb)
|
a.DELETE("/tasks/:listtask", taskHandler.DeleteWeb)
|
||||||
a.POST("/tasks/:listtask", taskHandler.UpdateWeb)
|
a.POST("/tasks/:listtask", taskHandler.UpdateWeb)
|
||||||
|
|
||||||
listTeamHandler := &crud.WebHandler{
|
listTeamHandler := &crud.WebHandler{
|
||||||
CObject: &models.TeamList{},
|
EmptyStruct: func() crud.CObject {
|
||||||
|
return &models.TeamList{}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
a.GET("/lists/:list/teams", listTeamHandler.ReadAllWeb)
|
a.GET("/lists/:list/teams", listTeamHandler.ReadAllWeb)
|
||||||
a.PUT("/lists/:list/teams", listTeamHandler.CreateWeb)
|
a.PUT("/lists/:list/teams", listTeamHandler.CreateWeb)
|
||||||
|
@ -101,7 +107,9 @@ func RegisterRoutes(e *echo.Echo) {
|
||||||
a.POST("/lists/:list/teams/:team", listTeamHandler.UpdateWeb)
|
a.POST("/lists/:list/teams/:team", listTeamHandler.UpdateWeb)
|
||||||
|
|
||||||
listUserHandler := &crud.WebHandler{
|
listUserHandler := &crud.WebHandler{
|
||||||
CObject: &models.ListUser{},
|
EmptyStruct: func() crud.CObject {
|
||||||
|
return &models.ListUser{}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
a.GET("/lists/:list/users", listUserHandler.ReadAllWeb)
|
a.GET("/lists/:list/users", listUserHandler.ReadAllWeb)
|
||||||
a.PUT("/lists/:list/users", listUserHandler.CreateWeb)
|
a.PUT("/lists/:list/users", listUserHandler.CreateWeb)
|
||||||
|
@ -109,7 +117,9 @@ func RegisterRoutes(e *echo.Echo) {
|
||||||
a.POST("/lists/:list/users/:user", listUserHandler.UpdateWeb)
|
a.POST("/lists/:list/users/:user", listUserHandler.UpdateWeb)
|
||||||
|
|
||||||
namespaceHandler := &crud.WebHandler{
|
namespaceHandler := &crud.WebHandler{
|
||||||
CObject: &models.Namespace{},
|
EmptyStruct: func() crud.CObject {
|
||||||
|
return &models.Namespace{}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
a.GET("/namespaces", namespaceHandler.ReadAllWeb)
|
a.GET("/namespaces", namespaceHandler.ReadAllWeb)
|
||||||
a.PUT("/namespaces", namespaceHandler.CreateWeb)
|
a.PUT("/namespaces", namespaceHandler.CreateWeb)
|
||||||
|
@ -119,7 +129,9 @@ func RegisterRoutes(e *echo.Echo) {
|
||||||
a.GET("/namespaces/:namespace/lists", apiv1.GetListsByNamespaceID)
|
a.GET("/namespaces/:namespace/lists", apiv1.GetListsByNamespaceID)
|
||||||
|
|
||||||
namespaceTeamHandler := &crud.WebHandler{
|
namespaceTeamHandler := &crud.WebHandler{
|
||||||
CObject: &models.TeamNamespace{},
|
EmptyStruct: func() crud.CObject {
|
||||||
|
return &models.TeamNamespace{}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
a.GET("/namespaces/:namespace/teams", namespaceTeamHandler.ReadAllWeb)
|
a.GET("/namespaces/:namespace/teams", namespaceTeamHandler.ReadAllWeb)
|
||||||
a.PUT("/namespaces/:namespace/teams", namespaceTeamHandler.CreateWeb)
|
a.PUT("/namespaces/:namespace/teams", namespaceTeamHandler.CreateWeb)
|
||||||
|
@ -127,7 +139,9 @@ func RegisterRoutes(e *echo.Echo) {
|
||||||
a.POST("/namespaces/:namespace/teams/:team", namespaceTeamHandler.UpdateWeb)
|
a.POST("/namespaces/:namespace/teams/:team", namespaceTeamHandler.UpdateWeb)
|
||||||
|
|
||||||
namespaceUserHandler := &crud.WebHandler{
|
namespaceUserHandler := &crud.WebHandler{
|
||||||
CObject: &models.NamespaceUser{},
|
EmptyStruct: func() crud.CObject {
|
||||||
|
return &models.NamespaceUser{}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
a.GET("/namespaces/:namespace/users", namespaceUserHandler.ReadAllWeb)
|
a.GET("/namespaces/:namespace/users", namespaceUserHandler.ReadAllWeb)
|
||||||
a.PUT("/namespaces/:namespace/users", namespaceUserHandler.CreateWeb)
|
a.PUT("/namespaces/:namespace/users", namespaceUserHandler.CreateWeb)
|
||||||
|
@ -135,7 +149,9 @@ func RegisterRoutes(e *echo.Echo) {
|
||||||
a.POST("/namespaces/:namespace/users/:user", namespaceUserHandler.UpdateWeb)
|
a.POST("/namespaces/:namespace/users/:user", namespaceUserHandler.UpdateWeb)
|
||||||
|
|
||||||
teamHandler := &crud.WebHandler{
|
teamHandler := &crud.WebHandler{
|
||||||
CObject: &models.Team{},
|
EmptyStruct: func() crud.CObject {
|
||||||
|
return &models.Team{}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
a.GET("/teams", teamHandler.ReadAllWeb)
|
a.GET("/teams", teamHandler.ReadAllWeb)
|
||||||
a.GET("/teams/:team", teamHandler.ReadOneWeb)
|
a.GET("/teams/:team", teamHandler.ReadOneWeb)
|
||||||
|
@ -144,7 +160,9 @@ func RegisterRoutes(e *echo.Echo) {
|
||||||
a.DELETE("/teams/:team", teamHandler.DeleteWeb)
|
a.DELETE("/teams/:team", teamHandler.DeleteWeb)
|
||||||
|
|
||||||
teamMemberHandler := &crud.WebHandler{
|
teamMemberHandler := &crud.WebHandler{
|
||||||
CObject: &models.TeamMember{},
|
EmptyStruct: func() crud.CObject {
|
||||||
|
return &models.TeamMember{}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
a.PUT("/teams/:team/members", teamMemberHandler.CreateWeb)
|
a.PUT("/teams/:team/members", teamMemberHandler.CreateWeb)
|
||||||
a.DELETE("/teams/:team/members/:user", teamMemberHandler.DeleteWeb)
|
a.DELETE("/teams/:team/members/:user", teamMemberHandler.DeleteWeb)
|
||||||
|
|
Loading…
Reference in a new issue