From 490f32d46b4682e3552481456c2e8583e76dc7b4 Mon Sep 17 00:00:00 2001 From: kolaente Date: Sat, 7 Jul 2018 14:19:34 +0200 Subject: [PATCH] Added CRUD helper method --- models/helper.go | 13 ++++++++ models/lists.go | 19 +++++++----- routes/api/v1/CRUD_helper.go | 57 ++++++++++++++++++++++++++++++++++++ routes/routes.go | 8 ++++- 4 files changed, 88 insertions(+), 9 deletions(-) create mode 100644 routes/api/v1/CRUD_helper.go diff --git a/models/helper.go b/models/helper.go index 368168fc4f..fad4813dac 100644 --- a/models/helper.go +++ b/models/helper.go @@ -14,3 +14,16 @@ func GetIntURLParam(param string, c echo.Context) (intParam int64, err error) { return intParam, err } + +func GetByID(id int64, result interface{}) (err error) { + exists, err := x.ID(id).Get(result) + if err != nil { + return err + } + + if !exists { + return ErrListDoesNotExist{} + } + + return +} \ No newline at end of file diff --git a/models/lists.go b/models/lists.go index b4bbdc795f..686eb22ee8 100644 --- a/models/lists.go +++ b/models/lists.go @@ -15,6 +15,15 @@ type List struct { Updated int64 `xorm:"updated" json:"updated"` } +func (l *List) AfterLoad() { + + // Get the owner + l.Owner, _, _ = GetUserByID(l.OwnerID) + + // Get the list items + l.Items, _ = GetItemsByListID(l.ID) +} + // GetListByID returns a list by its ID func GetListByID(id int64) (list List, err error) { exists, err := x.ID(id).Get(&list) // tName ist hässlich, geht das nicht auch anders? @@ -26,17 +35,11 @@ func GetListByID(id int64) (list List, err error) { return list, ErrListDoesNotExist{ID: id} } - // Get the list owner - list.Owner, _, err = GetUserByID(list.OwnerID) - if err != nil { - return List{}, err - } - - items, err := GetItemsByListID(list.ID) + /*items, err := GetItemsByListID(list.ID) if err != nil { return } - list.Items = items + list.Items = items*/ return list, nil } diff --git a/routes/api/v1/CRUD_helper.go b/routes/api/v1/CRUD_helper.go new file mode 100644 index 0000000000..37c9b607e3 --- /dev/null +++ b/routes/api/v1/CRUD_helper.go @@ -0,0 +1,57 @@ +package v1 + +import ( + "git.kolaente.de/konrad/list/models" + "github.com/labstack/echo" + "net/http" + "strconv" +) + +// Basic Method definitions +type CRUD interface { + Create() + Read(int64) (error) + Update() + Delete() +} + +// We use this to acces the default methods +type DefaultCRUD struct { + CRUD + Target interface{} +} + +// This method gets our data, which will be called by ReadWeb() +func (d *DefaultCRUD) Read(id int64) (err error) { + return models.GetByID(id, d.Target) +} + +// This does web stuff, aka returns json etc. Uses DefaultCRUD Methods to get the data +type CRUDWebHandler struct { + CObject CRUD +} + +// This does json, handles the request +func (c *CRUDWebHandler) ReadOneWeb(ctx echo.Context) error { + + // Get the ID + id, err := strconv.ParseInt(ctx.Param("id"), 10, 64) + if err != nil { + return ctx.JSON(http.StatusBadRequest, models.Message{"Invalid ID."}) + } + + // TODO check rights + + // Get our object + err = c.CObject.Read(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."}) + } + + // TODO how can we return c.CObject.Targetdirectly? + return ctx.JSON(http.StatusOK, c.CObject) +} diff --git a/routes/routes.go b/routes/routes.go index a61f7441bc..8e9a2f9a19 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -84,7 +84,13 @@ func RegisterRoutes(e *echo.Echo) { a.POST("/tokenTest", apiv1.CheckToken) a.GET("/lists", apiv1.GetListsByUser) - a.GET("/lists/:id", apiv1.GetListByID) + //a.GET("/lists/:id", apiv1.GetListByID) + listHandler := &apiv1.CRUDWebHandler{ + CObject: &apiv1.DefaultCRUD{ + Target: &models.List{}, + }, + } + a.GET("/lists/:id", listHandler.ReadOneWeb) a.POST("/lists/:id", apiv1.UpdateList) a.PUT("/lists/:id", apiv1.AddListItem) a.DELETE("/lists/:id", apiv1.DeleteListByID)