2018-11-26 21:17:33 +01:00
|
|
|
// Vikunja is a todo-list application to facilitate your life.
|
|
|
|
// Copyright 2018 Vikunja and contributors. All rights reserved.
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2018-06-10 14:14:10 +02:00
|
|
|
package models
|
|
|
|
|
2018-12-01 00:26:56 +01:00
|
|
|
import (
|
|
|
|
"code.vikunja.io/web"
|
|
|
|
)
|
2018-11-02 17:59:49 +01:00
|
|
|
|
2018-08-30 08:09:17 +02:00
|
|
|
// List represents a list of tasks
|
2018-06-10 14:14:10 +02:00
|
|
|
type List struct {
|
2019-01-03 23:22:06 +01:00
|
|
|
// The unique, numeric id of this list.
|
|
|
|
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"list"`
|
|
|
|
// The title of the list. You'll see this in the namespace overview.
|
2019-03-29 18:54:35 +01:00
|
|
|
Title string `xorm:"varchar(250) not null" json:"title" valid:"required,runelength(3|250)" minLength:"3" maxLength:"250"`
|
2019-01-03 23:22:06 +01:00
|
|
|
// The description of the list.
|
2019-03-29 18:54:35 +01:00
|
|
|
Description string `xorm:"varchar(1000) null" json:"description" valid:"runelength(0|1000)" maxLength:"1000"`
|
|
|
|
OwnerID int64 `xorm:"int(11) INDEX not null" json:"-"`
|
|
|
|
NamespaceID int64 `xorm:"int(11) INDEX not null" json:"-" param:"namespace"`
|
2018-06-10 15:55:56 +02:00
|
|
|
|
2019-01-03 23:22:06 +01:00
|
|
|
// The user who created this list.
|
|
|
|
Owner User `xorm:"-" json:"owner" valid:"-"`
|
|
|
|
// An array of tasks which belong to the list.
|
2018-08-30 08:09:17 +02:00
|
|
|
Tasks []*ListTask `xorm:"-" json:"tasks"`
|
2018-07-04 08:15:47 +02:00
|
|
|
|
2019-01-03 23:22:06 +01:00
|
|
|
// A unix timestamp when this list was created. You cannot change this value.
|
2019-03-29 18:54:35 +01:00
|
|
|
Created int64 `xorm:"created not null" json:"created"`
|
2019-01-03 23:22:06 +01:00
|
|
|
// A unix timestamp when this list was last updated. You cannot change this value.
|
2019-03-29 18:54:35 +01:00
|
|
|
Updated int64 `xorm:"updated not null" json:"updated"`
|
2018-07-08 22:50:01 +02:00
|
|
|
|
2018-12-01 00:26:56 +01:00
|
|
|
web.CRUDable `xorm:"-" json:"-"`
|
|
|
|
web.Rights `xorm:"-" json:"-"`
|
2018-06-10 14:14:10 +02:00
|
|
|
}
|
|
|
|
|
2018-07-10 14:02:23 +02:00
|
|
|
// GetListsByNamespaceID gets all lists in a namespace
|
2018-12-04 11:16:42 +01:00
|
|
|
func GetListsByNamespaceID(nID int64, doer *User) (lists []*List, err error) {
|
|
|
|
if nID == -1 {
|
|
|
|
err = x.Select("l.*").
|
|
|
|
Table("list").
|
|
|
|
Alias("l").
|
|
|
|
Join("LEFT", []string{"team_list", "tl"}, "l.id = tl.list_id").
|
|
|
|
Join("LEFT", []string{"team_members", "tm"}, "tm.team_id = tl.team_id").
|
|
|
|
Join("LEFT", []string{"users_list", "ul"}, "ul.list_id = l.id").
|
|
|
|
Where("tm.user_id = ?", doer.ID).
|
|
|
|
Or("ul.user_id = ?", doer.ID).
|
|
|
|
GroupBy("l.id").
|
|
|
|
Find(&lists)
|
|
|
|
} else {
|
|
|
|
err = x.Where("namespace_id = ?", nID).Find(&lists)
|
|
|
|
}
|
2019-02-18 20:32:41 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-04 11:16:42 +01:00
|
|
|
|
|
|
|
// get more list details
|
|
|
|
err = AddListDetails(lists)
|
2018-07-04 08:56:52 +02:00
|
|
|
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-11-12 16:46:35 +01:00
|
|
|
// @Summary Get all lists a user has access to
|
|
|
|
// @Description Returns all lists a user has access to.
|
|
|
|
// @tags list
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param p query int false "The page number. Used for pagination. If not provided, the first page of results is returned."
|
|
|
|
// @Param s query string false "Search lists by title."
|
2019-01-03 23:22:06 +01:00
|
|
|
// @Security JWTKeyAuth
|
2018-11-12 16:46:35 +01:00
|
|
|
// @Success 200 {array} models.List "The lists"
|
2018-12-01 02:59:17 +01:00
|
|
|
// @Failure 403 {object} code.vikunja.io/web.HTTPError "The user does not have access to the list"
|
2018-11-12 16:46:35 +01:00
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /lists [get]
|
2018-12-01 00:26:56 +01:00
|
|
|
func (l *List) ReadAll(search string, a web.Auth, page int) (interface{}, error) {
|
|
|
|
u, err := getUserWithError(a)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-09 18:33:06 +01:00
|
|
|
lists, err := getRawListsForUser(search, u, page)
|
2018-07-08 22:50:01 +02:00
|
|
|
if err != nil {
|
2018-11-02 17:59:49 +01:00
|
|
|
return nil, err
|
2018-07-08 22:50:01 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 19:16:14 +02:00
|
|
|
// Add more list details
|
2018-10-31 13:42:38 +01:00
|
|
|
AddListDetails(lists)
|
2018-10-05 19:16:14 +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-11-12 16:46:35 +01:00
|
|
|
// @Summary Gets one list
|
|
|
|
// @Description Returns a list by its ID.
|
|
|
|
// @tags list
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
2019-01-03 23:22:06 +01:00
|
|
|
// @Security JWTKeyAuth
|
2018-11-12 16:46:35 +01:00
|
|
|
// @Param id path int true "List ID"
|
|
|
|
// @Success 200 {object} models.List "The list"
|
2018-12-01 02:59:17 +01:00
|
|
|
// @Failure 403 {object} code.vikunja.io/web.HTTPError "The user does not have access to the list"
|
2018-11-12 16:46:35 +01:00
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /lists/{id} [get]
|
2018-07-21 15:08:46 +02:00
|
|
|
func (l *List) ReadOne() (err error) {
|
2018-10-06 13:05:29 +02:00
|
|
|
err = l.GetSimpleByID()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get list tasks
|
|
|
|
l.Tasks, err = GetTasksByListID(l.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get list owner
|
|
|
|
l.Owner, err = GetUserByID(l.OwnerID)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-06 13:07:42 +02:00
|
|
|
// GetSimpleByID gets a list with only the basic items, aka no tasks or user objects. Returns an error if the list does not exist.
|
2018-10-06 13:05:29 +02:00
|
|
|
func (l *List) GetSimpleByID() (err error) {
|
|
|
|
if l.ID < 1 {
|
|
|
|
return ErrListDoesNotExist{ID: l.ID}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to re-init our list object, because otherwise xorm creates a "where for every item in that list object,
|
|
|
|
// leading to not finding anything if the id is good, but for example the title is different.
|
|
|
|
id := l.ID
|
|
|
|
*l = List{}
|
|
|
|
exists, err := x.Where("id = ?", id).Get(l)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
return ErrListDoesNotExist{ID: l.ID}
|
|
|
|
}
|
|
|
|
|
2018-07-08 22:50:01 +02:00
|
|
|
return
|
|
|
|
}
|
2018-10-05 19:16:14 +02:00
|
|
|
|
2019-01-08 20:13:07 +01:00
|
|
|
// GetListSimplByTaskID gets a list by a task id
|
|
|
|
func GetListSimplByTaskID(taskID int64) (l *List, err error) {
|
|
|
|
// We need to re-init our list object, because otherwise xorm creates a "where for every item in that list object,
|
|
|
|
// leading to not finding anything if the id is good, but for example the title is different.
|
|
|
|
var list List
|
|
|
|
exists, err := x.
|
|
|
|
Select("list.*").
|
|
|
|
Table(List{}).
|
|
|
|
Join("INNER", "tasks", "list.id = tasks.list_id").
|
|
|
|
Where("tasks.id = ?", taskID).
|
|
|
|
Get(&list)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
return &List{}, ErrListDoesNotExist{ID: l.ID}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &list, nil
|
|
|
|
}
|
|
|
|
|
2018-11-02 17:59:49 +01:00
|
|
|
// Gets the lists only, without any tasks or so
|
2018-11-09 18:33:06 +01:00
|
|
|
func getRawListsForUser(search string, u *User, page int) (lists []*List, err error) {
|
2018-11-02 17:59:49 +01:00
|
|
|
fullUser, err := GetUserByID(u.ID)
|
|
|
|
if err != nil {
|
|
|
|
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").
|
|
|
|
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).
|
|
|
|
Or("ul.user_id = ?", fullUser.ID).
|
|
|
|
Or("un.user_id = ?", fullUser.ID).
|
|
|
|
GroupBy("l.id").
|
2018-11-09 11:30:17 +01:00
|
|
|
Limit(getLimitFromPageIndex(page)).
|
2018-11-09 18:33:06 +01:00
|
|
|
Where("l.title LIKE ?", "%"+search+"%").
|
2018-11-02 17:59:49 +01:00
|
|
|
Find(&lists)
|
|
|
|
|
|
|
|
return lists, err
|
|
|
|
}
|
|
|
|
|
2018-10-31 13:42:38 +01:00
|
|
|
// AddListDetails adds owner user objects and list tasks to all lists in the slice
|
|
|
|
func AddListDetails(lists []*List) (err error) {
|
2018-10-05 19:16:14 +02:00
|
|
|
var listIDs []int64
|
|
|
|
var ownerIDs []int64
|
|
|
|
for _, l := range lists {
|
|
|
|
listIDs = append(listIDs, l.ID)
|
|
|
|
ownerIDs = append(ownerIDs, l.OwnerID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all tasks
|
2018-10-31 13:42:38 +01:00
|
|
|
ts := []*ListTask{}
|
|
|
|
err = x.In("list_id", listIDs).Find(&ts)
|
2018-10-05 19:16:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all list owners
|
|
|
|
owners := []*User{}
|
|
|
|
err = x.In("id", ownerIDs).Find(&owners)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build it all into the lists slice
|
|
|
|
for in, list := range lists {
|
|
|
|
// Owner
|
|
|
|
for _, owner := range owners {
|
|
|
|
if list.OwnerID == owner.ID {
|
|
|
|
lists[in].Owner = *owner
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tasks
|
2018-10-31 13:42:38 +01:00
|
|
|
for _, task := range ts {
|
2018-10-05 19:16:14 +02:00
|
|
|
if task.ListID == list.ID {
|
|
|
|
lists[in].Tasks = append(lists[in].Tasks, task)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|