Implemented CRUD Helper, every struct needs its own methods
This commit is contained in:
parent
0c5ed914e0
commit
d5eb2f08e3
7 changed files with 116 additions and 68 deletions
9
models/crudable.go
Normal file
9
models/crudable.go
Normal file
|
@ -0,0 +1,9 @@
|
|||
package models
|
||||
|
||||
type CRUDable interface {
|
||||
Create()
|
||||
ReadOne(int64) error
|
||||
ReadAll(*User) (interface{}, error)
|
||||
Update()
|
||||
Delete()
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/labstack/echo"
|
||||
"strconv"
|
||||
)
|
||||
|
@ -26,4 +27,10 @@ func GetByID(id int64, result interface{}) (err error) {
|
|||
}
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func GetAllByUser(user *User, result interface{}) (err error) {
|
||||
fmt.Println(result)
|
||||
err = x.Where("owner_id = ", user.ID).Find(result)
|
||||
return
|
||||
}
|
||||
|
|
|
@ -13,6 +13,8 @@ type List struct {
|
|||
|
||||
Created int64 `xorm:"created" json:"created"`
|
||||
Updated int64 `xorm:"updated" json:"updated"`
|
||||
|
||||
CRUDable `xorm:"-" json:"-"`
|
||||
}
|
||||
|
||||
func (l *List) AfterLoad() {
|
||||
|
@ -35,12 +37,6 @@ func GetListByID(id int64) (list List, err error) {
|
|||
return list, ErrListDoesNotExist{ID: id}
|
||||
}
|
||||
|
||||
/*items, err := GetItemsByListID(list.ID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
list.Items = items*/
|
||||
|
||||
return list, nil
|
||||
}
|
||||
|
||||
|
@ -67,3 +63,35 @@ func GetListsByNamespaceID(nID int64) (lists []*List, err error) {
|
|||
err = x.Where("namespace_id = ?", nID).Find(&lists)
|
||||
return lists, err
|
||||
}
|
||||
|
||||
func (list *List) ReadAll(user *User) (interface{}, error) {
|
||||
lists := Lists{}
|
||||
err := lists.ReadAll(user)
|
||||
return lists, err
|
||||
}
|
||||
|
||||
type Lists []List
|
||||
|
||||
func (lists *Lists) ReadAll(user *User) (err error) {
|
||||
fullUser, _, err := GetUserByID(user.ID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = x.Select("list.*").
|
||||
Join("LEFT", "team_list", "list.id = team_list.list_id").
|
||||
Join("LEFT", "team_members", "team_members.team_id = team_list.team_id").
|
||||
Where("team_members.user_id = ?", fullUser.ID).
|
||||
Or("list.owner_id = ?", fullUser.ID).
|
||||
Find(lists)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (l *List) ReadOne(id int64) (err error) {
|
||||
*l, err = GetListByID(id)
|
||||
return
|
||||
}
|
||||
|
|
5
models/rights.go
Normal file
5
models/rights.go
Normal file
|
@ -0,0 +1,5 @@
|
|||
package models
|
||||
|
||||
type Rights interface {
|
||||
IsAdmin(*User) bool
|
||||
}
|
56
routes/CRUD/CRUD_helper.go
Normal file
56
routes/CRUD/CRUD_helper.go
Normal file
|
@ -0,0 +1,56 @@
|
|||
package CRUD
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.kolaente.de/konrad/list/models"
|
||||
"github.com/labstack/echo"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// This does web stuff, aka returns json etc. Uses CRUDable Methods to get the data
|
||||
type CRUDWebHandler struct {
|
||||
CObject interface{ models.CRUDable }
|
||||
}
|
||||
|
||||
// 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.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)
|
||||
}
|
||||
|
||||
//
|
||||
func (c *CRUDWebHandler) 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."})
|
||||
}
|
||||
|
||||
//c.CObject.IsAdmin()
|
||||
|
||||
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)
|
||||
}
|
|
@ -1,56 +0,0 @@
|
|||
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 *DefaultCRUD
|
||||
}
|
||||
|
||||
// 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."})
|
||||
}
|
||||
|
||||
return ctx.JSON(http.StatusOK, c.CObject.Target)
|
||||
}
|
|
@ -30,6 +30,7 @@ import (
|
|||
"github.com/labstack/echo/middleware"
|
||||
|
||||
"git.kolaente.de/konrad/list/models"
|
||||
CRUD "git.kolaente.de/konrad/list/routes/CRUD"
|
||||
apiv1 "git.kolaente.de/konrad/list/routes/api/v1"
|
||||
_ "git.kolaente.de/konrad/list/routes/api/v1/swagger" // for docs generation
|
||||
)
|
||||
|
@ -83,12 +84,10 @@ func RegisterRoutes(e *echo.Echo) {
|
|||
a.Use(middleware.JWT(models.Config.JWTLoginSecret))
|
||||
a.POST("/tokenTest", apiv1.CheckToken)
|
||||
|
||||
a.GET("/lists", apiv1.GetListsByUser)
|
||||
listHandler := &apiv1.CRUDWebHandler{
|
||||
CObject: &apiv1.DefaultCRUD{
|
||||
Target: &models.List{},
|
||||
},
|
||||
listHandler := &CRUD.CRUDWebHandler{
|
||||
CObject: &models.List{},
|
||||
}
|
||||
a.GET("/lists", listHandler.ReadAllWeb)
|
||||
a.GET("/lists/:id", listHandler.ReadOneWeb)
|
||||
a.POST("/lists/:id", apiv1.UpdateList)
|
||||
a.PUT("/lists/:id", apiv1.AddListItem)
|
||||
|
|
Loading…
Reference in a new issue