more optimization

This commit is contained in:
konrad 2018-07-11 01:45:11 +02:00 committed by kolaente
parent e97fa30202
commit b067425dc3
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
7 changed files with 171 additions and 129 deletions

1
.gitignore vendored
View file

@ -1,5 +1,4 @@
.idea/ .idea/
config.ini config.ini
*.db *.db
list
Run Run

40
routes/crud/create.go Normal file
View file

@ -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(&currentUser)
if err != nil {
fmt.Println(err)
return echo.NewHTTPError(http.StatusInternalServerError)
}
return ctx.JSON(http.StatusOK, c.CObject)
}

37
routes/crud/delete.go Normal file
View file

@ -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."})
}

View file

@ -1,10 +1,7 @@
package crud package crud
import ( import (
"fmt"
"git.kolaente.de/konrad/list/models" "git.kolaente.de/konrad/list/models"
"github.com/labstack/echo"
"net/http"
) )
// WebHandler defines the webhandler object // WebHandler defines the webhandler object
@ -15,128 +12,3 @@ type WebHandler struct {
models.Rights 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(&currentUser)
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, &currentUser)
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(&currentUser)
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."})
}

24
routes/crud/read_all.go Normal file
View file

@ -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(&currentUser)
if err != nil {
fmt.Println(err)
return ctx.JSON(http.StatusInternalServerError, models.Message{"Could not get."})
}
return ctx.JSON(http.StatusOK, lists)
}

31
routes/crud/read_one.go Normal file
View file

@ -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)
}

39
routes/crud/update.go Normal file
View file

@ -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, &currentUser)
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)
}