vikunja-api/models/lists.go

76 lines
1.8 KiB
Go
Raw Normal View History

2018-06-10 14:14:10 +02:00
package models
2018-06-10 14:22:37 +02:00
// List represents a list of items
2018-06-10 14:14:10 +02:00
type List struct {
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"`
Title string `xorm:"varchar(250)" json:"title"`
Description string `xorm:"varchar(1000)" json:"description"`
OwnerID int64 `xorm:"int(11)" json:"-"`
NamespaceID int64 `xorm:"int(11)" json:"-"`
2018-06-10 15:55:56 +02:00
2018-07-03 08:48:28 +02:00
Owner User `xorm:"-" json:"owner"`
Items []*ListItem `xorm:"-" json:"items"`
2018-07-04 08:15:47 +02:00
Created int64 `xorm:"created" json:"created"`
Updated int64 `xorm:"updated" json:"updated"`
CRUDable `xorm:"-" json:"-"`
2018-06-10 14:14:10 +02:00
}
2018-07-09 19:49:27 +02:00
// Lists is a multiple of list
type Lists []List
// AfterLoad loads the owner and list items
2018-07-07 14:19:34 +02:00
func (l *List) AfterLoad() {
// Get the owner
l.Owner, _, _ = GetUserByID(l.OwnerID)
// Get the list items
l.Items, _ = GetItemsByListID(l.ID)
}
2018-06-10 14:22:37 +02:00
// GetListByID returns a list by its ID
2018-07-04 08:15:47 +02:00
func GetListByID(id int64) (list List, err error) {
2018-07-09 19:49:27 +02:00
exists, err := x.ID(id).Get(&list)
2018-06-10 14:14:10 +02:00
if err != nil {
2018-07-06 08:40:35 +02:00
return list, err
2018-06-10 14:14:10 +02:00
}
if !exists {
2018-07-06 08:40:35 +02:00
return list, ErrListDoesNotExist{ID: id}
2018-06-10 14:14:10 +02:00
}
return list, nil
}
func GetListsByNamespaceID(nID int64) (lists []*List, err error) {
err = x.Where("namespace_id = ?", nID).Find(&lists)
return lists, err
2018-07-03 08:48:28 +02:00
}
2018-07-09 19:49:27 +02:00
// ReadAll gets all List a user has access to
func (list *List) ReadAll(user *User) (interface{}, error) {
lists := Lists{}
fullUser, _, err := GetUserByID(user.ID)
if err != nil {
2018-07-09 19:49:27 +02:00
return lists, err
}
2018-07-09 19:49:27 +02:00
// TODO: namespaces...
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).
2018-07-09 19:49:27 +02:00
Find(&lists)
2018-07-09 19:49:27 +02:00
return lists, err
}
2018-07-09 19:49:27 +02:00
// ReadOne gets one list by its ID
func (l *List) ReadOne(id int64) (err error) {
*l, err = GetListByID(id)
return
}