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"`
|
2018-06-13 12:18:55 +02:00
|
|
|
OwnerID int64 `xorm:"int(11)" json:"-"`
|
2018-07-03 08:26:28 +02:00
|
|
|
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"`
|
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-06-10 14:14:10 +02:00
|
|
|
}
|
|
|
|
|
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-06 08:40:35 +02:00
|
|
|
exists, err := x.ID(id).Get(&list) // tName ist hässlich, geht das nicht auch anders?
|
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
|
|
|
}
|
|
|
|
|
2018-07-07 14:19:34 +02:00
|
|
|
/*items, err := GetItemsByListID(list.ID)
|
2018-06-10 15:55:56 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-07-07 14:19:34 +02:00
|
|
|
list.Items = items*/
|
2018-06-10 15:55:56 +02:00
|
|
|
|
2018-06-10 14:14:10 +02:00
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
2018-06-10 14:43:35 +02:00
|
|
|
// GetListsByUser gets all lists a user owns
|
2018-06-10 14:41:42 +02:00
|
|
|
func GetListsByUser(user *User) (lists []*List, err error) {
|
|
|
|
fullUser, _, err := GetUserByID(user.ID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = x.Where("owner_id = ?", user.ID).Find(&lists)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for in := range lists {
|
|
|
|
lists[in].Owner = fullUser
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
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
|
|
|
}
|