70 lines
1.5 KiB
Go
70 lines
1.5 KiB
Go
package models
|
|
|
|
// List represents a list of items
|
|
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:"-"`
|
|
|
|
Owner User `xorm:"-" json:"owner"`
|
|
Items []*ListItem `xorm:"-" json:"items"`
|
|
|
|
Created int64 `xorm:"created" json:"created"`
|
|
Updated int64 `xorm:"updated" json:"updated"`
|
|
}
|
|
|
|
// GetListByID returns a list by its ID
|
|
func GetListByID(id int64) (list List, err error) {
|
|
list.ID = id
|
|
exists, err := x.Get(&list)
|
|
if err != nil {
|
|
return List{}, err
|
|
}
|
|
|
|
if !exists {
|
|
return List{}, ErrListDoesNotExist{ID: id}
|
|
}
|
|
|
|
// Get the list owner
|
|
user, _, err := GetUserByID(list.OwnerID)
|
|
if err != nil {
|
|
return List{}, err
|
|
}
|
|
|
|
list.Owner = user
|
|
list.Owner.Password = ""
|
|
|
|
items, err := GetItemsByListID(list.ID)
|
|
if err != nil {
|
|
return
|
|
}
|
|
list.Items = items
|
|
|
|
return list, nil
|
|
}
|
|
|
|
// GetListsByUser gets all lists a user owns
|
|
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) {
|
|
err = x.Where("namespace_id = ?", nID).Find(&lists)
|
|
return lists, err
|
|
}
|