2020-12-29 02:04:20 +01:00
// Vikunja is a to-do list application to facilitate your life.
2021-02-02 20:19:13 +01:00
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
2019-11-29 23:59:20 +01:00
//
2020-12-29 02:04:20 +01:00
// This program is free software: you can redistribute it and/or modify
2020-12-23 16:41:52 +01:00
// it under the terms of the GNU Affero General Public Licensee as published by
2019-11-29 23:59:20 +01:00
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
2020-12-29 02:04:20 +01:00
// This program is distributed in the hope that it will be useful,
2019-11-29 23:59:20 +01:00
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2020-12-23 16:41:52 +01:00
// GNU Affero General Public Licensee for more details.
2019-11-29 23:59:20 +01:00
//
2020-12-23 16:41:52 +01:00
// You should have received a copy of the GNU Affero General Public Licensee
2020-12-29 02:04:20 +01:00
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2019-11-29 23:59:20 +01:00
package models
import (
2020-01-26 18:08:06 +01:00
"code.vikunja.io/api/pkg/user"
2019-11-29 23:59:20 +01:00
"code.vikunja.io/web"
2020-12-23 16:32:28 +01:00
"xorm.io/xorm"
2019-11-29 23:59:20 +01:00
)
// TaskCollection is a struct used to hold filter details and not clutter the Task struct with information not related to actual tasks.
type TaskCollection struct {
2020-09-26 23:02:17 +02:00
ListID int64 ` param:"list" json:"-" `
Lists [ ] * List ` json:"-" `
2019-11-29 23:59:20 +01:00
2019-12-07 15:30:51 +01:00
// The query parameter to sort by. This is for ex. done, priority, etc.
2020-09-26 23:02:17 +02:00
SortBy [ ] string ` query:"sort_by" json:"sort_by" `
SortByArr [ ] string ` query:"sort_by[]" json:"-" `
2019-12-07 15:30:51 +01:00
// The query parameter to order the items by. This can be either asc or desc, with asc being the default.
2020-09-26 23:02:17 +02:00
OrderBy [ ] string ` query:"order_by" json:"order_by" `
OrderByArr [ ] string ` query:"order_by[]" json:"-" `
2019-12-07 15:30:51 +01:00
2020-04-11 16:20:33 +02:00
// The field name of the field to filter by
2020-09-26 23:02:17 +02:00
FilterBy [ ] string ` query:"filter_by" json:"filter_by" `
FilterByArr [ ] string ` query:"filter_by[]" json:"-" `
2020-04-11 16:20:33 +02:00
// The value of the field name to filter by
2020-09-26 23:02:17 +02:00
FilterValue [ ] string ` query:"filter_value" json:"filter_value" `
FilterValueArr [ ] string ` query:"filter_value[]" json:"-" `
2020-04-11 16:20:33 +02:00
// The comparator for field and value
2020-09-26 23:02:17 +02:00
FilterComparator [ ] string ` query:"filter_comparator" json:"filter_comparator" `
FilterComparatorArr [ ] string ` query:"filter_comparator[]" json:"-" `
2020-05-09 17:48:56 +02:00
// The way all filter conditions are concatenated together, can be either "and" or "or".,
2020-09-26 23:02:17 +02:00
FilterConcat string ` query:"filter_concat" json:"filter_concat" `
2020-06-27 19:04:01 +02:00
// If set to true, the result will also include null values
2020-09-26 23:02:17 +02:00
FilterIncludeNulls bool ` query:"filter_include_nulls" json:"filter_include_nulls" `
2020-04-11 16:20:33 +02:00
2019-11-29 23:59:20 +01:00
web . CRUDable ` xorm:"-" json:"-" `
web . Rights ` xorm:"-" json:"-" `
}
2020-04-11 16:20:33 +02:00
func validateTaskField ( fieldName string ) error {
switch fieldName {
case
taskPropertyID ,
2020-05-16 12:17:44 +02:00
taskPropertyTitle ,
2020-04-11 16:20:33 +02:00
taskPropertyDescription ,
taskPropertyDone ,
2020-06-27 19:04:01 +02:00
taskPropertyDoneAt ,
taskPropertyDueDate ,
2020-04-11 16:20:33 +02:00
taskPropertyCreatedByID ,
taskPropertyListID ,
taskPropertyRepeatAfter ,
taskPropertyPriority ,
2020-06-27 19:04:01 +02:00
taskPropertyStartDate ,
taskPropertyEndDate ,
2020-04-11 16:20:33 +02:00
taskPropertyHexColor ,
taskPropertyPercentDone ,
taskPropertyUID ,
taskPropertyCreated ,
2020-04-24 17:23:03 +02:00
taskPropertyUpdated ,
taskPropertyPosition :
2020-04-11 16:20:33 +02:00
return nil
}
return ErrInvalidTaskField { TaskField : fieldName }
}
2019-11-29 23:59:20 +01:00
// ReadAll gets all tasks for a collection
2019-12-07 15:30:51 +01:00
// @Summary Get tasks in a list
2019-11-29 23:59:20 +01:00
// @Description Returns all tasks for the current list.
// @tags task
// @Accept json
// @Produce json
// @Param listID path int true "The list ID."
// @Param page query int false "The page number. Used for pagination. If not provided, the first page of results is returned."
// @Param per_page query int false "The maximum number of items per page. Note this parameter is limited by the configured maximum of items per page."
// @Param s query string false "Search tasks by task text."
2020-06-27 19:04:01 +02:00
// @Param sort_by query string false "The sorting parameter. You can pass this multiple times to get the tasks ordered by multiple different parametes, along with `order_by`. Possible values to sort by are `id`, `title`, `description`, `done`, `done_at`, `due_date`, `created_by_id`, `list_id`, `repeat_after`, `priority`, `start_date`, `end_date`, `hex_color`, `percent_done`, `uid`, `created`, `updated`. Default is `id`."
2019-12-07 15:30:51 +01:00
// @Param order_by query string false "The ordering parameter. Possible values to order by are `asc` or `desc`. Default is `asc`."
2020-12-22 00:13:15 +01:00
// @Param filter_by query string false "The name of the field to filter by. Allowed values are all task properties. Task properties which are their own object require passing in the id of that entity. Accepts an array for multiple filters which will be chanied together, all supplied filter must match."
2020-04-11 16:20:33 +02:00
// @Param filter_value query string false "The value to filter for."
2020-12-19 22:39:11 +01:00
// @Param filter_comparator query string false "The comparator to use for a filter. Available values are `equals`, `greater`, `greater_equals`, `less`, `less_equals`, `like` and `in`. `in` expects comma-separated values in `filter_value`. Defaults to `equals`"
2020-05-09 17:48:56 +02:00
// @Param filter_concat query string false "The concatinator to use for filters. Available values are `and` or `or`. Defaults to `or`."
2020-06-27 19:04:01 +02:00
// @Param filter_include_nulls query string false "If set to true the result will include filtered fields whose value is set to `null`. Available values are `true` or `false`. Defaults to `false`."
2019-11-29 23:59:20 +01:00
// @Security JWTKeyAuth
// @Success 200 {array} models.Task "The tasks"
// @Failure 500 {object} models.Message "Internal error"
// @Router /lists/{listID}/tasks [get]
2020-12-23 16:32:28 +01:00
func ( tf * TaskCollection ) ReadAll ( s * xorm . Session , a web . Auth , search string , page int , perPage int ) ( result interface { } , resultCount int , totalItems int64 , err error ) {
2019-12-07 15:30:51 +01:00
2020-09-26 23:02:17 +02:00
// If the list id is < -1 this means we're dealing with a saved filter - in that case we get and populate the filter
// -1 is the favorites list which works as intended
if tf . ListID < - 1 {
2020-12-23 16:32:28 +01:00
sf , err := getSavedFilterSimpleByID ( s , getSavedFilterIDFromListID ( tf . ListID ) )
2020-09-26 23:02:17 +02:00
if err != nil {
return nil , 0 , 0 , err
}
2020-12-23 16:32:28 +01:00
return sf . getTaskCollection ( ) . ReadAll ( s , a , search , page , perPage )
2020-09-26 23:02:17 +02:00
}
2019-12-07 16:57:19 +01:00
if len ( tf . SortByArr ) > 0 {
tf . SortBy = append ( tf . SortBy , tf . SortByArr ... )
}
if len ( tf . OrderByArr ) > 0 {
tf . OrderBy = append ( tf . OrderBy , tf . OrderByArr ... )
}
2019-12-07 15:30:51 +01:00
var sort = make ( [ ] * sortParam , 0 , len ( tf . SortBy ) )
for i , s := range tf . SortBy {
param := & sortParam {
2020-04-24 17:23:03 +02:00
sortBy : s ,
2019-12-07 15:30:51 +01:00
orderBy : orderAscending ,
}
// This checks if tf.OrderBy has an entry with the same index as the current entry from tf.SortBy
// Taken from https://stackoverflow.com/a/27252199/10924593
if len ( tf . OrderBy ) > i {
param . orderBy = getSortOrderFromString ( tf . OrderBy [ i ] )
}
2020-05-15 16:07:36 +02:00
2019-12-07 15:30:51 +01:00
// Param validation
if err := param . validate ( ) ; err != nil {
return nil , 0 , 0 , err
}
sort = append ( sort , param )
2019-11-29 23:59:20 +01:00
}
taskopts := & taskOptions {
2020-06-27 19:04:01 +02:00
search : search ,
page : page ,
perPage : perPage ,
sortby : sort ,
filterConcat : taskFilterConcatinator ( tf . FilterConcat ) ,
filterIncludeNulls : tf . FilterIncludeNulls ,
2020-04-11 16:20:33 +02:00
}
taskopts . filters , err = getTaskFiltersByCollections ( tf )
if err != nil {
return
2019-11-29 23:59:20 +01:00
}
shareAuth , is := a . ( * LinkSharing )
if is {
2020-12-23 16:32:28 +01:00
list , err := GetListSimpleByID ( s , shareAuth . ListID )
2019-11-29 23:59:20 +01:00
if err != nil {
return nil , 0 , 0 , err
}
2020-12-23 16:32:28 +01:00
return getTasksForLists ( s , [ ] * List { list } , a , taskopts )
2019-11-29 23:59:20 +01:00
}
// If the list ID is not set, we get all tasks for the user.
// This allows to use this function in Task.ReadAll with a possibility to deprecate the latter at some point.
if tf . ListID == 0 {
2020-12-23 16:32:28 +01:00
tf . Lists , _ , _ , err = getRawListsForUser (
s ,
& listOptions {
user : & user . User { ID : a . GetID ( ) } ,
page : - 1 ,
} ,
)
2019-11-29 23:59:20 +01:00
if err != nil {
return nil , 0 , 0 , err
}
} else {
// Check the list exists and the user has acess on it
list := & List { ID : tf . ListID }
2020-12-23 16:32:28 +01:00
canRead , _ , err := list . CanRead ( s , a )
2019-11-29 23:59:20 +01:00
if err != nil {
return nil , 0 , 0 , err
}
if ! canRead {
return nil , 0 , 0 , ErrUserDoesNotHaveAccessToList { ListID : tf . ListID }
}
tf . Lists = [ ] * List { { ID : tf . ListID } }
}
2020-12-23 16:32:28 +01:00
return getTasksForLists ( s , tf . Lists , a , taskopts )
2019-11-29 23:59:20 +01:00
}