vikunja/models/list.go

91 lines
2.6 KiB
Go
Raw Normal View History

2018-06-10 12:14:10 +00:00
package models
2018-08-30 06:09:17 +00:00
// List represents a list of tasks
2018-06-10 12:14:10 +00:00
type List struct {
2018-07-21 13:08:46 +00:00
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"list"`
2018-06-10 12:14:10 +00:00
Title string `xorm:"varchar(250)" json:"title"`
Description string `xorm:"varchar(1000)" json:"description"`
2018-10-05 16:46:01 +00:00
OwnerID int64 `xorm:"int(11) INDEX" json:"-"`
NamespaceID int64 `xorm:"int(11) INDEX" json:"-" param:"namespace"`
2018-06-10 13:55:56 +00:00
2018-07-03 06:48:28 +00:00
Owner User `xorm:"-" json:"owner"`
2018-08-30 06:09:17 +00:00
Tasks []*ListTask `xorm:"-" json:"tasks"`
2018-07-04 06:15:47 +00:00
Created int64 `xorm:"created" json:"created"`
Updated int64 `xorm:"updated" json:"updated"`
CRUDable `xorm:"-" json:"-"`
2018-07-10 12:02:23 +00:00
Rights `xorm:"-" json:"-"`
2018-06-10 12:14:10 +00:00
}
2018-08-30 06:09:17 +00:00
// AfterLoad loads the owner and list tasks
2018-07-07 12:19:34 +00:00
func (l *List) AfterLoad() {
// Get the owner
2018-08-30 17:14:16 +00:00
l.Owner, _ = GetUserByID(l.OwnerID)
2018-07-07 12:19:34 +00:00
2018-08-30 06:09:17 +00:00
// Get the list tasks
l.Tasks, _ = GetTasksByListID(l.ID)
2018-07-07 12:19:34 +00:00
}
2018-06-10 12:22:37 +00:00
// GetListByID returns a list by its ID
2018-07-04 06:15:47 +00:00
func GetListByID(id int64) (list List, err error) {
if id < 1 {
return list, ErrListDoesNotExist{ID: id}
}
2018-07-09 17:49:27 +00:00
exists, err := x.ID(id).Get(&list)
2018-06-10 12:14:10 +00:00
if err != nil {
2018-07-06 06:40:35 +00:00
return list, err
2018-06-10 12:14:10 +00:00
}
if !exists {
2018-07-06 06:40:35 +00:00
return list, ErrListDoesNotExist{ID: id}
2018-06-10 12:14:10 +00:00
}
return list, nil
}
2018-07-10 12:02:23 +00:00
// GetListsByNamespaceID gets all lists in a namespace
func GetListsByNamespaceID(nID int64) (lists []*List, err error) {
err = x.Where("namespace_id = ?", nID).Find(&lists)
return lists, err
2018-07-03 06:48:28 +00:00
}
// ReadAll gets all lists a user has access to
2018-07-10 12:02:23 +00:00
func (l *List) ReadAll(user *User) (interface{}, error) {
2018-07-12 19:10:58 +00:00
lists := []List{}
2018-08-30 17:14:02 +00:00
fullUser, err := GetUserByID(user.ID)
if err != nil {
2018-07-09 17:49:27 +00:00
return lists, err
}
// Gets all Lists where the user is either owner or in a team which has access to the list
// Or in a team which has namespace read access
err = x.Select("l.*").
Table("list").
Alias("l").
Join("INNER", []string{"namespaces", "n"}, "l.namespace_id = n.id").
Join("LEFT", []string{"team_namespaces", "tn"}, "tn.namespace_id = n.id").
Join("LEFT", []string{"team_members", "tm"}, "tm.team_id = tn.team_id").
Join("LEFT", []string{"team_list", "tl"}, "l.id = tl.list_id").
Join("LEFT", []string{"team_members", "tm2"}, "tm2.team_id = tl.team_id").
2018-09-06 06:42:18 +00:00
Join("LEFT", []string{"users_list", "ul"}, "ul.list_id = l.id").
Join("LEFT", []string{"users_namespace", "un"}, "un.namespace_id = l.namespace_id").
Where("tm.user_id = ?", fullUser.ID).
Or("tm2.user_id = ?", fullUser.ID).
Or("l.owner_id = ?", fullUser.ID).
2018-09-06 06:42:18 +00:00
Or("ul.user_id = ?", fullUser.ID).
Or("un.user_id = ?", fullUser.ID).
GroupBy("l.id").
2018-07-09 17:49:27 +00:00
Find(&lists)
2018-07-09 17:49:27 +00:00
return lists, err
}
2018-07-09 17:49:27 +00:00
// ReadOne gets one list by its ID
2018-07-21 13:08:46 +00:00
func (l *List) ReadOne() (err error) {
*l, err = GetListByID(l.ID)
return
}