1
0

Implemented CRUD Helper, every struct needs its own methods

This commit is contained in:
kolaente
2018-07-08 22:50:01 +02:00
parent 0c5ed914e0
commit d5eb2f08e3
7 changed files with 116 additions and 68 deletions

9
models/crudable.go Normal file
View File

@ -0,0 +1,9 @@
package models
type CRUDable interface {
Create()
ReadOne(int64) error
ReadAll(*User) (interface{}, error)
Update()
Delete()
}

View File

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

View File

@ -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
View File

@ -0,0 +1,5 @@
package models
type Rights interface {
IsAdmin(*User) bool
}