From b067425dc3001356b8a8928f77bac89c08d01dd5 Mon Sep 17 00:00:00 2001 From: konrad Date: Wed, 11 Jul 2018 01:45:11 +0200 Subject: [PATCH] more optimization --- .gitignore | 1 - routes/crud/create.go | 40 +++++++++++++ routes/crud/delete.go | 37 ++++++++++++ routes/crud/helper.go | 128 ---------------------------------------- routes/crud/read_all.go | 24 ++++++++ routes/crud/read_one.go | 31 ++++++++++ routes/crud/update.go | 39 ++++++++++++ 7 files changed, 171 insertions(+), 129 deletions(-) create mode 100644 routes/crud/create.go create mode 100644 routes/crud/delete.go create mode 100644 routes/crud/read_all.go create mode 100644 routes/crud/read_one.go create mode 100644 routes/crud/update.go diff --git a/.gitignore b/.gitignore index 7a8c2b3609..b1cf3971f1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ .idea/ config.ini *.db -list Run \ No newline at end of file diff --git a/routes/crud/create.go b/routes/crud/create.go new file mode 100644 index 0000000000..9284a91add --- /dev/null +++ b/routes/crud/create.go @@ -0,0 +1,40 @@ +package crud + +import ( + "github.com/labstack/echo" + "net/http" + "fmt" + "git.kolaente.de/konrad/list/models" +) + +// CreateWeb is the handler to create an object +func (c *WebHandler) CreateWeb(ctx echo.Context) error { + // Get the object + if err := ctx.Bind(&c.CObject); err != nil { + return ctx.JSON(http.StatusBadRequest, models.Message{"No model provided."}) + } + + // Get the user to pass for later checks + currentUser, err := models.GetCurrentUser(ctx) + if err != nil { + return echo.NewHTTPError(http.StatusInternalServerError, "Could not determine the current user.") + } + + // Get an ID if we have one + var id int64 = 0 + if ctx.Param("id") != "" { + id, err := models.GetIntURLParam("id", ctx) + if err != nil { + + } + } + + // Create + err = c.CObject.Create(¤tUser) + if err != nil { + fmt.Println(err) + return echo.NewHTTPError(http.StatusInternalServerError) + } + + return ctx.JSON(http.StatusOK, c.CObject) +} \ No newline at end of file diff --git a/routes/crud/delete.go b/routes/crud/delete.go new file mode 100644 index 0000000000..611e8d41ec --- /dev/null +++ b/routes/crud/delete.go @@ -0,0 +1,37 @@ +package crud + +import ( + "github.com/labstack/echo" + "net/http" + "git.kolaente.de/konrad/list/models" +) + +// DeleteWeb is the web handler to delete something +func (c *WebHandler) DeleteWeb(ctx echo.Context) error { + // Get the ID + id, err := models.GetIntURLParam("id", ctx) + if err != nil { + return ctx.JSON(http.StatusBadRequest, models.Message{"Invalid ID."}) + } + + // Check if the user has the right to delete + user, err := models.GetCurrentUser(ctx) + if err != nil { + return echo.NewHTTPError(http.StatusInternalServerError) + } + + err = c.CObject.Delete(id, &user) + if err != nil { + if models.IsErrNeedToBeListAdmin(err) { + return echo.NewHTTPError(http.StatusForbidden, "You need to be the list admin to delete a list.") + } + + if models.IsErrListDoesNotExist(err) { + return echo.NewHTTPError(http.StatusNotFound, "This list does not exist.") + } + + return echo.NewHTTPError(http.StatusInternalServerError) + } + + return ctx.JSON(http.StatusOK, models.Message{"Successfully deleted."}) +} \ No newline at end of file diff --git a/routes/crud/helper.go b/routes/crud/helper.go index fb122e4540..a307992f30 100644 --- a/routes/crud/helper.go +++ b/routes/crud/helper.go @@ -1,10 +1,7 @@ package crud import ( - "fmt" "git.kolaente.de/konrad/list/models" - "github.com/labstack/echo" - "net/http" ) // WebHandler defines the webhandler object @@ -15,128 +12,3 @@ type WebHandler struct { models.Rights } } - -// ReadOneWeb is the webhandler to get one object -func (c *WebHandler) ReadOneWeb(ctx echo.Context) error { - - // Get the ID - id, err := models.GetIntURLParam("id", ctx) - if err != nil { - return ctx.JSON(http.StatusBadRequest, models.Message{"Invalid ID."}) - } - - // TODO check rights - - // Get our object - err = c.CObject.ReadOne(id) - if err != nil { - if models.IsErrListDoesNotExist(err) { - return ctx.JSON(http.StatusNotFound, models.Message{"Not found."}) - } - - return ctx.JSON(http.StatusInternalServerError, models.Message{"An error occured."}) - } - - return ctx.JSON(http.StatusOK, c.CObject) -} - -// ReadAllWeb is the webhandler to get all objects of a type -func (c *WebHandler) ReadAllWeb(ctx echo.Context) error { - currentUser, err := models.GetCurrentUser(ctx) - if err != nil { - return ctx.JSON(http.StatusInternalServerError, models.Message{"Could not determine the current user."}) - } - - lists, err := c.CObject.ReadAll(¤tUser) - if err != nil { - fmt.Println(err) - return ctx.JSON(http.StatusInternalServerError, models.Message{"Could not get."}) - } - - return ctx.JSON(http.StatusOK, lists) -} - -// UpdateWeb is the webhandler to update an object -func (c *WebHandler) UpdateWeb(ctx echo.Context) error { - // Get the object - if err := ctx.Bind(&c.CObject); err != nil { - return ctx.JSON(http.StatusBadRequest, models.Message{"No model provided."}) - } - - // Get the ID - id, err := models.GetIntURLParam("id", ctx) - if err != nil { - return ctx.JSON(http.StatusBadRequest, models.Message{"Invalid ID."}) - } - - // Check if the user has the right to do that - currentUser, err := models.GetCurrentUser(ctx) - if err != nil { - return ctx.JSON(http.StatusInternalServerError, models.Message{"Could not determine the current user."}) - } - - // Do the update - err = c.CObject.Update(id, ¤tUser) - if err != nil { - if models.IsErrNeedToBeListAdmin(err) { - return echo.NewHTTPError(http.StatusForbidden, "You need to be list admin to do that.") - } - - return echo.NewHTTPError(http.StatusInternalServerError) - } - - return ctx.JSON(http.StatusOK, c.CObject) -} - -// CreateWeb is the handler to create an object -func (c *WebHandler) CreateWeb(ctx echo.Context) error { - // Get the object - if err := ctx.Bind(&c.CObject); err != nil { - return ctx.JSON(http.StatusBadRequest, models.Message{"No model provided."}) - } - - // Get the user to pass for later checks - currentUser, err := models.GetCurrentUser(ctx) - if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError, "Could not determine the current user.") - } - - // Create - err = c.CObject.Create(¤tUser) - if err != nil { - fmt.Println(err) - return echo.NewHTTPError(http.StatusInternalServerError) - } - - return ctx.JSON(http.StatusOK, c.CObject) -} - -// DeleteWeb is the web handler to delete something -func (c *WebHandler) DeleteWeb(ctx echo.Context) error { - // Get the ID - id, err := models.GetIntURLParam("id", ctx) - if err != nil { - return ctx.JSON(http.StatusBadRequest, models.Message{"Invalid ID."}) - } - - // Check if the user has the right to delete - user, err := models.GetCurrentUser(ctx) - if err != nil { - return echo.NewHTTPError(http.StatusInternalServerError) - } - - err = c.CObject.Delete(id, &user) - if err != nil { - if models.IsErrNeedToBeListAdmin(err) { - return echo.NewHTTPError(http.StatusForbidden, "You need to be the list admin to delete a list.") - } - - if models.IsErrListDoesNotExist(err) { - return echo.NewHTTPError(http.StatusNotFound, "This list does not exist.") - } - - return echo.NewHTTPError(http.StatusInternalServerError) - } - - return ctx.JSON(http.StatusOK, models.Message{"Successfully deleted."}) -} diff --git a/routes/crud/read_all.go b/routes/crud/read_all.go new file mode 100644 index 0000000000..36938a0363 --- /dev/null +++ b/routes/crud/read_all.go @@ -0,0 +1,24 @@ +package crud + +import ( + "github.com/labstack/echo" + "net/http" + "fmt" + "git.kolaente.de/konrad/list/models" +) + +// ReadAllWeb is the webhandler to get all objects of a type +func (c *WebHandler) ReadAllWeb(ctx echo.Context) error { + currentUser, err := models.GetCurrentUser(ctx) + if err != nil { + return ctx.JSON(http.StatusInternalServerError, models.Message{"Could not determine the current user."}) + } + + lists, err := c.CObject.ReadAll(¤tUser) + if err != nil { + fmt.Println(err) + return ctx.JSON(http.StatusInternalServerError, models.Message{"Could not get."}) + } + + return ctx.JSON(http.StatusOK, lists) +} \ No newline at end of file diff --git a/routes/crud/read_one.go b/routes/crud/read_one.go new file mode 100644 index 0000000000..38b7d21a24 --- /dev/null +++ b/routes/crud/read_one.go @@ -0,0 +1,31 @@ +package crud + +import ( + "github.com/labstack/echo" + "git.kolaente.de/konrad/list/models" + "net/http" +) + +// ReadOneWeb is the webhandler to get one object +func (c *WebHandler) ReadOneWeb(ctx echo.Context) error { + + // Get the ID + id, err := models.GetIntURLParam("id", ctx) + if err != nil { + return ctx.JSON(http.StatusBadRequest, models.Message{"Invalid ID."}) + } + + // TODO check rights + + // Get our object + err = c.CObject.ReadOne(id) + if err != nil { + if models.IsErrListDoesNotExist(err) { + return ctx.JSON(http.StatusNotFound, models.Message{"Not found."}) + } + + return ctx.JSON(http.StatusInternalServerError, models.Message{"An error occured."}) + } + + return ctx.JSON(http.StatusOK, c.CObject) +} diff --git a/routes/crud/update.go b/routes/crud/update.go new file mode 100644 index 0000000000..9a05cacdf1 --- /dev/null +++ b/routes/crud/update.go @@ -0,0 +1,39 @@ +package crud + +import ( + "github.com/labstack/echo" + "net/http" + "git.kolaente.de/konrad/list/models" +) + +// UpdateWeb is the webhandler to update an object +func (c *WebHandler) UpdateWeb(ctx echo.Context) error { + // Get the object + if err := ctx.Bind(&c.CObject); err != nil { + return ctx.JSON(http.StatusBadRequest, models.Message{"No model provided."}) + } + + // Get the ID + id, err := models.GetIntURLParam("id", ctx) + if err != nil { + return ctx.JSON(http.StatusBadRequest, models.Message{"Invalid ID."}) + } + + // Check if the user has the right to do that + currentUser, err := models.GetCurrentUser(ctx) + if err != nil { + return ctx.JSON(http.StatusInternalServerError, models.Message{"Could not determine the current user."}) + } + + // Do the update + err = c.CObject.Update(id, ¤tUser) + if err != nil { + if models.IsErrNeedToBeListAdmin(err) { + return echo.NewHTTPError(http.StatusForbidden, "You need to be list admin to do that.") + } + + return echo.NewHTTPError(http.StatusInternalServerError) + } + + return ctx.JSON(http.StatusOK, c.CObject) +}