vikunja/models/lists.go

74 lines
1.5 KiB
Go
Raw Normal View History

2018-06-10 12:14:10 +00:00
package models
2018-06-10 12:22:37 +00:00
// List represents a list of items
2018-06-10 12:14:10 +00: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 12:14:10 +00:00
Created int64 `xorm:"created" json:"created"`
Updated int64 `xorm:"updated" json:"updated"`
2018-06-10 13:55:56 +00:00
2018-07-03 06:48:28 +00:00
Owner User `xorm:"-" json:"owner"`
Items []*ListItem `xorm:"-" json:"items"`
2018-06-10 12:14:10 +00:00
}
2018-06-10 12:22:37 +00:00
// GetListByID returns a list by its ID
func GetListByID(id int64) (list *List, err error) {
2018-06-10 12:14:10 +00:00
list.ID = id
exists, err := x.Get(&list)
if err != nil {
return &List{}, err
2018-06-10 12:14:10 +00:00
}
if !exists {
return &List{}, ErrListDoesNotExist{ID: id}
2018-06-10 12:14:10 +00:00
}
// Get the list owner
user, _, err := GetUserByID(list.OwnerID)
if err != nil {
return &List{}, err
2018-06-10 12:14:10 +00:00
}
list.Owner = user
list.Owner.Password = ""
2018-06-10 12:14:10 +00:00
2018-06-10 13:55:56 +00:00
items, err := GetItemsByListID(list.ID)
if err != nil {
return
}
list.Items = items
2018-06-10 12:14:10 +00:00
return list, nil
}
2018-06-10 12:43:35 +00:00
// GetListsByUser gets all lists a user owns
2018-06-10 12:41:42 +00: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
}
func GetListsByNamespaceID(nID int64) (lists []*List, err error) {
exists, err := x.Where("namespace_id = ?", nID).Get(lists)
if !exists {
return lists, ErrNamespaceDoesNotExist{}
}
return
2018-07-03 06:48:28 +00:00
}