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 {
|
2018-07-21 15:08:46 +02:00
|
|
|
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"list"`
|
2018-06-10 14:14:10 +02:00
|
|
|
Title string `xorm:"varchar(250)" json:"title"`
|
|
|
|
Description string `xorm:"varchar(1000)" json:"description"`
|
2018-06-13 12:18:55 +02:00
|
|
|
OwnerID int64 `xorm:"int(11)" json:"-"`
|
2018-07-21 15:08:46 +02:00
|
|
|
NamespaceID int64 `xorm:"int(11)" json:"-" param:"namespace"`
|
2018-06-10 15:55:56 +02:00
|
|
|
|
2018-07-03 08:48:28 +02:00
|
|
|
Owner User `xorm:"-" json:"owner"`
|
2018-06-10 19:49:40 +02:00
|
|
|
Items []*ListItem `xorm:"-" json:"items"`
|
2018-07-04 08:15:47 +02:00
|
|
|
|
2018-07-04 19:21:04 +02:00
|
|
|
Created int64 `xorm:"created" json:"created"`
|
|
|
|
Updated int64 `xorm:"updated" json:"updated"`
|
2018-07-08 22:50:01 +02:00
|
|
|
|
|
|
|
CRUDable `xorm:"-" json:"-"`
|
2018-07-10 14:02:23 +02:00
|
|
|
Rights `xorm:"-" json:"-"`
|
2018-06-10 14:14:10 +02:00
|
|
|
}
|
|
|
|
|
2018-07-09 19:49:27 +02:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2018-07-10 14:02:23 +02:00
|
|
|
// GetListsByNamespaceID gets all lists in a namespace
|
2018-07-03 08:26:28 +02:00
|
|
|
func GetListsByNamespaceID(nID int64) (lists []*List, err error) {
|
2018-07-04 08:56:52 +02:00
|
|
|
err = x.Where("namespace_id = ?", nID).Find(&lists)
|
|
|
|
return lists, err
|
2018-07-03 08:48:28 +02:00
|
|
|
}
|
2018-07-08 22:50:01 +02:00
|
|
|
|
2018-07-11 13:00:00 +02:00
|
|
|
// ReadAll gets all lists a user has access to
|
2018-07-10 14:02:23 +02:00
|
|
|
func (l *List) ReadAll(user *User) (interface{}, error) {
|
2018-07-12 21:10:58 +02:00
|
|
|
lists := []List{}
|
2018-07-08 22:50:01 +02:00
|
|
|
fullUser, _, err := GetUserByID(user.ID)
|
|
|
|
if err != nil {
|
2018-07-09 19:49:27 +02:00
|
|
|
return lists, err
|
2018-07-08 22:50:01 +02:00
|
|
|
}
|
|
|
|
|
2018-07-12 21:07:59 +02:00
|
|
|
// 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").
|
|
|
|
Where("tm.user_id = ?", fullUser.ID).
|
|
|
|
Or("tm2.user_id = ?", fullUser.ID).
|
|
|
|
Or("l.owner_id = ?", fullUser.ID).
|
2018-07-09 19:49:27 +02:00
|
|
|
Find(&lists)
|
2018-07-08 22:50:01 +02:00
|
|
|
|
2018-07-09 19:49:27 +02:00
|
|
|
return lists, err
|
2018-07-08 22:50:01 +02:00
|
|
|
}
|
|
|
|
|
2018-07-09 19:49:27 +02:00
|
|
|
// ReadOne gets one list by its ID
|
2018-07-21 15:08:46 +02:00
|
|
|
func (l *List) ReadOne() (err error) {
|
|
|
|
*l, err = GetListByID(l.ID)
|
2018-07-08 22:50:01 +02:00
|
|
|
return
|
|
|
|
}
|